Search in sources :

Example 1 with MediaType

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();
    }
}
Also used : MediaType(org.n52.janmayen.http.MediaType)

Example 2 with MediaType

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);
    }
}
Also used : ExceptionEvent(org.n52.iceland.event.events.ExceptionEvent) HTTPException(org.n52.iceland.exception.HTTPException) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) OwsEncodingException(org.n52.iceland.coding.encode.OwsEncodingException) MediaType(org.n52.janmayen.http.MediaType) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport)

Example 3 with MediaType

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);
    }
}
Also used : MediaType(org.n52.janmayen.http.MediaType)

Example 4 with MediaType

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;
}
Also used : MediaType(org.n52.janmayen.http.MediaType)

Example 5 with MediaType

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);
}
Also used : Binding(org.n52.iceland.binding.Binding) HTTPException(org.n52.iceland.exception.HTTPException) MediaType(org.n52.janmayen.http.MediaType) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException)

Aggregations

MediaType (org.n52.janmayen.http.MediaType)28 Test (org.junit.Test)21 HTTPException (org.n52.iceland.exception.HTTPException)4 EncodingException (org.n52.svalbard.encode.exception.EncodingException)3 OwsEncodingException (org.n52.iceland.coding.encode.OwsEncodingException)2 ExceptionEvent (org.n52.iceland.event.events.ExceptionEvent)2 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)2 OwsExceptionReport (org.n52.shetland.ogc.ows.exception.OwsExceptionReport)2 CountingOutputStream (com.google.common.io.CountingOutputStream)1 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 DescribeSensor (net.opengis.sos.x10.DescribeSensorDocument.DescribeSensor)1 Binding (org.n52.iceland.binding.Binding)1 ResponseProxy (org.n52.iceland.coding.encode.ResponseProxy)1