Skip to content

Commit

Permalink
Fix for Swagger apiKey.
Browse files Browse the repository at this point in the history
Added improved ServerResponse exception handling.
  • Loading branch information
noboomu committed May 22, 2017
1 parent b215afc commit 7d97753
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 93 deletions.
94 changes: 60 additions & 34 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.jsoniter.output.JsonContext;
import com.jsoniter.output.JsonStream;
Expand Down Expand Up @@ -48,8 +49,8 @@ public class ServerResponse<T>
protected boolean hasHeaders = false;
protected boolean hasIoCallback = false;
protected boolean processXml = false;
protected boolean processJson = false;
protected boolean preprocessed = false;
protected boolean processJson = false;
protected boolean preprocessed = false;

public ServerResponse()
{
Expand Down Expand Up @@ -129,14 +130,14 @@ public void setContentType(String contentType)

if (this.contentType.equals(javax.ws.rs.core.MediaType.APPLICATION_JSON))
{
if(!this.preprocessed)
if (!this.preprocessed)
{
this.processJson = true;
}
}
else if (this.contentType.equals(javax.ws.rs.core.MediaType.APPLICATION_XML))
{
if(!this.preprocessed)
if (!this.preprocessed)
{
this.processXml = true;
}
Expand All @@ -149,20 +150,19 @@ public ServerResponse<T> body(ByteBuffer body)
this.preprocessed = true;
return this;
}


public ServerResponse<T> body(String body)
{
return this.body( ByteBuffer.wrap(body.getBytes()) );
return this.body(ByteBuffer.wrap(body.getBytes()));
}

public ServerResponse<T> entity(T entity)
{
this.entity = entity;
this.preprocessed = false;

return this;
}
}

public ServerResponse<T> throwable(Throwable throwable)
{
Expand All @@ -173,7 +173,6 @@ public ServerResponse<T> throwable(Throwable throwable)
}
return this;
}


public ServerResponse<T> status(int status)
{
Expand All @@ -191,7 +190,7 @@ public ServerResponse<T> header(HttpString headerName, String value)
public ServerResponse<T> cookie(String cookieName, Cookie cookie)
{
this.cookies.put(cookieName, cookie);
this.hasCookies = true;
this.hasCookies = true;
return this;
}

Expand All @@ -203,9 +202,9 @@ public ServerResponse<T> contentType(String contentType)

public ServerResponse<T> applicationJson()
{
if(!this.preprocessed)
if (!this.preprocessed)
{
this.processJson = true;
this.processJson = true;
}
this.contentType = javax.ws.rs.core.MediaType.APPLICATION_JSON;
return this;
Expand All @@ -216,8 +215,7 @@ public ServerResponse<T> textHtml()
this.contentType = javax.ws.rs.core.MediaType.TEXT_HTML;
return this;
}



public ServerResponse<T> applicationOctetStream()
{
this.contentType = javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
Expand All @@ -226,11 +224,11 @@ public ServerResponse<T> applicationOctetStream()

public ServerResponse<T> applicationXml()
{
if(!this.preprocessed)
if (!this.preprocessed)
{
this.processXml = true;
}
this.contentType = javax.ws.rs.core.MediaType.APPLICATION_XML;
this.processXml = true;
}
this.contentType = javax.ws.rs.core.MediaType.APPLICATION_XML;
return this;
}

Expand All @@ -239,14 +237,13 @@ public ServerResponse<T> textPlain()
this.contentType = javax.ws.rs.core.MediaType.TEXT_PLAIN;
return this;
}

public ServerResponse<T> jsonContext(Class<? extends JsonContext> context)
{
this.jsonContext = context;
return this;
}


public ServerResponse<T> ok()
{
this.status = StatusCodes.OK;
Expand Down Expand Up @@ -308,17 +305,16 @@ public ServerResponse<T> withIoCallback(IoCallback ioCallback)
return this;
}


public void send( final HttpServerExchange exchange) throws RuntimeException
public void send(final HttpServerExchange exchange) throws RuntimeException
{
send(null,exchange);
send(null, exchange);
}

public void send(final HttpHandler handler, final HttpServerExchange exchange) throws RuntimeException
{
final boolean hasBody = this.body != null;
final boolean hasEntity = this.entity != null;

final boolean hasError = this.throwable != null;

if (this.hasHeaders)
{
Expand Down Expand Up @@ -359,6 +355,38 @@ else if (ServerPredicates.ACCEPT_XML_PREDICATE.resolve(exchange))
}
}

if (hasError)
{
Map<String, String> errorMap = new HashMap<>();

errorMap.put("message", throwable.getMessage());

if (throwable.getStackTrace() != null)
{
if (throwable.getStackTrace().length > 0)
{
errorMap.put("exceptionClass", throwable.getStackTrace()[0].getClassName());
}
}

if (this.processXml)
{
try
{
exchange.getResponseSender().send(ByteBuffer.wrap(XML_MAPPER.writeValueAsBytes(errorMap)));
} catch (JsonProcessingException e)
{
log.warn("Unable to create XML from error...");
}
}
else
{
exchange.getResponseSender().send(JsonStream.serializeToBytes(errorMap, this.jsonContext));
}

return;
}

if (hasBody)
{
if (!this.hasIoCallback)
Expand All @@ -380,8 +408,8 @@ else if (hasEntity)
}
else
{
exchange.getResponseSender().send(JsonStream.serializeToBytes(this.entity, this.jsonContext));

exchange.getResponseSender().send(JsonStream.serializeToBytes(this.entity, this.jsonContext));
}

} catch (Exception e)
Expand All @@ -408,28 +436,26 @@ public static <T> ServerResponse<T> response(Class<T> clazz)
{
return new ServerResponse<T>();
}
public static ServerResponse<ByteBuffer> response(ByteBuffer body)

public static ServerResponse<ByteBuffer> response(ByteBuffer body)
{
return new ServerResponse<ByteBuffer>().body(body);
}
public static ServerResponse<ByteBuffer> response(String body)

public static ServerResponse<ByteBuffer> response(String body)
{
return new ServerResponse<ByteBuffer>().body(body);
}

public static <T> ServerResponse<T> response(T entity)
{
return new ServerResponse<T>().entity(entity);
}

@SuppressWarnings("rawtypes")
public static ServerResponse response()
{
return new ServerResponse();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public HttpHandler wrap(final HttpHandler handler)
}
};

ApiKeyAuthDefinition keyAuthDefinition = new ApiKeyAuthDefinition(key, keyLocation);
ApiKeyAuthDefinition keyAuthDefinition = new ApiKeyAuthDefinition(name, keyLocation);
swagger.addSecurityDefinition(name, keyAuthDefinition);

registeredHandlerWrappers.put(name, wrapper);
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/main/resources/io/sinistral/proteus/swagger/swagger-ui.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ swagger {
{
apiKeys = [
{
name="defaultApiKey"
key="defaultApiKey"
in="HEADER",
key="default-api-key"
name="default-api-key"
value="123456789"
}
]
Expand Down

0 comments on commit 7d97753

Please sign in to comment.