Skip to content

Commit

Permalink
Improve response and request error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Mar 5, 2018
1 parent bb84e83 commit cfc80d8
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/main/java/io/sinistral/proteus/ProteusApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.nio.file.Paths;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -46,6 +47,7 @@
import io.sinistral.proteus.server.handlers.HandlerGenerator;
import io.sinistral.proteus.server.handlers.ServerDefaultHttpHandler;
import io.sinistral.proteus.utilities.SecurityOps;
import io.sinistral.proteus.utilities.TablePrinter;
import io.undertow.Undertow;
import io.undertow.Undertow.ListenerInfo;
import io.undertow.UndertowOptions;
Expand Down Expand Up @@ -382,7 +384,21 @@ public void printStatus()
sb.append("\n\nUsing global headers: \n\n");
sb.append(globalHeadersParameters.entrySet().stream().map(e -> "\t" + e.getKey() + " = " + e.getValue()).collect(Collectors.joining("\n")));
sb.append("\n\nRegistered endpoints: \n\n");

List<String> tableHeaders = Arrays.asList("Method","Path","Consumes","Produces","Controller");

List<List<String>> tableRows = this.registeredEndpoints.stream().sorted().map( e -> {

return Arrays.asList( e.getMethod().toString(), e.getPathTemplate(), String.format("[%s]", e.getConsumes()), String.format("[%s]", e.getProduces()), String.format("(%s.%s)", e.getControllerName() , e.getControllerMethod() ) );

}).collect(Collectors.toList());

TablePrinter printer = new TablePrinter(tableHeaders, tableRows);

sb.append(printer.toString());

sb.append(this.registeredEndpoints.stream().sorted().map(EndpointInfo::toString).collect(Collectors.joining("\n")));

sb.append("\n\nRegistered services: \n\n");

ImmutableMultimap<State, Service> serviceStateMap = this.serviceManager.servicesByState();
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/io/sinistral/proteus/server/Extractors.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,13 @@ public static java.util.Optional<String> string(final HttpServerExchange exchan

public static java.util.Optional<Path> filePath(final HttpServerExchange exchange, final String name)
{
return java.util.Optional.ofNullable(exchange.getAttachment(FormDataParser.FORM_DATA).get(name)).map(Deque::getFirst).map( fv -> fv.getFile().toPath());
return java.util.Optional.ofNullable(exchange.getAttachment(FormDataParser.FORM_DATA).get(name)).map(Deque::getFirst).map( fv -> fv.getPath());
}

public static java.util.Optional<ByteBuffer> byteBuffer(final HttpServerExchange exchange, final String name)
{
return Optional.filePath(exchange,name).map( fp -> {



try(final FileChannel fileChannel = FileChannel.open(fp, StandardOpenOption.READ))
{
final ByteBuffer buffer = ByteBuffer.allocate((int)fileChannel.size());
Expand All @@ -219,11 +218,7 @@ public static java.util.Optional<ByteBuffer> byteBuffer(final HttpServerExchang
{
return null;
}

});



}
}

Expand All @@ -233,9 +228,7 @@ public static String string(final HttpServerExchange exchange, final String nam
{
try
{

return exchange.getRequestHeaders().get(name).getFirst();

} catch(NullPointerException e)
{
throw new IllegalArgumentException("Missing parameter " + name, e);
Expand Down Expand Up @@ -468,6 +461,10 @@ else if( a instanceof javax.ws.rs.OPTIONS)
{
return Methods.OPTIONS;
}
else if( a instanceof javax.ws.rs.HEAD)
{
return Methods.HEAD;
}

else
{
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/sinistral/proteus/server/ServerRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ private void extractBytes() throws IOException
final StreamSourceChannel channel = this.exchange.getRequestChannel();

while (true) {

buf.clear();

buf.clear();

int c = channel.read(buf);

if (c == -1) {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,26 @@ public ServerResponse<T> badRequest()
this.status = StatusCodes.BAD_REQUEST;
return this;
}

public ServerResponse<T> badRequest(Throwable t)
{
this.status = StatusCodes.BAD_REQUEST;
this.throwable = t;
return this;
}

public ServerResponse<T> internalServerError()
{
this.status = StatusCodes.INTERNAL_SERVER_ERROR;
return this;
}

public ServerResponse<T> internalServerError(Throwable t)
{
this.status = StatusCodes.INTERNAL_SERVER_ERROR;
this.throwable = t;
return this;
}

public ServerResponse<T> created()
{
Expand All @@ -280,12 +294,26 @@ public ServerResponse<T> notFound()
this.status = StatusCodes.NOT_FOUND;
return this;
}

public ServerResponse<T> notFound(Throwable t)
{
this.status = StatusCodes.NOT_FOUND;
this.throwable = t;
return this;
}

public ServerResponse<T> forbidden()
{
this.status = StatusCodes.FORBIDDEN;
return this;
}

public ServerResponse<T> forbidden(Throwable t)
{
this.status = StatusCodes.FORBIDDEN;
this.throwable = t;
return this;
}

public ServerResponse<T> found()
{
Expand All @@ -298,6 +326,13 @@ public ServerResponse<T> noContent()
this.status = StatusCodes.NO_CONTENT;
return this;
}

public ServerResponse<T> noContent(Throwable t)
{
this.status = StatusCodes.NO_CONTENT;
this.throwable = t;
return this;
}

public ServerResponse<T> withIoCallback(IoCallback ioCallback)
{
Expand Down
127 changes: 127 additions & 0 deletions src/main/java/io/sinistral/proteus/utilities/TablePrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
*
*/
package io.sinistral.proteus.utilities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author jbauer
*/
public class TablePrinter
{
private final int TABLEPADDING = 4;


private List<String> headers;
private List<List<String>> table;
private List<Integer> maxLength;

public TablePrinter(List<String> headersIn, List<List<String>> content)
{
this.headers = headersIn;
this.maxLength = new ArrayList<Integer>();
for (int i = 0; i < headers.size(); i++)
{
maxLength.add(headers.get(i).length());
}
this.table = content;

updateMaxLengths();
}

public void updateField(int row, int col, String input)
{
table.get(row).set(col, input);
updateMaxColumnLength(col);
}


public String toString()
{
StringBuilder sb = new StringBuilder();
StringBuilder rowSeparatorBuilder = new StringBuilder();
String padder = "";
String rowSeperator = "";

for (int i = 0; i < 4; i++)
{
padder += " ";
}

for (int i = 0; i < maxLength.size(); i++)
{
for (int j = 0; j < maxLength.get(i) + (TABLEPADDING * 2); j++)
{
rowSeparatorBuilder.append("-");
}
}

rowSeperator = rowSeparatorBuilder.toString();

sb.append("\n");

for (int i = 0; i < headers.size(); i++)
{
sb.append(padder);
sb.append(headers.get(i));
for (int k = 0; k < (maxLength.get(i) - headers.get(i).length()); k++)
{
sb.append(" ");
}
sb.append(padder);
}

sb.append("\n");
sb.append(rowSeperator);
sb.append("\n");

for (int i = 0; i < table.size(); i++)
{
List<String> tempRow = table.get(i);
for (int j = 0; j < tempRow.size(); j++)
{
sb.append(padder);
sb.append(tempRow.get(j));
for (int k = 0; k < (maxLength.get(j) - tempRow.get(j).length()); k++)
{
sb.append(" ");
}
sb.append(padder);
}
sb.append("\n");
}

return sb.toString();
}

private void updateMaxLengths()
{
for (int i = 0; i < table.size(); i++)
{
List<String> temp = table.get(i);
for (int j = 0; j < temp.size(); j++)
{
if (temp.get(j).length() > maxLength.get(j))
{
maxLength.set(j, temp.get(j).length());
}
}
}
}

private void updateMaxColumnLength(int col)
{
for (int i = 0; i < table.size(); i++)
{
if (table.get(i).get(col).length() > maxLength.get(col))
{
maxLength.set(col, table.get(i).get(col).length());
}
}
}

}

0 comments on commit cfc80d8

Please sign in to comment.