Search in sources :

Example 1 with PathSegmentImpl

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

the class JAXRSUtils method getPathSegments.

public static List<PathSegment> getPathSegments(String thePath, boolean decode, boolean ignoreLastSlash) {
    List<PathSegment> theList = StringUtils.splitAsStream(thePath, "/").filter(StringUtils.notEmpty()).map(p -> new PathSegmentImpl(p, decode)).collect(Collectors.toList());
    int len = thePath.length();
    if (len > 0 && thePath.charAt(len - 1) == '/') {
        String value = ignoreLastSlash ? "" : "/";
        theList.add(new PathSegmentImpl(value, false));
    }
    return theList;
}
Also used : Arrays(java.util.Arrays) PackageUtils(org.apache.cxf.common.util.PackageUtils) Produces(javax.ws.rs.Produces) StringUtils(org.apache.cxf.common.util.StringUtils) SecurityContext(javax.ws.rs.core.SecurityContext) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) ContextResolver(javax.ws.rs.ext.ContextResolver) MediaType(javax.ws.rs.core.MediaType) Service(org.apache.cxf.service.Service) ProtocolHeaders(org.apache.cxf.jaxrs.ext.ProtocolHeaders) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) Map(java.util.Map) PropertyUtils(org.apache.cxf.common.util.PropertyUtils) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) BeanParamInfo(org.apache.cxf.jaxrs.model.BeanParamInfo) AsyncResponse(javax.ws.rs.container.AsyncResponse) Set(java.util.Set) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) Reader(java.io.Reader) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) StandardCharsets(java.nio.charset.StandardCharsets) PathSegmentImpl(org.apache.cxf.jaxrs.impl.PathSegmentImpl) WebApplicationException(javax.ws.rs.WebApplicationException) UriInfo(javax.ws.rs.core.UriInfo) QName(javax.xml.namespace.QName) AttachmentUtils(org.apache.cxf.jaxrs.utils.multipart.AttachmentUtils) ContainerResponseContextImpl(org.apache.cxf.jaxrs.impl.ContainerResponseContextImpl) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) Configuration(javax.ws.rs.core.Configuration) ProtocolHeadersImpl(org.apache.cxf.jaxrs.ext.ProtocolHeadersImpl) Parameter(org.apache.cxf.jaxrs.model.Parameter) Supplier(java.util.function.Supplier) HttpMethod(javax.ws.rs.HttpMethod) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) ResourceBundle(java.util.ResourceBundle) LogUtils(org.apache.cxf.common.logging.LogUtils) OperationResourceInfoStack(org.apache.cxf.jaxrs.model.OperationResourceInfoStack) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) LinkedHashSet(java.util.LinkedHashSet) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) PathSegment(javax.ws.rs.core.PathSegment) ReaderInterceptorMBR(org.apache.cxf.jaxrs.impl.ReaderInterceptorMBR) IOException(java.io.IOException) Field(java.lang.reflect.Field) BeanResourceInfo(org.apache.cxf.jaxrs.model.BeanResourceInfo) Exchange(org.apache.cxf.message.Exchange) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) ParameterizedType(java.lang.reflect.ParameterizedType) TreeMap(java.util.TreeMap) ResourceContext(javax.ws.rs.container.ResourceContext) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) ResponseImpl(org.apache.cxf.jaxrs.impl.ResponseImpl) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) DOMUtils(org.apache.cxf.helpers.DOMUtils) ClassResourceInfoComparator(org.apache.cxf.jaxrs.model.ClassResourceInfoComparator) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Application(javax.ws.rs.core.Application) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) ClientErrorException(javax.ws.rs.ClientErrorException) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) Consumes(javax.ws.rs.Consumes) ReflectionUtil(org.apache.cxf.common.util.ReflectionUtil) Method(java.lang.reflect.Method) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) Providers(javax.ws.rs.ext.Providers) StreamingOutput(javax.ws.rs.core.StreamingOutput) Logger(java.util.logging.Logger) MediaTypeHeaderProvider(org.apache.cxf.jaxrs.impl.MediaTypeHeaderProvider) Collectors(java.util.stream.Collectors) Cookie(javax.ws.rs.core.Cookie) List(java.util.List) ReaderInterceptorContextImpl(org.apache.cxf.jaxrs.impl.ReaderInterceptorContextImpl) HttpHeaders(javax.ws.rs.core.HttpHeaders) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Request(javax.ws.rs.core.Request) ContainerRequestContextImpl(org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) SortedMap(java.util.SortedMap) ReaderInterceptorContext(javax.ws.rs.ext.ReaderInterceptorContext) OperationResourceInfoComparator(org.apache.cxf.jaxrs.model.OperationResourceInfoComparator) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) AbstractConfigurableProvider(org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider) Level(java.util.logging.Level) HashSet(java.util.HashSet) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) ResourceInfo(javax.ws.rs.container.ResourceInfo) Fault(org.apache.cxf.interceptor.Fault) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) HttpHeadersImpl(org.apache.cxf.jaxrs.impl.HttpHeadersImpl) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) ResponseBuilderImpl(org.apache.cxf.jaxrs.impl.ResponseBuilderImpl) LinkedList(java.util.LinkedList) OutputStream(java.io.OutputStream) Message(org.apache.cxf.message.Message) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) BundleUtils(org.apache.cxf.common.i18n.BundleUtils) UriInfoImpl(org.apache.cxf.jaxrs.impl.UriInfoImpl) MessageUtils(org.apache.cxf.message.MessageUtils) Comparator(java.util.Comparator) WriterInterceptorContextImpl(org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl) Collections(java.util.Collections) InputStream(java.io.InputStream) MethodInvocationInfo(org.apache.cxf.jaxrs.model.MethodInvocationInfo) PathSegmentImpl(org.apache.cxf.jaxrs.impl.PathSegmentImpl) PathSegment(javax.ws.rs.core.PathSegment)

Example 2 with PathSegmentImpl

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

the class InjectionUtils method handleParameter.

@SuppressWarnings("unchecked")
public static <T> T handleParameter(String value, boolean decoded, Class<T> pClass, Type genericType, Annotation[] paramAnns, ParameterType pType, Message message) {
    if (value == null) {
        return null;
    }
    if (pType == ParameterType.PATH) {
        if (PathSegment.class.isAssignableFrom(pClass)) {
            return pClass.cast(new PathSegmentImpl(value, decoded));
        } else if (!MessageUtils.getContextualBoolean(message, IGNORE_MATRIX_PARAMETERS)) {
            value = new PathSegmentImpl(value, false).getPath();
        }
    }
    value = decodeValue(value, decoded, pType);
    Object result = null;
    try {
        result = createFromParameterHandler(value, pClass, genericType, paramAnns, message);
    } catch (IllegalArgumentException nfe) {
        throw createParamConversionException(pType, nfe);
    }
    if (result != null) {
        T theResult = null;
        if (pClass.isPrimitive()) {
            theResult = (T) result;
        } else {
            theResult = pClass.cast(result);
        }
        return theResult;
    }
    if (Number.class.isAssignableFrom(pClass) && "".equals(value)) {
        // pass empty string to boxed number type will result in 404
        return null;
    }
    if (Boolean.class == pClass) {
        // allow == checks for Boolean object
        pClass = (Class<T>) Boolean.TYPE;
    }
    if (pClass.isPrimitive()) {
        try {
            // the object is a Boolean object
            return (T) PrimitiveUtils.read(value, pClass);
        } catch (NumberFormatException nfe) {
            throw createParamConversionException(pType, nfe);
        }
    }
    boolean adapterHasToBeUsed = false;
    Class<?> cls = pClass;
    Class<?> valueType = JAXBUtils.getValueTypeFromAdapter(pClass, pClass, paramAnns);
    if (valueType != cls) {
        cls = valueType;
        adapterHasToBeUsed = true;
    }
    if (pClass == String.class && !adapterHasToBeUsed) {
        return pClass.cast(value);
    }
    // check constructors accepting a single String value
    try {
        Constructor<?> c = cls.getConstructor(new Class<?>[] { String.class });
        result = c.newInstance(new Object[] { value });
    } catch (NoSuchMethodException ex) {
    // try valueOf
    } catch (WebApplicationException ex) {
        throw ex;
    } catch (Exception ex) {
        Throwable t = getOrThrowActualException(ex);
        LOG.warning(new org.apache.cxf.common.i18n.Message("CLASS_CONSTRUCTOR_FAILURE", BUNDLE, pClass.getName()).toString());
        Response r = JAXRSUtils.toResponse(HttpUtils.getParameterFailureStatus(pType));
        throw ExceptionUtils.toHttpException(t, r);
    }
    if (result == null) {
        // check for valueOf(String) static methods
        String[] methodNames = cls.isEnum() ? new String[] { "fromString", "fromValue", "valueOf" } : new String[] { "valueOf", "fromString" };
        result = evaluateFactoryMethods(value, pType, result, cls, methodNames);
    }
    if (adapterHasToBeUsed) {
        // as the last resort, try XmlJavaTypeAdapters
        Object valueToReplace = result != null ? result : value;
        try {
            result = JAXBUtils.convertWithAdapter(valueToReplace, pClass, paramAnns);
        } catch (Throwable ex) {
            result = null;
        }
    }
    if (result == null) {
        reportServerError("WRONG_PARAMETER_TYPE", pClass.getName());
    }
    try {
        return pClass.cast(result);
    } catch (ClassCastException ex) {
        reportServerError("WRONG_PARAMETER_TYPE", pClass.getName());
        return null;
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) WebApplicationException(javax.ws.rs.WebApplicationException) Response(javax.ws.rs.core.Response) PathSegmentImpl(org.apache.cxf.jaxrs.impl.PathSegmentImpl)

Aggregations

WebApplicationException (javax.ws.rs.WebApplicationException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1