use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.
the class SimpleBinding method writeResponse.
protected void writeResponse(HttpServletRequest request, HttpServletResponse response, OwsServiceResponse serviceResponse) throws HTTPException, IOException {
try {
MediaType contentType = chooseResponseContentType(serviceResponse, HTTPHeaders.getAcceptHeader(request), getDefaultContentType());
if (!serviceResponse.isSetContentType()) {
serviceResponse.setContentType(contentType);
}
httpUtils.writeObject(request, response, contentType, serviceResponse, this);
} finally {
serviceResponse.close();
}
}
use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.
the class SimpleBinding method handleEncodingException.
@Override
public Object handleEncodingException(HttpServletRequest request, HttpServletResponse response, EncodingException ex) throws HTTPException {
try {
OwsExceptionReport oer;
if (ex instanceof OwsEncodingException) {
oer = ((OwsEncodingException) ex).getCause();
} else if (ex.getCause() instanceof OwsExceptionReport) {
oer = (OwsExceptionReport) ex.getCause();
} else {
oer = new NoApplicableCodeException().withMessage(ex.getMessage()).causedBy(ex);
}
eventBus.submit(new ExceptionEvent(oer));
MediaType contentType = chooseResponseContentTypeForExceptionReport(HTTPHeaders.getAcceptHeader(request), getDefaultContentType());
Object encoded = encodeOwsExceptionReport(oer, contentType);
if (isUseHttpResponseCodes() && oer.hasStatus()) {
response.setStatus(oer.getStatus().getCode());
}
return encoded;
} catch (OwsExceptionReport e) {
throw new HTTPException(HTTPStatus.INTERNAL_SERVER_ERROR, e);
}
}
use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.
the class SoapBinding method writeResponse.
private void writeResponse(SoapChain chain) throws IOException, HTTPException {
MediaType contentType = chooseResponseContentType(chain.getBodyResponse(), HTTPHeaders.getAcceptHeader(chain.getHttpRequest()), getDefaultContentType());
// TODO allow other bindings to encode response as soap messages
if (contentType.isCompatible(getDefaultContentType())) {
checkSoapInjection(chain);
httpUtils.writeObject(chain.getHttpRequest(), chain.getHttpResponse(), checkMediaType(chain), chain, this);
} else {
httpUtils.writeObject(chain.getHttpRequest(), chain.getHttpResponse(), contentType, chain.getBodyResponse(), this);
}
}
use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.
the class ResponseWriter method getEncodedContentType.
default MediaType getEncodedContentType(ResponseFormat responseFormat) {
MediaType defaultContentType = getContentType();
if (responseFormat.isSetResponseFormat()) {
try {
String rf = responseFormat.getResponseFormat();
MediaType mt = MediaType.parse(rf).withoutParameters();
if (MediaTypes.COMPATIBLE_TYPES.containsEntry(mt, defaultContentType)) {
return defaultContentType;
}
return mt;
} catch (IllegalArgumentException iae) {
// ignore
}
}
return defaultContentType;
}
use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.
the class Service method getBinding.
/**
* Get the implementation of {@link Binding} that is registered for the given <code>request</code>.
*
* @param request URL pattern from request URL
*
* @return The implementation of {@link Binding} that is registered for the given <code>urlPattern</code>.
*
* @throws HTTPException If the URL pattern or ContentType is not supported by this service.
*/
private Binding getBinding(HttpServletRequest request) throws HTTPException {
final String requestURI = request.getPathInfo();
if (requestURI == null || requestURI.isEmpty() || requestURI.equals("/")) {
MediaType contentType = getContentType(request);
// strip of the parameters to get rid of things like encoding
Binding binding = this.bindingRepository.getBinding(contentType.withoutParameters());
if (binding == null) {
if (contentType.equals(MediaTypes.APPLICATION_KVP)) {
throw new HTTPException(HTTPStatus.METHOD_NOT_ALLOWED);
} else {
throw new HTTPException(HTTPStatus.UNSUPPORTED_MEDIA_TYPE);
}
} else {
if (requestTimeout > 0) {
try {
return TIME_LIMITER.newProxy(binding, Binding.class, requestTimeout, TimeUnit.SECONDS);
} catch (UncheckedTimeoutException ute) {
HTTPException httpException = new HTTPException(HTTPStatus.GATEWAY_TIME_OUT);
httpException.addSuppressed(ute);
throw httpException;
}
}
return binding;
}
}
throw new HTTPException(HTTPStatus.NOT_FOUND);
}
Aggregations