Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving locale forwarder to its own library #710

Merged
merged 9 commits into from
Jun 19, 2014
8 changes: 8 additions & 0 deletions brjs-core/src/main/java/org/bladerunnerjs/model/BRJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class Messages {
private final NodeItem<DirNode> apiDocs = new NodeItem<>(this, DirNode.class, "sdk/docs/jsdoc");
private final NodeItem<DirNode> testResults = new NodeItem<>(this, DirNode.class, "sdk/test-results");

private final NodeItem<DirNode> localeForwarderUtilDir = new NodeItem<>(this, DirNode.class, "sdk/libs/locale-forwarder");
private static final String LOCALE_FORWARDER_FILENAME = "LocaleForwarder.js";

private WorkingDirNode workingDir;
private final Logger logger;
private final CommandList commandList;
Expand Down Expand Up @@ -293,6 +296,11 @@ public DirNode appJars()
return appJars.item();
}

public File localeForwarderUtil()
{
return localeForwarderUtilDir.item().file(LOCALE_FORWARDER_FILENAME);
}

public DirNode conf()
{
return configuration.item();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import java.io.IOException;
import java.util.List;

import javax.naming.InvalidNameException;

import org.apache.commons.io.FileUtils;
import org.bladerunnerjs.appserver.BRJSThreadSafeModelAccessor;
import org.bladerunnerjs.model.BRJS;
import org.bladerunnerjs.model.exception.modelupdate.ModelUpdateException;
import org.bladerunnerjs.plugin.AssetLocationPlugin;
import org.bladerunnerjs.plugin.AssetPlugin;
import org.bladerunnerjs.plugin.CommandPlugin;
Expand Down Expand Up @@ -311,4 +314,11 @@ public BuilderChainer hasDevVersion(String version)

return builderChainer;
}

public BuilderChainer localeForwarderHasContents(String string) throws IOException, InvalidNameException, ModelUpdateException
{
FileUtils.write(brjs.localeForwarderUtil(), string);

return builderChainer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,50 @@
import com.google.common.base.Joiner;


public class AppRequestHandler {
public class AppRequestHandler
{

public static final String LOCALE_FORWARDING_REQUEST = "locale-forwarding-request";
public static final String INDEX_PAGE_REQUEST = "index-page-request";
public static final String UNVERSIONED_BUNDLE_REQUEST = "unversioned-bundle-request";
public static final String BUNDLE_REQUEST = "bundle-request";
public static final String WORKBENCH_LOCALE_FORWARDING_REQUEST = "workbench-locale-forwarding-request";
public static final String WORKBENCH_INDEX_PAGE_REQUEST = "workbench-index-page-request";
public static final String WORKBENCH_BUNDLE_REQUEST = "workbench-bundle-request";

private final App app;
private final MemoizedValue<ContentPathParser> contentPathParser;

public AppRequestHandler(App app) {

public AppRequestHandler(App app)
{
this.app = app;
contentPathParser = new MemoizedValue<>("AppRequestHandler.contentPathParser", app.root(), app.dir());
}

public boolean canHandleLogicalRequest(String requestPath) {

public boolean canHandleLogicalRequest(String requestPath)
{
return getContentPathParser().canParseRequest(requestPath);
}

public void handleLogicalRequest(String requestPath, OutputStream os, PageAccessor pageAccessor) throws MalformedRequestException, ResourceNotFoundException, ContentProcessingException {

public void handleLogicalRequest(String requestPath, OutputStream os, PageAccessor pageAccessor) throws MalformedRequestException, ResourceNotFoundException, ContentProcessingException
{
ParsedContentPath parsedContentPath = getContentPathParser().parse(requestPath);
Map<String, String> pathProperties = parsedContentPath.properties;
String aspectName = getAspectName(requestPath, pathProperties);

String devVersion = app.root().getAppVersionGenerator().getDevVersion();

switch(parsedContentPath.formName) {

switch (parsedContentPath.formName)
{
case LOCALE_FORWARDING_REQUEST:
case WORKBENCH_LOCALE_FORWARDING_REQUEST:
writeLocaleForwardingPage(os);
break;

case INDEX_PAGE_REQUEST:
writeIndexPage(app.aspect(aspectName), pathProperties.get("locale"), devVersion, pageAccessor, os, RequestMode.Dev);
break;

case WORKBENCH_INDEX_PAGE_REQUEST:
writeIndexPage(app.bladeset(pathProperties.get("bladeset")).blade(pathProperties.get("blade")).workbench(), pathProperties.get("locale"), devVersion, pageAccessor, os, RequestMode.Dev);
break;
Expand All @@ -78,14 +84,15 @@ public void handleLogicalRequest(String requestPath, OutputStream os, PageAccess
case BUNDLE_REQUEST:
app.aspect(aspectName).handleLogicalRequest(pathProperties.get("content-path"), os, devVersion);
break;

case WORKBENCH_BUNDLE_REQUEST:
app.bladeset(pathProperties.get("bladeset")).blade(pathProperties.get("blade")).workbench().handleLogicalRequest(pathProperties.get("content-path"), os, devVersion);
break;
}
}

public String createRequest(String requestFormName, String... args) throws MalformedTokenException {

public String createRequest(String requestFormName, String... args) throws MalformedTokenException
{
return getContentPathParser().createRequest(requestFormName, args);
}

Expand All @@ -100,37 +107,42 @@ public void writeIndexPage(BrowsableNode browsableNode, String locale, String ve
try (Writer writer = new OutputStreamWriter(byteArrayOutputStream, browserCharacterEncoding)) {
browsableNode.filterIndexPage(indexPageContent, locale, version, writer, requestMode);
}

os.write(byteArrayOutputStream.toByteArray());
}
catch (IOException | ConfigException | ModelOperationException e) {
throw new ContentProcessingException(e, "Error when trying to write the index page for " + RelativePathUtility.get(browsableNode.root().dir(), indexPage));
}
}

private String getAspectName(String requestPath, Map<String, String> contentPathProperties) throws MalformedRequestException {
private String getAspectName(String requestPath, Map<String, String> contentPathProperties) throws MalformedRequestException
{
String aspectName = contentPathProperties.get("aspect");

if(aspectName.equals("default/")) {

if (aspectName.equals("default/"))
{
throw new MalformedRequestException(requestPath, "The '/default' prefix should be omitted for the default aspect.");
}
else if(aspectName.isEmpty()) {
else if (aspectName.isEmpty())
{
aspectName = "default";
}
else {
else
{
aspectName = aspectName.substring(0, aspectName.length() - 1);
}

return aspectName;
}

public void writeLocaleForwardingPage(OutputStream os) throws ContentProcessingException {
try(Writer writer = new OutputStreamWriter(os, app.root().bladerunnerConf().getBrowserCharacterEncoding());
Reader reader = new FileReader(app.root().sdkLibsDir().file("locale-forwarder.js"))) {
Reader reader = new FileReader( app.root().localeForwarderUtil() ) ) {
writer.write("<head>\n");
writer.write("<noscript><meta http-equiv='refresh' content='0; url=" + app.appConf().getDefaultLocale() + "/'></noscript>\n");
writer.write("<script type='text/javascript'>\n");
writer.write("var $appSupportedLocales = {'" + Joiner.on("':true, '").join(app.appConf().getLocales()) + "':true};\n");
writer.write("var define = function(requirePath, definition) { definition(null, window, null); }\n");
IOUtils.copy(reader, writer);
writer.write("\n</script>\n");
writer.write("</head>\n");
Expand All @@ -140,8 +152,9 @@ public void writeLocaleForwardingPage(OutputStream os) throws ContentProcessingE
throw new ContentProcessingException(e);
}
}

private ContentPathParser getContentPathParser() {

private ContentPathParser getContentPathParser()
{
return contentPathParser.value(() -> {
ContentPathParserBuilder contentPathParserBuilder = new ContentPathParserBuilder();
contentPathParserBuilder
Expand All @@ -164,14 +177,16 @@ private ContentPathParser getContentPathParser() {
return contentPathParserBuilder.build();
});
}

private String getAspectNames() {

private String getAspectNames()
{
List<String> aspectNames = new ArrayList<>();

for(Aspect aspect : app.aspects()) {

for (Aspect aspect : app.aspects())
{
aspectNames.add(aspect.getName());
}

return Joiner.on("|").join(aspectNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void initTestObjects() throws Exception {
given(brjs).hasModelObserverPlugins(new AppDeploymentObserverPlugin())
.and(brjs).hasContentPlugins(new MockContentPlugin())
.and(brjs).hasBeenCreated()
.and(brjs.sdkLibsDir()).containsFile("locale-forwarder.js")
.and(brjs).localeForwarderHasContents("locale-forwarder.js")
.and(brjs).containsFolder("apps")
.and(brjs).containsFolder("sdk/system-applications");
appServer = brjs.applicationServer(appServerPort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void indexPageCanBeAccessedWithAQueryString() throws Exception {
given(app).hasBeenPopulated()
.and(appServer).started()
.and(aspect).indexPageHasContent("index page")
.and(sdkLibsDir).containsFile("locale-forwarder.js");
.and(brjs).localeForwarderHasContents("locale-forwarder.js");
when(appServer).requestIsMadeFor("/app/en/?query=1", response);
then(response).textEquals("index page");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.bladerunnerjs.model.App;
import org.bladerunnerjs.model.Aspect;
import org.bladerunnerjs.model.DirNode;
import org.bladerunnerjs.plugin.plugins.commands.standard.BuildAppCommand;
import org.bladerunnerjs.testing.specutility.engine.SpecTest;
import org.bladerunnerjs.utility.ServerUtility;
Expand All @@ -20,7 +19,6 @@ public class ServedWarTest extends SpecTest {
private StringBuffer brjsResponse = new StringBuffer();
private Aspect aspect;
private Aspect loginAspect;
private DirNode sdkLibsDir;

@Before
public void initTestObjects() throws Exception
Expand All @@ -32,12 +30,11 @@ public void initTestObjects() throws Exception
app = brjs.app("app1");
aspect = app.aspect("default");
loginAspect = app.aspect("login");
sdkLibsDir = brjs.sdkLibsDir();
}

@Test
public void exportedWarCanBeDeployedOnAnAppServer() throws Exception {
given(sdkLibsDir).containsFileWithContents("locale-forwarder.js", "Locale Forwarder")
given(brjs).localeForwarderHasContents("Locale Forwarder")
.and(aspect).containsFileWithContents("index.html", "Hello World!")
.and(aspect).containsResourceFileWithContents("template.html", "<div id='template-id'>content</div>")
.and(brjs).hasProdVersion("1234")
Expand All @@ -54,7 +51,7 @@ public void exportedWarCanBeDeployedOnAnAppServer() throws Exception {

@Test
public void exportedWarIndexPageIsTheSameAsBrjsHosted() throws Exception {
given(sdkLibsDir).containsFile("locale-forwarder.js")
given(brjs).localeForwarderHasContents("locale-forwarder.js")
.and(aspect).containsFileWithContents("index.html", "Hello World!")
.and(app).hasBeenBuiltAsWar(brjs.dir())
.and(warServer).hasWar("app1.war", "app")
Expand All @@ -66,7 +63,7 @@ public void exportedWarIndexPageIsTheSameAsBrjsHosted() throws Exception {

@Test
public void exportedWarJsBundleIsTheSameAsBrjsHosted() throws Exception {
given(sdkLibsDir).containsFile("locale-forwarder.js")
given(brjs).localeForwarderHasContents("locale-forwarder.js")
.and(aspect).indexPageRequires("appns/Class")
.and(aspect).hasClass("appns/Class")
.and(brjs).hasProdVersion("APP.VERSION")
Expand All @@ -82,7 +79,7 @@ public void exportedWarJsBundleIsTheSameAsBrjsHosted() throws Exception {
@Test
public void exportedWarCssBundleIsTheSameAsBrjsHosted() throws Exception {
given(aspect).containsResourceFileWithContents("style.css", "body { color: red; }")
.and(sdkLibsDir).containsFile("locale-forwarder.js")
.and(brjs).localeForwarderHasContents("locale-forwarder.js")
.and(aspect).containsFileWithContents("index.html", "Hello World!")
.and(brjs).hasProdVersion("1234")
.and(app).hasBeenBuiltAsWar(brjs.dir())
Expand All @@ -95,7 +92,7 @@ public void exportedWarCssBundleIsTheSameAsBrjsHosted() throws Exception {

@Test
public void warCommandDoesntExportFilesFromAnotherAspect() throws Exception {
given(sdkLibsDir).containsFile("locale-forwarder.js")
given(brjs).localeForwarderHasContents("locale-forwarder.js")
.and(loginAspect).containsFileWithContents("index.html", "Hello World!")
.and(loginAspect).containsFileWithContents("themes/noir/images/file.gif", "** SOME GIF STUFF... **")
.and(brjs).hasProdVersion("1234")
Expand Down
Loading