use of org.openecard.control.binding.http.common.Http11Response in project open-ecard by ecsec.
the class HttpAppPluginActionHandler method createHTTPResponseFromBindingResult.
private HttpResponse createHTTPResponseFromBindingResult(BindingResult bindingResult) {
BindingResultCode resultCode = bindingResult.getResultCode();
LOG.debug("Recieved BindingResult with ResultCode {}", resultCode);
HttpResponse response;
switch(resultCode) {
case OK:
response = new Http11Response(HttpStatus.SC_OK);
break;
case REDIRECT:
response = new Http11Response(HttpStatus.SC_SEE_OTHER);
String location = bindingResult.getAuxResultData().get(AuxDataKeys.REDIRECT_LOCATION);
if (location != null && !location.isEmpty()) {
response.addHeader(HeaderTypes.LOCATION.fieldName(), location);
} else {
// redirect requires a location field
LOG.error("No redirect address available in given BindingResult instance.");
response = new Http11Response(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
break;
case WRONG_PARAMETER:
case MISSING_PARAMETER:
response = new Http11Response(HttpStatus.SC_BAD_REQUEST);
break;
case INTERNAL_ERROR:
response = new Http11Response(HttpStatus.SC_INTERNAL_SERVER_ERROR);
break;
case RESOURCE_UNAVAILABLE:
case DEPENDING_HOST_UNREACHABLE:
response = new Http11Response(HttpStatus.SC_NOT_FOUND);
break;
case RESOURCE_LOCKED:
response = new Http11Response(HttpStatus.SC_LOCKED);
break;
case TIMEOUT:
response = new Http11Response(HttpStatus.SC_GATEWAY_TIMEOUT);
break;
case TOO_MANY_REQUESTS:
// Code for TOO MANY REQUESTS is 429 according to RFC 6585
response = new Http11Response(429);
break;
default:
LOG.error("Untreated result code: " + resultCode);
response = new Http11Response(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
addHTTPEntity(response, bindingResult);
return response;
}
use of org.openecard.control.binding.http.common.Http11Response in project open-ecard by ecsec.
the class IndexHandler method handle.
@Override
public HttpResponse handle(HttpRequest httpRequest) throws HttpException, Exception {
HttpResponse httpResponse = new Http11Response(HttpStatus.SC_SEE_OTHER);
httpResponse.setHeader(HeaderTypes.LOCATION.fieldName(), "/index.html");
return httpResponse;
}
use of org.openecard.control.binding.http.common.Http11Response in project open-ecard by ecsec.
the class ControlCommonHandler method handle.
/**
* Handles a HTTP request.
*
* @param request HttpRequest
* @param response HttpResponse
* @param context HttpContext
* @throws HttpException
* @throws IOException
*/
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
_logger.debug("HTTP request: {}", request.toString());
HttpResponse httpResponse = null;
try {
// Forward request parameters to response parameters
response.setParams(request.getParams());
httpResponse = handle(request);
} catch (org.openecard.control.binding.http.HttpException e) {
httpResponse = new Http11Response(HttpStatus.SC_BAD_REQUEST);
httpResponse.setEntity(new StringEntity(e.getMessage(), "UTF-8"));
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
httpResponse.setEntity(new StringEntity(e.getMessage(), "UTF-8"));
}
httpResponse.setStatusCode(e.getHTTPStatusCode());
} catch (Exception e) {
httpResponse = new Http11Response(HttpStatus.SC_INTERNAL_SERVER_ERROR);
_logger.error(e.getMessage(), e);
} finally {
Http11Response.copyHttpResponse(httpResponse, response);
_logger.debug("HTTP response: {}", response);
_logger.debug("HTTP request handled by: {}", this.getClass().getName());
}
}
use of org.openecard.control.binding.http.common.Http11Response in project open-ecard by ecsec.
the class FileHandler method handle.
@Override
public HttpResponse handle(HttpRequest httpRequest) throws HttpException, Exception {
// Return 404 Not Found in the default case
Http11Response httpResponse = new Http11Response(HttpStatus.SC_NOT_FOUND);
RequestLine requestLine = httpRequest.getRequestLine();
if (requestLine.getMethod().equals("GET")) {
URI requestURI = URI.create(requestLine.getUri());
URL filePath = documentRoot.getFile(URLDecoder.decode(requestURI.getPath(), "UTF-8"));
if (filePath != null) {
// Handle file
_logger.debug("Handle file request");
handleFile(httpResponse, filePath);
} else {
_logger.debug("The DocumentRoot does not contain the URI: {}", requestURI.getPath());
}
} else {
// Return 405 Method Not Allowed
httpResponse.setStatusCode(HttpStatus.SC_METHOD_NOT_ALLOWED);
}
return httpResponse;
}
Aggregations