Search in sources :

Example 1 with ExtractorException

use of org.glassfish.jersey.internal.inject.ExtractorException in project dropwizard by dropwizard.

the class AbstractParamConverterProvider method getConverter.

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
    if (AbstractParam.class.isAssignableFrom(rawType)) {
        final String parameterName = JerseyParameterNameProvider.getParameterNameFromAnnotations(annotations).orElse("Parameter");
        final Constructor<T> constructor;
        try {
            constructor = rawType.getConstructor(String.class, String.class);
        } catch (NoSuchMethodException ignored) {
            // leaving Jersey to handle these parameters as it normally would.
            return null;
        }
        return new ParamConverter<T>() {

            @Override
            @SuppressWarnings("unchecked")
            public T fromString(String value) {
                if (rawType != NonEmptyStringParam.class && Strings.isNullOrEmpty(value)) {
                    return null;
                }
                try {
                    return _fromString(value);
                } catch (InvocationTargetException ex) {
                    final Throwable cause = ex.getCause();
                    if (cause instanceof WebApplicationException) {
                        throw (WebApplicationException) cause;
                    } else {
                        throw new ExtractorException(cause);
                    }
                } catch (final Exception ex) {
                    throw new ProcessingException(ex);
                }
            }

            protected T _fromString(String value) throws Exception {
                return constructor.newInstance(value, parameterName);
            }

            @Override
            public String toString(T value) throws IllegalArgumentException {
                if (value == null) {
                    throw new IllegalArgumentException(LocalizationMessages.METHOD_PARAMETER_CANNOT_BE_NULL("value"));
                }
                return value.toString();
            }
        };
    }
    return null;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ParamConverter(javax.ws.rs.ext.ParamConverter) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with ExtractorException

use of org.glassfish.jersey.internal.inject.ExtractorException in project jersey by jersey.

the class MultivaluedParameterExtractorFactory method process.

@SuppressWarnings("unchecked")
private MultivaluedParameterExtractor<?> process(final ParamConverterFactory paramConverterFactory, final String defaultValue, final Class<?> rawType, final Type type, final Annotation[] annotations, final String parameterName) {
    // Try to find a converter that support rawType and type at first.
    // E.g. if someone writes a converter that support List<Integer> this approach should precede the next one.
    ParamConverter<?> converter = paramConverterFactory.getConverter(rawType, type, annotations);
    if (converter != null) {
        try {
            return new SingleValueExtractor(converter, parameterName, defaultValue);
        } catch (final ExtractorException e) {
            throw e;
        } catch (final Exception e) {
            throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
        }
    }
    // Check whether the rawType is the type of the collection supported.
    if (rawType == List.class || rawType == Set.class || rawType == SortedSet.class) {
        // Get the generic type of the list. If none is found default to String.
        final List<ClassTypePair> typePairs = ReflectionHelper.getTypeArgumentAndClass(type);
        final ClassTypePair typePair = (typePairs.size() == 1) ? typePairs.get(0) : null;
        if (typePair == null || typePair.rawClass() == String.class) {
            return StringCollectionExtractor.getInstance(rawType, parameterName, defaultValue);
        } else {
            converter = paramConverterFactory.getConverter(typePair.rawClass(), typePair.type(), annotations);
            if (converter == null) {
                return null;
            }
            try {
                return CollectionExtractor.getInstance(rawType, converter, parameterName, defaultValue);
            } catch (final ExtractorException e) {
                throw e;
            } catch (final Exception e) {
                throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
            }
        }
    }
    // Check primitive types.
    if (rawType == String.class) {
        return new SingleStringValueExtractor(parameterName, defaultValue);
    } else if (rawType == Character.class) {
        return new PrimitiveCharacterExtractor(parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(rawType));
    } else if (rawType.isPrimitive()) {
        // Convert primitive to wrapper class
        final Class<?> wrappedRaw = PrimitiveMapper.primitiveToClassMap.get(rawType);
        if (wrappedRaw == null) {
            // Primitive type not supported
            return null;
        }
        if (wrappedRaw == Character.class) {
            return new PrimitiveCharacterExtractor(parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(wrappedRaw));
        }
        // Check for static valueOf(String)
        final Method valueOf = AccessController.doPrivileged(ReflectionHelper.getValueOfStringMethodPA(wrappedRaw));
        if (valueOf != null) {
            try {
                return new PrimitiveValueOfExtractor(valueOf, parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(wrappedRaw));
            } catch (final Exception e) {
                throw new ProcessingException(LocalizationMessages.DEFAULT_COULD_NOT_PROCESS_METHOD(defaultValue, valueOf));
            }
        }
    }
    return null;
}
Also used : ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) Method(java.lang.reflect.Method) SortedSet(java.util.SortedSet) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ProcessingException(javax.ws.rs.ProcessingException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ProcessingException(javax.ws.rs.ProcessingException)

Example 3 with ExtractorException

use of org.glassfish.jersey.internal.inject.ExtractorException in project jersey by jersey.

the class ParamConverterInternalTest method testEagerConverter.

@Test
public void testEagerConverter() throws Exception {
    try {
        new ApplicationHandler(new ResourceConfig(MyEagerParamProvider.class, Resource.class));
        fail("ExtractorException expected.");
    } catch (final ExtractorException expected) {
    // ok
    }
}
Also used : ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Aggregations

ExtractorException (org.glassfish.jersey.internal.inject.ExtractorException)3 ProcessingException (javax.ws.rs.ProcessingException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 SortedSet (java.util.SortedSet)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ParamConverter (javax.ws.rs.ext.ParamConverter)1 ClassTypePair (org.glassfish.jersey.internal.util.collection.ClassTypePair)1 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)1 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)1 Test (org.junit.Test)1