Search in sources :

Example 1 with CountingOutputStreamEvent

use of org.n52.iceland.event.events.CountingOutputStreamEvent in project arctic-sea by 52North.

the class AbstractStatisticsServiceEventListener method addEventToResolver.

private void addEventToResolver(BatchResolver resolver, Event event) {
    StatisticsServiceEventResolver<?> evtResolver = null;
    if (event instanceof ExceptionEvent) {
        ExceptionEventResolver sosExceptionEventResolver = resolverFactory.getExceptionEventResolver();
        sosExceptionEventResolver.setEvent((ExceptionEvent) event);
        evtResolver = sosExceptionEventResolver;
    } else if (event instanceof OutgoingResponseEvent) {
        OutgoingResponseEventResolver outgoingResponseEventResolver = resolverFactory.getOutgoingResponseEventResolver();
        outgoingResponseEventResolver.setEvent((OutgoingResponseEvent) event);
        evtResolver = outgoingResponseEventResolver;
    } else if (event instanceof CountingOutputStreamEvent) {
        CountingOutputStreamEventResolver countingOutputstreamEventResolver = resolverFactory.getCountingOutputstreamEventResolver();
        countingOutputstreamEventResolver.setEvent((CountingOutputStreamEvent) event);
        evtResolver = countingOutputstreamEventResolver;
    } else {
        evtResolver = findResolver(event);
    }
    // Default fallback event resolver
    if (evtResolver == null) {
        DefaultServiceEventResolver defaultServiceEventResolver = resolverFactory.getDefaultServiceEventResolver();
        defaultServiceEventResolver.setEvent(event);
        evtResolver = defaultServiceEventResolver;
    }
    resolver.addEventResolver(evtResolver);
}
Also used : ExceptionEvent(org.n52.iceland.event.events.ExceptionEvent) CountingOutputStreamEventResolver(org.n52.iceland.statistics.impl.resolvers.CountingOutputStreamEventResolver) CountingOutputStreamEvent(org.n52.iceland.event.events.CountingOutputStreamEvent) DefaultServiceEventResolver(org.n52.iceland.statistics.impl.resolvers.DefaultServiceEventResolver) OutgoingResponseEventResolver(org.n52.iceland.statistics.impl.resolvers.OutgoingResponseEventResolver) ExceptionEventResolver(org.n52.iceland.statistics.impl.resolvers.ExceptionEventResolver) OutgoingResponseEvent(org.n52.iceland.event.events.OutgoingResponseEvent)

Example 2 with CountingOutputStreamEvent

use of org.n52.iceland.event.events.CountingOutputStreamEvent 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 3 with CountingOutputStreamEvent

use of org.n52.iceland.event.events.CountingOutputStreamEvent in project arctic-sea by 52North.

the class EventHandlerFinderTest method findDirectDefaultServiceEvent.

@Test
public void findDirectDefaultServiceEvent() {
    Map<String, StatisticsServiceEventHandler<?>> handlers = new HashMap<>();
    CountingOutputStreamEventHandler handler = new CountingOutputStreamEventHandler();
    CountingOutputStreamEvent request = new CountingOutputStreamEvent();
    handlers.put(request.getClass().getSimpleName(), handler);
    Assert.assertNotNull(EventHandlerFinder.findHandler(request, handlers));
}
Also used : HashMap(java.util.HashMap) CountingOutputStreamEvent(org.n52.iceland.event.events.CountingOutputStreamEvent) CountingOutputStreamEventHandler(org.n52.iceland.statistics.impl.handlers.CountingOutputStreamEventHandler) StatisticsServiceEventHandler(org.n52.iceland.statistics.api.interfaces.StatisticsServiceEventHandler) Test(org.junit.Test)

Aggregations

CountingOutputStreamEvent (org.n52.iceland.event.events.CountingOutputStreamEvent)3 CountingOutputStream (com.google.common.io.CountingOutputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Test (org.junit.Test)1 ResponseProxy (org.n52.iceland.coding.encode.ResponseProxy)1 ExceptionEvent (org.n52.iceland.event.events.ExceptionEvent)1 OutgoingResponseEvent (org.n52.iceland.event.events.OutgoingResponseEvent)1 HTTPException (org.n52.iceland.exception.HTTPException)1 StatisticsServiceEventHandler (org.n52.iceland.statistics.api.interfaces.StatisticsServiceEventHandler)1 CountingOutputStreamEventHandler (org.n52.iceland.statistics.impl.handlers.CountingOutputStreamEventHandler)1 CountingOutputStreamEventResolver (org.n52.iceland.statistics.impl.resolvers.CountingOutputStreamEventResolver)1 DefaultServiceEventResolver (org.n52.iceland.statistics.impl.resolvers.DefaultServiceEventResolver)1 ExceptionEventResolver (org.n52.iceland.statistics.impl.resolvers.ExceptionEventResolver)1 OutgoingResponseEventResolver (org.n52.iceland.statistics.impl.resolvers.OutgoingResponseEventResolver)1 EncodingException (org.n52.svalbard.encode.exception.EncodingException)1