Search in sources :

Example 1 with HttpHeadersImpl

use of org.apache.cxf.jaxrs.impl.HttpHeadersImpl in project tomee by apache.

the class JAASAuthenticationFilter method handleAuthenticationException.

protected Response handleAuthenticationException(SecurityException ex, Message m) {
    HttpHeaders headers = new HttpHeadersImpl(m);
    if (redirectURI != null && isRedirectPossible(headers)) {
        final URI finalRedirectURI;
        if (!redirectURI.isAbsolute()) {
            String endpointAddress = HttpUtils.getEndpointAddress(m);
            Object basePathProperty = m.get(Message.BASE_PATH);
            if (ignoreBasePath && basePathProperty != null && !"/".equals(basePathProperty)) {
                int index = endpointAddress.lastIndexOf(basePathProperty.toString());
                if (index != -1) {
                    endpointAddress = endpointAddress.substring(0, index);
                }
            }
            finalRedirectURI = UriBuilder.fromUri(endpointAddress).path(redirectURI.toString()).build();
        } else {
            finalRedirectURI = redirectURI;
        }
        return Response.status(getRedirectStatus()).header(HttpHeaders.LOCATION, finalRedirectURI).build();
    }
    ResponseBuilder builder = Response.status(Response.Status.UNAUTHORIZED);
    StringBuilder sb = new StringBuilder();
    List<String> authHeader = headers.getRequestHeader(HttpHeaders.AUTHORIZATION);
    if (authHeader != null && !authHeader.isEmpty()) {
        // should HttpHeadersImpl do it ?
        String[] authValues = authHeader.get(0).split(" ");
        if (authValues.length > 0) {
            sb.append(authValues[0]);
        }
    } else {
        sb.append("Basic");
    }
    if (realmName != null) {
        sb.append(" realm=\"").append(realmName).append('"');
    }
    builder.header(HttpHeaders.WWW_AUTHENTICATE, sb.toString());
    return builder.build();
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) HttpHeadersImpl(org.apache.cxf.jaxrs.impl.HttpHeadersImpl)

Example 2 with HttpHeadersImpl

use of org.apache.cxf.jaxrs.impl.HttpHeadersImpl in project cxf by apache.

the class WadlGenerator method doFilter.

protected void doFilter(ContainerRequestContext context, Message m) {
    if (!"GET".equals(m.get(Message.HTTP_REQUEST_METHOD))) {
        return;
    }
    UriInfo ui = context.getUriInfo();
    if (!ui.getQueryParameters().containsKey(WADL_QUERY)) {
        if (stylesheetReference != null || !docLocationMap.isEmpty()) {
            String path = ui.getPath(false);
            if (path.startsWith("/") && !path.isEmpty()) {
                path = path.substring(1);
            }
            if (stylesheetReference != null && path.endsWith(".xsl") || docLocationMap.containsKey(path)) {
                context.abortWith(getExistingResource(m, ui, path));
            }
        }
        return;
    }
    if (ignoreRequests) {
        context.abortWith(Response.status(404).build());
        return;
    }
    if (allowList != null && !allowList.isEmpty()) {
        ServletRequest servletRequest = (ServletRequest) m.getContextualProperty("HTTP.REQUEST");
        final String remoteAddress;
        if (servletRequest != null) {
            remoteAddress = servletRequest.getRemoteAddr();
        } else {
            remoteAddress = "";
        }
        boolean foundMatch = false;
        for (String addr : allowList) {
            if (addr.equals(remoteAddress)) {
                foundMatch = true;
                break;
            }
        }
        if (!foundMatch) {
            context.abortWith(Response.status(404).build());
            return;
        }
    }
    HttpHeaders headers = new HttpHeadersImpl(m);
    List<MediaType> accepts = headers.getAcceptableMediaTypes();
    MediaType type = accepts.contains(WADL_TYPE) ? WADL_TYPE : accepts.contains(MediaType.APPLICATION_JSON_TYPE) ? MediaType.APPLICATION_JSON_TYPE : defaultWadlResponseMediaType;
    Response response = getExistingWadl(m, ui, type);
    if (response != null) {
        context.abortWith(response);
        return;
    }
    boolean isJson = isJson(type);
    StringBuilder sbMain = generateWADL(getBaseURI(m, ui), getResourcesList(m, ui), isJson, m, ui);
    m.getExchange().put(JAXRSUtils.IGNORE_MESSAGE_WRITERS, !isJson && ignoreMessageWriters);
    Response r = Response.ok().type(type).entity(createResponseEntity(m, ui, sbMain.toString(), isJson)).build();
    context.abortWith(r);
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) ServletRequest(javax.servlet.ServletRequest) HttpHeaders(javax.ws.rs.core.HttpHeaders) MediaType(javax.ws.rs.core.MediaType) UriInfo(javax.ws.rs.core.UriInfo) HttpHeadersImpl(org.apache.cxf.jaxrs.impl.HttpHeadersImpl)

Example 3 with HttpHeadersImpl

use of org.apache.cxf.jaxrs.impl.HttpHeadersImpl in project cxf by apache.

the class BinaryDataProvider method copyInputToOutput.

protected void copyInputToOutput(InputStream is, OutputStream os, Annotation[] anns, MultivaluedMap<String, Object> outHeaders) throws IOException {
    if (isRangeSupported()) {
        Message inMessage = PhaseInterceptorChain.getCurrentMessage().getExchange().getInMessage();
        handleRangeRequest(is, os, new HttpHeadersImpl(inMessage), outHeaders);
    } else {
        boolean nioWrite = AnnotationUtils.getAnnotation(anns, UseNio.class) != null;
        if (nioWrite) {
            ContinuationProvider provider = getContinuationProvider();
            if (provider != null) {
                copyUsingNio(is, os, provider.getContinuation());
            }
            return;
        }
        if (closeResponseInputStream) {
            IOUtils.copyAndCloseInput(is, os, bufferSize);
        } else {
            IOUtils.copy(is, os, bufferSize);
        }
    }
}
Also used : UseNio(org.apache.cxf.annotations.UseNio) ContinuationProvider(org.apache.cxf.continuations.ContinuationProvider) Message(org.apache.cxf.message.Message) HttpHeadersImpl(org.apache.cxf.jaxrs.impl.HttpHeadersImpl)

Example 4 with HttpHeadersImpl

use of org.apache.cxf.jaxrs.impl.HttpHeadersImpl in project camel by apache.

the class CxfRsInvoker method prepareExchange.

private org.apache.camel.Exchange prepareExchange(Exchange cxfExchange, Method method, Object[] paramArray, Object response) {
    ExchangePattern ep = ExchangePattern.InOut;
    if (method.getReturnType() == Void.class) {
        ep = ExchangePattern.InOnly;
    }
    final org.apache.camel.Exchange camelExchange = endpoint.createExchange(ep);
    if (response != null) {
        camelExchange.getOut().setBody(response);
    }
    CxfRsBinding binding = endpoint.getBinding();
    binding.populateExchangeFromCxfRsRequest(cxfExchange, camelExchange, method, paramArray);
    // the CXF in message property. Question: where should this property name be set up ? 
    if (endpoint.isPropagateContexts()) {
        camelExchange.setProperty(UriInfo.class.getName(), new UriInfoImpl(cxfExchange.getInMessage()));
        camelExchange.setProperty(Request.class.getName(), new RequestImpl(cxfExchange.getInMessage()));
        camelExchange.setProperty(HttpHeaders.class.getName(), new HttpHeadersImpl(cxfExchange.getInMessage()));
        camelExchange.setProperty(SecurityContext.class.getName(), new SecurityContextImpl(cxfExchange.getInMessage()));
    }
    return camelExchange;
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) Request(javax.ws.rs.core.Request) HttpHeadersImpl(org.apache.cxf.jaxrs.impl.HttpHeadersImpl) ExchangePattern(org.apache.camel.ExchangePattern) SecurityContext(javax.ws.rs.core.SecurityContext) UriInfo(javax.ws.rs.core.UriInfo) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) UriInfoImpl(org.apache.cxf.jaxrs.impl.UriInfoImpl)

Example 5 with HttpHeadersImpl

use of org.apache.cxf.jaxrs.impl.HttpHeadersImpl in project tomee by apache.

the class JAXRSUtils method readFromMessageBodyReader.

@SuppressWarnings("unchecked")
public static Object readFromMessageBodyReader(List<ReaderInterceptor> readers, Class<?> targetTypeClass, Type parameterType, Annotation[] parameterAnnotations, InputStream is, MediaType mediaType, Message m) throws IOException, WebApplicationException {
    // Verbose but avoids an extra context instantiation for the typical path
    if (readers.size() > 1) {
        ReaderInterceptor first = readers.remove(0);
        ReaderInterceptorContext context = new ReaderInterceptorContextImpl(targetTypeClass, parameterType, parameterAnnotations, is, m, readers);
        return first.aroundReadFrom(context);
    }
    MessageBodyReader<?> provider = ((ReaderInterceptorMBR) readers.get(0)).getMBR();
    @SuppressWarnings("rawtypes") Class cls = targetTypeClass;
    return provider.readFrom(cls, parameterType, parameterAnnotations, mediaType, new HttpHeadersImpl(m).getRequestHeaders(), is);
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) ReaderInterceptorContextImpl(org.apache.cxf.jaxrs.impl.ReaderInterceptorContextImpl) ReaderInterceptorMBR(org.apache.cxf.jaxrs.impl.ReaderInterceptorMBR) ReaderInterceptorContext(javax.ws.rs.ext.ReaderInterceptorContext) HttpHeadersImpl(org.apache.cxf.jaxrs.impl.HttpHeadersImpl)

Aggregations

HttpHeadersImpl (org.apache.cxf.jaxrs.impl.HttpHeadersImpl)11 HttpHeaders (javax.ws.rs.core.HttpHeaders)6 Cookie (javax.ws.rs.core.Cookie)3 URI (java.net.URI)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 UriInfo (javax.ws.rs.core.UriInfo)2 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)2 ReaderInterceptorContext (javax.ws.rs.ext.ReaderInterceptorContext)2 ReaderInterceptorContextImpl (org.apache.cxf.jaxrs.impl.ReaderInterceptorContextImpl)2 ReaderInterceptorMBR (org.apache.cxf.jaxrs.impl.ReaderInterceptorMBR)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ServletRequest (javax.servlet.ServletRequest)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AsyncResponse (javax.ws.rs.container.AsyncResponse)1 MediaType (javax.ws.rs.core.MediaType)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1