Skip to content

Commit

Permalink
mime types is finally in place to automatically look up by extension …
Browse files Browse the repository at this point in the history
…and send back AND we encode to what the mime type file would tell us to encode to as well along with sending the mime type
  • Loading branch information
deanhiller committed Aug 28, 2016
1 parent 05006f2 commit 50f1f08
Show file tree
Hide file tree
Showing 31 changed files with 795 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ else if(list.size() == 0)
}

public List<Header> getHeaders(String key) {
return headers.get(key);
return headers.get(key.toLowerCase());
}

/**
Expand All @@ -51,7 +51,7 @@ public List<Header> getHeaders(String key) {
* user would think that it would be marshalled out
*/
protected void addHeader(Header header) {
List<Header> list = headers.get(header.getName());
List<Header> list = headers.get(header.getName().toLowerCase());
if(list == null) {
list = new ArrayList<>();
//Header names are not case sensitive while values are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void configure(Binder binder) {
@Provides
@Singleton
public CompileOnDemand provideCompile(RouterConfig routerConfig) {
if(routerConfig.getOverridesModule() != null)
if(routerConfig.getWebappOverrides() != null)
throw new IllegalArgumentException("In DEVELOPMENT server mode, when you provide"
+ " HttpRouterConfig, you cannot have a app OverridesModule set as "
+ "this would be a classloader headache foor you. Check our tests to see for another possible method ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<head>
</head>
<body>
Development. Your app's webpage was not found for this reason: TBD ${error}$

You are in Development Server. Your request received a 404. Your app's webpage was not found for
this reason: TBD ${error}$
<br/>
<br/>
Fill in the module we ended up using and the routes in that module that did not match here...
<br/>
<br/>
Fill in iframe with app's notFound page here as well...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.webpieces.router.api.actions.Action;
import org.webpieces.router.api.actions.Actions;
import org.webpieces.router.api.actions.Redirect;
import org.webpieces.router.api.actions.RenderHtml;
import org.webpieces.router.api.actions.Render;
import org.webpieces.router.api.routing.Param;

public class MeetingController {
Expand All @@ -19,11 +19,11 @@ public class MeetingController {
@Inject
private SomeService service;

public RenderHtml notFound() {
public Render notFound() {
return Actions.renderThis();
}

public RenderHtml internalError() {
public Render internalError() {
return Actions.renderThis();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class RouterConfig {

private Charset fileEncoding = StandardCharsets.UTF_8;
private Charset urlEncoding = StandardCharsets.UTF_8;
private Charset defaultResponseBodyEncoding = StandardCharsets.UTF_8;

private String secretKey;

Expand All @@ -29,9 +30,6 @@ public class RouterConfig {
public VirtualFile getMetaFile() {
return metaFile;
}
public Module getOverridesModule() {
return webappOverrides;
}
public RouterConfig setMetaFile(VirtualFile routersFile) {
if(!routersFile.exists())
throw new IllegalArgumentException("path="+routersFile+" does not exist");
Expand All @@ -40,34 +38,47 @@ else if(routersFile.isDirectory())
this.metaFile = routersFile;
return this;
}

public Module getWebappOverrides() {
return webappOverrides;
}
public RouterConfig setWebappOverrides(Module webappOverrides) {
this.webappOverrides = webappOverrides;
return this;
}

public Charset getFileEncoding() {
return fileEncoding;
}
public RouterConfig setFileEncoding(Charset fileEncoding) {
this.fileEncoding = fileEncoding;
return this;
}

public Charset getUrlEncoding() {
return urlEncoding;
}
public RouterConfig setUrlEncoding(Charset urlEncoding) {
this.urlEncoding = urlEncoding;
return this;
}

public boolean getIsCookiesHttpOnly() {
return isCookiesHttpOnly;
}
public boolean getIsCookiesSecure() {
return isCookiesSecure;
}
public RouterConfig setCookiesHttpOnly(boolean isCookiesHttpOnly) {
this.isCookiesHttpOnly = isCookiesHttpOnly;
return this;
}

public boolean getIsCookiesSecure() {
return isCookiesSecure;
}
public RouterConfig setCookiesSecure(boolean isCookiesSecure) {
this.isCookiesSecure = isCookiesSecure;
return this;
}

public String getSecretKey() {
return secretKey;
}
Expand All @@ -76,5 +87,12 @@ public RouterConfig setSecretKey(String secretKey) {
return this;
}


public Charset getDefaultResponseBodyEncoding() {
return defaultResponseBodyEncoding;
}
public RouterConfig setDefaultResponseBodyEncoding(Charset defaultResponseBodyEncoding) {
this.defaultResponseBodyEncoding = defaultResponseBodyEncoding;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.webpieces.ctx.api.RequestContext;
import org.webpieces.router.api.routing.RouteId;
import org.webpieces.router.impl.actions.RedirectImpl;
import org.webpieces.router.impl.actions.RenderHtmlImpl;
import org.webpieces.router.impl.actions.RenderImpl;
import org.webpieces.router.impl.ctx.RequestLocalCtx;
import org.webpieces.router.impl.ctx.ResponseProcessor;

Expand All @@ -21,8 +21,8 @@ public class Actions {
* @param pageArgs
* @return
*/
public static RenderHtml renderView(String templatePath, Object ... pageArgs) {
RenderHtmlImpl renderHtml = new RenderHtmlImpl(templatePath, pageArgs);
public static Render renderView(String templatePath, Object ... pageArgs) {
RenderImpl renderHtml = new RenderImpl(templatePath, pageArgs);
ResponseProcessor processor = RequestLocalCtx.get();
if(processor != null) {
//If there is a context (the controller method is synchronous), then we do all calculations on their thread so if there
Expand All @@ -38,7 +38,7 @@ public static RenderHtml renderView(String templatePath, Object ... pageArgs) {
* @param pageArgs
* @return
*/
public static RenderHtml renderThis(Object ... pageArgs) {
public static Render renderThis(Object ... pageArgs) {
return renderView(null, pageArgs);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.webpieces.router.api.actions;

public interface RenderHtml extends Action {
public interface Render extends Action {


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.util.Map;

import org.webpieces.router.api.actions.RenderHtml;
import org.webpieces.router.api.actions.Render;

public class RenderResponse implements RenderHtml {
public class RenderResponse implements Render {

public View view;
public RouteType routeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.webpieces.router.api.exceptions.BadRequestException;
import org.webpieces.router.api.exceptions.NotFoundException;
import org.webpieces.router.impl.actions.RedirectImpl;
import org.webpieces.router.impl.actions.RenderHtmlImpl;
import org.webpieces.router.impl.actions.RenderImpl;
import org.webpieces.router.impl.ctx.RequestLocalCtx;
import org.webpieces.router.impl.ctx.ResponseProcessor;
import org.webpieces.router.impl.ctx.SessionImpl;
Expand Down Expand Up @@ -200,8 +200,8 @@ public Void continueProcessing(ResponseProcessor processor, Object controllerRes
//client code ends up in the stack trace making it easier to tell what the path was to failure
} else if(controllerResponse instanceof RedirectImpl) {
processor.createFullRedirect((RedirectImpl)controllerResponse);
} else if(controllerResponse instanceof RenderHtmlImpl) {
processor.createRenderResponse((RenderHtmlImpl)controllerResponse);
} else if(controllerResponse instanceof RenderImpl) {
processor.createRenderResponse((RenderImpl)controllerResponse);
} else {
throw new UnsupportedOperationException("Not yet done but could "
+ "call into the Action witht the responseCb to handle so apps can decide what to send back");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public Injector createInjector(WebAppMeta routerModule) {
List<Module> guiceModules = routerModule.getGuiceModules();

Module module = Modules.combine(guiceModules);
if(config.getOverridesModule() != null)
module = Modules.override(module).with(config.getOverridesModule());
if(config.getWebappOverrides() != null)
module = Modules.override(module).with(config.getWebappOverrides());

Injector injector = Guice.createInjector(module);
return injector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import java.util.HashMap;
import java.util.Map;

import org.webpieces.router.api.actions.RenderHtml;
import org.webpieces.router.api.actions.Render;

public class RenderHtmlImpl implements RenderHtml {
public class RenderImpl implements Render {

private String relativeOrAbsoluteView;
private Map<String, Object> pageArgs = new HashMap<>();

public RenderHtmlImpl(String view, Object ... pageArgs) {
public RenderImpl(String view, Object ... pageArgs) {
this.relativeOrAbsoluteView = view;
if(pageArgs.length % 2 != 0)
throw new IllegalArgumentException("All arguments to render must be even with String, Object, String, Object (ie. key, value, key, value)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.webpieces.router.impl.Route;
import org.webpieces.router.impl.RouteMeta;
import org.webpieces.router.impl.actions.RedirectImpl;
import org.webpieces.router.impl.actions.RenderHtmlImpl;
import org.webpieces.router.impl.actions.RenderImpl;
import org.webpieces.router.impl.params.ObjectToParamTranslator;

public class ResponseProcessor {
Expand Down Expand Up @@ -85,7 +85,7 @@ else if(!nextRequestMeta.getRoute().matchesMethod(HttpMethod.GET))
return redirectResponse;
}

public RenderResponse createRenderResponse(RenderHtmlImpl controllerResponse) {
public RenderResponse createRenderResponse(RenderImpl controllerResponse) {
if(responseSent)
throw new IllegalStateException("You already sent a response. do not call Actions.redirect or Actions.render more than once");
responseSent = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static class HtmlCharacterEntityReferences {
static final String HEX_REFERENCE_START = "&#x";
static final char REFERENCE_END = ';';
static final char CHAR_NULL = (char) -1;
private static final String PROPERTIES_FILE = "htmlentities.properties";
private static final String PROPERTIES_FILE = "/htmlentities.properties";
private final String[] characterToEntityReferenceMap = new String[3000];
private final Map<String, Character> entityReferenceToCharacterMap = new HashMap<String, Character>(252);

Expand All @@ -89,7 +89,7 @@ public HtmlCharacterEntityReferences() {
InputStream is = HtmlCharacterEntityReferences.class.getResourceAsStream(PROPERTIES_FILE);
if (is == null) {
throw new IllegalStateException(
"Cannot find reference definition file [htmlentities.properties] as class path resource");
"Cannot find reference definition file ["+PROPERTIES_FILE+"] as class path resource");
}
try {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ public void assertNotContains(String text) {
throw new IllegalStateException("Expected body to NOT contain='"+text+"' but body was="+bodyAsString);
}

public void assertContentType(String mimeType) {
Header type = getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_TYPE);
String value = type.getValue();
if(!mimeType.equals(value))
throw new IllegalStateException("Expected mimeType="+mimeType+" but found type="+value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.webpieces.frontend.api.FrontendSocket;
import org.webpieces.httpparser.api.common.KnownHeaderName;
import org.webpieces.httpparser.api.dto.HttpPayload;
import org.webpieces.httpparser.api.dto.HttpResponse;
import org.webpieces.nio.api.channels.Channel;
Expand All @@ -15,38 +16,41 @@ public class MockFrontendSocket implements FrontendSocket {

private static final Logger log = LoggerFactory.getLogger(MockFrontendSocket.class);
private List<FullResponse> payloads = new ArrayList<>();
private boolean waitingResponseStart = true;
private FullResponse chunkedResponse;

@Override
public CompletableFuture<FrontendSocket> close() {
return null;
}

@Override
public CompletableFuture<FrontendSocket> write(HttpPayload payload) {
if(waitingResponseStart) {
public synchronized CompletableFuture<FrontendSocket> write(HttpPayload payload) {
if(chunkedResponse == null) {
HttpResponse response = payload.getHttpResponse();
if(response == null) {
log.warn("should be receiving http response but received="+payload);
return null;
}
payloads.add(new FullResponse(response));
waitingResponseStart = false;
FullResponse nextResp = new FullResponse(response);
if(response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH) != null)
payloads.add(nextResp);
else
chunkedResponse = nextResp;

return null;
} else if(payloads.size() == 0) {
log.error("Should get HttpResponse first but instead received something else="+payload);
return null;
}

FullResponse fullResponse = payloads.get(payloads.size()-1);

switch (payload.getMessageType()) {
case CHUNK:
fullResponse.addChunk(payload.getHttpChunk());
chunkedResponse.addChunk(payload.getHttpChunk());
break;
case LAST_CHUNK:
fullResponse.setLastChunk(payload.getLastHttpChunk());
waitingResponseStart = true;
chunkedResponse.setLastChunk(payload.getLastHttpChunk());
payloads.add(chunkedResponse);
chunkedResponse = null;
break;
default:
log.error("expecting chunk but received payload="+payload);
Expand All @@ -65,9 +69,29 @@ public List<FullResponse> getResponses() {
return payloads;
}

public synchronized List<FullResponse> getResponses(long waitTimeMs, int count) {
try {
return getResponsesImpl(waitTimeMs, count);
} catch (InterruptedException e) {
throw new RuntimeException("failed waiting", e);
}
}

public synchronized List<FullResponse> getResponsesImpl(long waitTimeMs, int count) throws InterruptedException {
long start = System.currentTimeMillis();
while(payloads.size() < count) {
this.wait(waitTimeMs+500);
long time = System.currentTimeMillis() - start;
if(time > waitTimeMs)
throw new IllegalStateException("While waiting for "+count+" responses, some or all never came. count that came="+payloads.size());
}

return payloads;
}

public void clear() {
payloads.clear();
waitingResponseStart = true;
chunkedResponse = null;
}

}
2 changes: 1 addition & 1 deletion webserver/http-webserver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sourceSets {
extraTest {
resources {
srcDirs = ["src/test/java"]
includes = ["**/*.html", "**/*.tag", "**/*.properties"]
includes = ["**/*.html", "**/*.tag", "**/*.properties", "**/*.json"]
}
}
test {
Expand Down
Loading

0 comments on commit 50f1f08

Please sign in to comment.