Search in sources :

Example 71 with Message

use of org.apache.cxf.message.Message in project tomee by apache.

the class KerberosAuthenticationFilter method filter.

@Override
public void filter(ContainerRequestContext context) {
    List<String> authHeaders = messageContext.getHttpHeaders().getRequestHeader(HttpHeaders.AUTHORIZATION);
    if (authHeaders == null || authHeaders.size() != 1) {
        LOG.fine("No Authorization header is available");
        throw ExceptionUtils.toNotAuthorizedException(null, getFaultResponse());
    }
    String[] authPair = authHeaders.get(0).split(" ");
    if (authPair.length != 2 || !NEGOTIATE_SCHEME.equalsIgnoreCase(authPair[0])) {
        LOG.fine("Negotiate Authorization scheme is expected");
        throw ExceptionUtils.toNotAuthorizedException(null, getFaultResponse());
    }
    byte[] serviceTicket = getServiceTicket(authPair[1]);
    try {
        Subject serviceSubject = loginAndGetSubject();
        GSSContext gssContext = createGSSContext();
        Subject.doAs(serviceSubject, new ValidateServiceTicketAction(gssContext, serviceTicket));
        GSSName srcName = gssContext.getSrcName();
        if (srcName == null) {
            throw ExceptionUtils.toNotAuthorizedException(null, getFaultResponse());
        }
        String complexUserName = srcName.toString();
        String simpleUserName = complexUserName;
        int index = simpleUserName.lastIndexOf('@');
        if (index > 0) {
            simpleUserName = simpleUserName.substring(0, index);
        }
        Message m = JAXRSUtils.getCurrentMessage();
        m.put(SecurityContext.class, createSecurityContext(simpleUserName, complexUserName, gssContext));
        if (!gssContext.getCredDelegState()) {
            gssContext.dispose();
        }
    } catch (LoginException e) {
        LOG.fine("Unsuccessful JAAS login for the service principal: " + e.getMessage());
        throw ExceptionUtils.toNotAuthorizedException(e, getFaultResponse());
    } catch (GSSException e) {
        LOG.fine("GSS API exception: " + e.getMessage());
        throw ExceptionUtils.toNotAuthorizedException(e, getFaultResponse());
    } catch (PrivilegedActionException e) {
        LOG.fine("PrivilegedActionException: " + e.getMessage());
        throw ExceptionUtils.toNotAuthorizedException(e, getFaultResponse());
    }
}
Also used : GSSName(org.ietf.jgss.GSSName) Message(org.apache.cxf.message.Message) PrivilegedActionException(java.security.PrivilegedActionException) Subject(javax.security.auth.Subject) GSSException(org.ietf.jgss.GSSException) GSSContext(org.ietf.jgss.GSSContext) LoginException(javax.security.auth.login.LoginException)

Example 72 with Message

use of org.apache.cxf.message.Message in project tomee by apache.

the class CXFNonSpringJaxrsServlet method setInterceptors.

@SuppressWarnings("unchecked")
protected void setInterceptors(JAXRSServerFactoryBean bean, ServletConfig servletConfig, String paramName, String splitChar) throws ServletException {
    String value = servletConfig.getInitParameter(paramName);
    if (value == null) {
        return;
    }
    String[] values = value.split(splitChar);
    List<Interceptor<? extends Message>> list = new ArrayList<>();
    for (String interceptorVal : values) {
        Map<String, List<String>> props = new HashMap<>();
        String theValue = getClassNameAndProperties(interceptorVal, props);
        if (!theValue.isEmpty()) {
            try {
                Class<?> intClass = loadClass(theValue, "Interceptor");
                Object object = intClass.newInstance();
                injectProperties(object, props);
                list.add((Interceptor<? extends Message>) object);
            } catch (ServletException ex) {
                throw ex;
            } catch (Exception ex) {
                LOG.warning("Interceptor class " + theValue + " can not be created");
                throw new ServletException(ex);
            }
        }
    }
    if (!list.isEmpty()) {
        if (OUT_INTERCEPTORS_PARAM.equals(paramName)) {
            bean.setOutInterceptors(list);
        } else if (OUT_FAULT_INTERCEPTORS_PARAM.equals(paramName)) {
            bean.setOutFaultInterceptors(list);
        } else {
            bean.setInInterceptors(list);
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Interceptor(org.apache.cxf.interceptor.Interceptor)

Example 73 with Message

use of org.apache.cxf.message.Message in project tomee by apache.

the class JAXRSUtils method selectResourceClass.

public static Map<ClassResourceInfo, MultivaluedMap<String, String>> selectResourceClass(List<ClassResourceInfo> resources, String path, Message message) {
    LOG.fine(() -> new org.apache.cxf.common.i18n.Message("START_CRI_MATCH", BUNDLE, path).toString());
    if (resources.size() == 1) {
        MultivaluedMap<String, String> values = new MetadataMap<>();
        return resources.get(0).getURITemplate().match(path, values) ? Collections.singletonMap(resources.get(0), values) : null;
    }
    SortedMap<ClassResourceInfo, MultivaluedMap<String, String>> candidateList = new TreeMap<ClassResourceInfo, MultivaluedMap<String, String>>(new ClassResourceInfoComparator(message));
    for (ClassResourceInfo cri : resources) {
        MultivaluedMap<String, String> map = new MetadataMap<>();
        if (cri.getURITemplate().match(path, map)) {
            candidateList.put(cri, map);
            LOG.fine(() -> new org.apache.cxf.common.i18n.Message("CRI_SELECTED_POSSIBLY", BUNDLE, cri.getServiceClass().getName(), path, cri.getURITemplate().getValue()).toString());
        } else {
            LOG.fine(() -> new org.apache.cxf.common.i18n.Message("CRI_NO_MATCH", BUNDLE, path, cri.getServiceClass().getName()).toString());
        }
    }
    if (!candidateList.isEmpty()) {
        Map<ClassResourceInfo, MultivaluedMap<String, String>> cris = new LinkedHashMap<>(candidateList.size());
        ClassResourceInfo firstCri = null;
        for (Map.Entry<ClassResourceInfo, MultivaluedMap<String, String>> entry : candidateList.entrySet()) {
            ClassResourceInfo cri = entry.getKey();
            if (cris.isEmpty()) {
                firstCri = cri;
                cris.put(cri, entry.getValue());
            } else if (firstCri != null && URITemplate.compareTemplates(firstCri.getURITemplate(), cri.getURITemplate()) == 0) {
                cris.put(cri, entry.getValue());
            } else {
                break;
            }
            LOG.fine(() -> new org.apache.cxf.common.i18n.Message("CRI_SELECTED", BUNDLE, cri.getServiceClass().getName(), path, cri.getURITemplate().getValue()).toString());
        }
        return cris;
    }
    return null;
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfoComparator(org.apache.cxf.jaxrs.model.ClassResourceInfoComparator) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 74 with Message

use of org.apache.cxf.message.Message in project tomee by apache.

the class JAXRSUtils method createContextValue.

public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {
    Message contextMessage = getContextMessage(m);
    Object o = null;
    if (UriInfo.class.isAssignableFrom(clazz)) {
        o = createUriInfo(contextMessage);
    } else if (HttpHeaders.class.isAssignableFrom(clazz) || ProtocolHeaders.class.isAssignableFrom(clazz)) {
        o = createHttpHeaders(contextMessage, clazz);
    } else if (SecurityContext.class.isAssignableFrom(clazz)) {
        SecurityContext customContext = contextMessage.get(SecurityContext.class);
        o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
    } else if (MessageContext.class.isAssignableFrom(clazz)) {
        o = new MessageContextImpl(m);
    } else if (ResourceInfo.class.isAssignableFrom(clazz)) {
        o = new ResourceInfoImpl(contextMessage);
    } else if (ResourceContext.class.isAssignableFrom(clazz)) {
        OperationResourceInfo operationResourceInfo = contextMessage.getExchange().get(OperationResourceInfo.class);
        if (operationResourceInfo != null) {
            o = new ResourceContextImpl(contextMessage, operationResourceInfo);
        }
    } else if (Request.class.isAssignableFrom(clazz)) {
        o = new RequestImpl(contextMessage);
    } else if (Providers.class.isAssignableFrom(clazz)) {
        o = new ProvidersImpl(contextMessage);
    } else if (ContextResolver.class.isAssignableFrom(clazz)) {
        o = createContextResolver(genericType, contextMessage);
    } else if (Configuration.class.isAssignableFrom(clazz)) {
        o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
    } else if (Application.class.isAssignableFrom(clazz)) {
        ProviderInfo<?> providerInfo = (ProviderInfo<?>) contextMessage.getExchange().getEndpoint().get(Application.class.getName());
        o = providerInfo == null ? null : providerInfo.getProvider();
    } else if (contextMessage != null) {
        ContextProvider<?> provider = ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
        if (provider != null) {
            o = provider.createContext(contextMessage);
        }
    }
    if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
        o = HttpUtils.createServletResourceValue(contextMessage, clazz);
    }
    return clazz.cast(o);
}
Also used : SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) Configuration(javax.ws.rs.core.Configuration) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) SecurityContext(javax.ws.rs.core.SecurityContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 75 with Message

use of org.apache.cxf.message.Message in project tomee by apache.

the class MultipartProvider method getHandlerForObject.

private <T> DataHandler getHandlerForObject(T obj, Class<T> cls, Type genericType, Annotation[] anns, String mimeType) {
    MediaType mt = JAXRSUtils.toMediaType(mimeType);
    mc.put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY, this);
    final MessageBodyWriter<T> r;
    try {
        r = mc.getProviders().getMessageBodyWriter(cls, genericType, anns, mt);
    } finally {
        mc.put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY, null);
    }
    if (r == null) {
        org.apache.cxf.common.i18n.Message message = new org.apache.cxf.common.i18n.Message("NO_MSG_WRITER", BUNDLE, cls);
        LOG.severe(message.toString());
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }
    return new MessageBodyWriterDataHandler<T>(r, obj, cls, genericType, anns, mt);
}
Also used : Message(org.apache.cxf.message.Message) MediaType(javax.ws.rs.core.MediaType)

Aggregations

Message (org.apache.cxf.message.Message)1002 Test (org.junit.Test)507 MessageImpl (org.apache.cxf.message.MessageImpl)291 Exchange (org.apache.cxf.message.Exchange)199 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)169 Endpoint (org.apache.cxf.endpoint.Endpoint)91 Interceptor (org.apache.cxf.interceptor.Interceptor)87 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)85 ArrayList (java.util.ArrayList)83 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)76 List (java.util.List)75 IOException (java.io.IOException)73 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)73 Method (java.lang.reflect.Method)69 Bus (org.apache.cxf.Bus)69 QName (javax.xml.namespace.QName)62 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)55 HashMap (java.util.HashMap)53 Fault (org.apache.cxf.interceptor.Fault)51 ByteArrayInputStream (java.io.ByteArrayInputStream)49