Skip to content

Commit

Permalink
few tiny code changes; #42
Browse files Browse the repository at this point in the history
  • Loading branch information
bbilger committed Nov 16, 2017
1 parent ca1bc17 commit eb51d9d
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
Expand Down Expand Up @@ -53,8 +55,9 @@ public abstract class FnRequestHandler extends

private static final Type INPUT_EVENT_TYPE = (new GenericType<Ref<InputEvent>>() { }).getType();
private static final Type RUNTIME_CONTEXT_TYPE = (new GenericType<Ref<RuntimeContext>>() { }).getType();
private static final String DEFAULT_CONTENT_TYPE = MediaType.APPLICATION_JSON;

private RuntimeContext rctx;
private String defaultContentType = MediaType.APPLICATION_JSON;


/**
Expand Down Expand Up @@ -121,8 +124,8 @@ private String formatKey(String key) {
*/
@Override
protected void extendActualJerseyContainerRequest(ContainerRequest actualContainerRequest,
JRestlessContainerRequest containerRequest,
WrappedInput wrappedInput) {
JRestlessContainerRequest containerRequest,
WrappedInput wrappedInput) {
InputEvent event = wrappedInput.inputEvent;
actualContainerRequest.setRequestScopedInitializer(locator -> {
Ref<InputEvent> inputEventRef = locator
Expand Down Expand Up @@ -203,14 +206,14 @@ public ByteArrayOutputStream getEntityOutputStream() {

@Override
public void writeResponse(Response.StatusType statusType,
Map<String, List<String>> headers,
OutputStream outputStream)
Map<String, List<String>> headers,
OutputStream outputStream)
throws IOException {
// NOTE: This is a safe cast as it is set to a ByteArrayOutputStream by getEntityOutputStream
// See JRestlessHandlerContainer class for more details
String responseBody = ((ByteArrayOutputStream) outputStream).toString(StandardCharsets.UTF_8.name());
String contentType = headers
.getOrDefault("Content-Type", Collections.singletonList(defaultContentType))
.getOrDefault(HttpHeaders.CONTENT_TYPE, Collections.singletonList(DEFAULT_CONTENT_TYPE))
.get(0);
Map<String, String> outHeaders = new HashMap<>();
headers.forEach((k, v) -> outHeaders.put(k, v.stream().collect(Collectors.joining(","))));
Expand All @@ -230,8 +233,8 @@ public void writeResponse(Response.StatusType statusType,
*/
@Override
protected WrappedOutput onRequestFailure(Exception e,
WrappedInput wrappedInput,
@Nullable JRestlessContainerRequest jRestlessContainerRequest) {
WrappedInput wrappedInput,
@Nullable JRestlessContainerRequest jRestlessContainerRequest) {
LOG.error("Request failed", e);

OutputEvent outputEvent = OutputEvent.emptyResult(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
Expand Down Expand Up @@ -259,7 +262,7 @@ public void setRuntimeContext(RuntimeContext rctx) {
* @return The output event to the oracle functions platform
*/
public final OutputEvent handleRequest(InputEvent inputEvent) {
return inputEvent.consumeBody((inputStream) -> {
return inputEvent.consumeBody(inputStream -> {
WrappedInput wrappedInput = new WrappedInput(inputEvent, inputStream);
WrappedOutput response = this.delegateRequest(wrappedInput);
return response.outputEvent;
Expand All @@ -281,7 +284,7 @@ public static class WrappedOutput {
private final String body;
private final int statusCode;

public WrappedOutput(OutputEvent outputEvent, @Nullable String body, Response.StatusType statusType) {
public WrappedOutput(OutputEvent outputEvent, @Nullable String body, @Nonnull Response.StatusType statusType) {
requireNonNull(statusType);
this.statusCode = statusType.getStatusCode();
this.body = body;
Expand Down

0 comments on commit eb51d9d

Please sign in to comment.