Search in sources :

Example 6 with MediaType

use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.

the class HttpUtils method writeObject.

private void writeObject(HttpServletRequest request, HttpServletResponse response, MediaType contentType, Writable writable, EncodingExceptionHandler owserHandler) throws IOException, HTTPException {
    OutputStream out = null;
    response.setContentType(writable.getEncodedContentType().toString());
    try {
        out = response.getOutputStream();
        if (HTTPHeaders.supportsGzipEncoding(request) && writable.supportsGZip()) {
            out = new GZIPOutputStream(out);
            response.setHeader(HTTPHeaders.CONTENT_ENCODING, HTTPConstants.GZIP_ENCODING);
        }
        if (isCountingOutputStream) {
            out = new CountingOutputStream(out);
        }
        if (writable.hasForcedHttpStatus()) {
            response.setStatus(writable.getForcedHttpStatus().getCode());
        }
        writable.write(out, new ResponseProxy(response));
        out.flush();
    } catch (EncodingException e) {
        Object writeOwsExceptionReport = owserHandler.handleEncodingException(request, response, e);
        if (writeOwsExceptionReport != null) {
            Writable owserWritable = getWritable(writeOwsExceptionReport, contentType);
            try {
                owserWritable.write(out, new ResponseProxy(response));
                if (out != null) {
                    out.flush();
                }
            } catch (EncodingException ex) {
                throw new HTTPException(HTTPStatus.INTERNAL_SERVER_ERROR, ex);
            }
        }
    } finally {
        if (out instanceof CountingOutputStream) {
            Long bytesWritten = ((CountingOutputStream) out).getCount();
            eventBus.submit(new CountingOutputStreamEvent(bytesWritten));
        }
        if (out != null) {
            LOGGER.debug("Response status = " + response.getStatus());
            out.close();
        }
    }
}
Also used : ResponseProxy(org.n52.iceland.coding.encode.ResponseProxy) CountingOutputStream(com.google.common.io.CountingOutputStream) HTTPException(org.n52.iceland.exception.HTTPException) GZIPOutputStream(java.util.zip.GZIPOutputStream) EncodingException(org.n52.svalbard.encode.exception.EncodingException) CountingOutputStreamEvent(org.n52.iceland.event.events.CountingOutputStreamEvent) CountingOutputStream(com.google.common.io.CountingOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 7 with MediaType

use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.

the class MediaTypeTest method wildCard.

@Test
public void wildCard() {
    MediaType mt = MediaType.parse("*/*");
    errors.checkThat(mt.getType(), is("*"));
    errors.checkThat(mt.getSubtype(), is("*"));
    errors.checkThat(mt.isWildcard(), is(true));
    errors.checkThat(mt.isWildcardType(), is(true));
    errors.checkThat(mt.isWildcardSubtype(), is(true));
    errors.checkThat(mt.getParameters().size(), is(0));
}
Also used : MediaType(org.n52.janmayen.http.MediaType) Test(org.junit.Test)

Example 8 with MediaType

use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.

the class MediaTypeTest method applicationXmlWithQuotedParameterAndEqualSignWithin.

@Test
public void applicationXmlWithQuotedParameterAndEqualSignWithin() {
    MediaType mt = MediaType.parse(" application/xml; a=\"as = df\" ");
    errors.checkThat(mt.getType(), is("application"));
    errors.checkThat(mt.getSubtype(), is("xml"));
    errors.checkThat(mt.getParameters().size(), is(1));
    errors.checkThat(mt.getParameter("a").iterator().next(), is("as = df"));
}
Also used : MediaType(org.n52.janmayen.http.MediaType) Test(org.junit.Test)

Example 9 with MediaType

use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.

the class MediaTypeTest method wildCardSubtype.

@Test
public void wildCardSubtype() {
    MediaType mt = MediaType.parse("application/*");
    errors.checkThat(mt.getType(), is("application"));
    errors.checkThat(mt.getSubtype(), is("*"));
    errors.checkThat(mt.isWildcard(), is(false));
    errors.checkThat(mt.isWildcardType(), is(false));
    errors.checkThat(mt.isWildcardSubtype(), is(true));
    errors.checkThat(mt.getParameters().size(), is(0));
}
Also used : MediaType(org.n52.janmayen.http.MediaType) Test(org.junit.Test)

Example 10 with MediaType

use of org.n52.janmayen.http.MediaType in project arctic-sea by 52North.

the class MediaTypeTest method applicationXmlWithQuotedParameterAndSpacesWithin.

@Test
public void applicationXmlWithQuotedParameterAndSpacesWithin() {
    MediaType mt = MediaType.parse(" application/xml; a=\"as df\" ");
    errors.checkThat(mt.getType(), is("application"));
    errors.checkThat(mt.getSubtype(), is("xml"));
    errors.checkThat(mt.getParameters().size(), is(1));
    errors.checkThat(mt.getParameter("a").iterator().next(), is("as df"));
}
Also used : MediaType(org.n52.janmayen.http.MediaType) Test(org.junit.Test)

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