Search in sources :

Example 1 with InvalidPathException

use of org.apache.cxf.transport.websocket.InvalidPathException in project cxf by apache.

the class DefaultProtocolInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    LOG.log(Level.FINE, "inspect");
    if (AtmosphereResource.TRANSPORT.WEBSOCKET != r.transport() && AtmosphereResource.TRANSPORT.SSE != r.transport() && AtmosphereResource.TRANSPORT.POLLING != r.transport()) {
        LOG.fine("Skipping ignorable request");
        return Action.CONTINUE;
    }
    if (AtmosphereResource.TRANSPORT.POLLING == r.transport()) {
        final String saruuid = (String) r.getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
        final AtmosphereResponse suspendedResponse = suspendedResponses.get(saruuid);
        LOG.fine("Attaching a proxy writer to suspended response");
        r.getResponse().asyncIOWriter(new AtmosphereInterceptorWriter() {

            @Override
            public AsyncIOWriter write(AtmosphereResponse r, String data) throws IOException {
                suspendedResponse.write(data);
                suspendedResponse.flushBuffer();
                return this;
            }

            @Override
            public AsyncIOWriter write(AtmosphereResponse r, byte[] data) throws IOException {
                suspendedResponse.write(data);
                suspendedResponse.flushBuffer();
                return this;
            }

            @Override
            public AsyncIOWriter write(AtmosphereResponse r, byte[] data, int offset, int length) throws IOException {
                suspendedResponse.write(data, offset, length);
                suspendedResponse.flushBuffer();
                return this;
            }

            @Override
            public void close(AtmosphereResponse response) throws IOException {
            }
        });
        // REVISIT we need to keep this response's asyncwriter alive so that data can be written to the
        // suspended response, but investigate if there is a better alternative.
        r.getResponse().destroyable(false);
        return Action.CONTINUE;
    }
    r.addEventListener(new AtmosphereResourceEventListenerAdapter() {

        @Override
        public void onSuspend(AtmosphereResourceEvent event) {
            final String srid = (String) event.getResource().getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
            LOG.log(Level.FINE, "Registrering suspended resource: {}", srid);
            suspendedResponses.put(srid, event.getResource().getResponse());
            AsyncIOWriter writer = event.getResource().getResponse().getAsyncIOWriter();
            if (writer instanceof AtmosphereInterceptorWriter) {
                ((AtmosphereInterceptorWriter) writer).interceptor(interceptor);
            }
        }

        @Override
        public void onDisconnect(AtmosphereResourceEvent event) {
            super.onDisconnect(event);
            final String srid = (String) event.getResource().getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
            LOG.log(Level.FINE, "Unregistrering suspended resource: {}", srid);
            suspendedResponses.remove(srid);
        }
    });
    AtmosphereRequest request = r.getRequest();
    if (request.getAttribute(REQUEST_DISPATCHED) == null) {
        AtmosphereResponse response = null;
        AtmosphereFramework framework = r.getAtmosphereConfig().framework();
        try {
            byte[] data = WebSocketUtils.readBody(request.getInputStream());
            if (data.length == 0) {
                if (AtmosphereResource.TRANSPORT.WEBSOCKET == r.transport() || AtmosphereResource.TRANSPORT.SSE == r.transport()) {
                    r.suspend();
                    return Action.SUSPEND;
                }
                return Action.CANCELLED;
            }
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "inspecting data {0}", new String(data));
            }
            try {
                AtmosphereRequest ar = createAtmosphereRequest(request, data);
                response = new WrappedAtmosphereResponse(r.getResponse(), ar);
                ar.localAttributes().put(REQUEST_DISPATCHED, "true");
                String refid = ar.getHeader(WebSocketConstants.DEFAULT_REQUEST_ID_KEY);
                if (refid != null) {
                    ar.localAttributes().put(WebSocketConstants.DEFAULT_REQUEST_ID_KEY, refid);
                }
                // This is a new request, we must clean the Websocket AtmosphereResource.
                request.removeAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE);
                response.request(ar);
                attachWriter(r);
                Action action = framework.doCometSupport(ar, response);
                if (action.type() == Action.TYPE.SUSPEND) {
                    ar.destroyable(false);
                    response.destroyable(false);
                }
            } catch (Exception e) {
                LOG.log(Level.WARNING, "Error during request dispatching", e);
                if (response == null) {
                    response = new WrappedAtmosphereResponse(r.getResponse(), request);
                }
                if (e instanceof InvalidPathException) {
                    response.setIntHeader(WebSocketUtils.SC_KEY, 400);
                } else {
                    response.setIntHeader(WebSocketUtils.SC_KEY, 500);
                }
                OutputStream out = response.getOutputStream();
                out.write(createResponse(response, null, true));
                out.close();
            }
            return Action.CANCELLED;
        } catch (IOException e) {
            LOG.log(Level.WARNING, "Error during protocol processing", e);
        }
    } else {
        request.destroyable(false);
    }
    return Action.CONTINUE;
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Action(org.atmosphere.cpr.Action) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) AsyncIOWriter(org.atmosphere.cpr.AsyncIOWriter) IOException(java.io.IOException) InvalidPathException(org.apache.cxf.transport.websocket.InvalidPathException) IOException(java.io.IOException) InvalidPathException(org.apache.cxf.transport.websocket.InvalidPathException) AtmosphereResourceEventListenerAdapter(org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereFramework(org.atmosphere.cpr.AtmosphereFramework) AtmosphereResourceEvent(org.atmosphere.cpr.AtmosphereResourceEvent) AtmosphereInterceptorWriter(org.atmosphere.cpr.AtmosphereInterceptorWriter)

Example 2 with InvalidPathException

use of org.apache.cxf.transport.websocket.InvalidPathException in project cxf by apache.

the class DefaultProtocolInterceptor method createAtmosphereRequest.

/**
 * Creates a virtual request using the specified parent request and the actual data.
 *
 * @param r
 * @param data
 * @return
 * @throws IOException
 */
protected AtmosphereRequest createAtmosphereRequest(AtmosphereRequest r, byte[] data) throws IOException {
    AtmosphereRequest.Builder b = new AtmosphereRequestImpl.Builder();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    Map<String, String> hdrs = WebSocketUtils.readHeaders(in);
    String path = hdrs.get(WebSocketUtils.URI_KEY);
    String origin = r.getRequestURI();
    if (!path.startsWith(origin)) {
        LOG.log(Level.WARNING, "invalid path: {0} not within {1}", new Object[] { path, origin });
        throw new InvalidPathException();
    }
    String requestURI = path;
    String requestURL = r.getRequestURL() + requestURI.substring(r.getRequestURI().length());
    String contentType = hdrs.get("Content-Type");
    String method = hdrs.get(WebSocketUtils.METHOD_KEY);
    b.pathInfo(path).contentType(contentType).headers(hdrs).method(method).requestURI(requestURI).requestURL(requestURL).request(r);
    // add the body only if it is present
    byte[] body = WebSocketUtils.readBody(in);
    if (body.length > 0) {
        b.body(body);
    }
    return b.build();
}
Also used : AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidPathException(org.apache.cxf.transport.websocket.InvalidPathException)

Aggregations

InvalidPathException (org.apache.cxf.transport.websocket.InvalidPathException)2 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1 Action (org.atmosphere.cpr.Action)1 AsyncIOWriter (org.atmosphere.cpr.AsyncIOWriter)1 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)1 AtmosphereInterceptorWriter (org.atmosphere.cpr.AtmosphereInterceptorWriter)1 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)1 AtmosphereResourceEventListenerAdapter (org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter)1 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)1