javax.ws.rs.ext.ParamConverter Java Examples

The following examples show how to use javax.ws.rs.ext.ParamConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BookServer.java    From cxf with Apache License 2.0 7 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType,
                                          Annotation[] annotations) {
    if (rawType == Book.class) {

        MessageBodyReader<Book> mbr = providers.getMessageBodyReader(Book.class,
                                                                     Book.class,
                                                                     annotations,
                                                                     MediaType.APPLICATION_XML_TYPE);
        MessageBodyWriter<Book> mbw = providers.getMessageBodyWriter(Book.class,
                                                                     Book.class,
                                                                     annotations,
                                                                     MediaType.APPLICATION_XML_TYPE);
        return (ParamConverter<T>)new XmlParamConverter(mbr, mbw);
    } else if (rawType == byte.class) {
        return (ParamConverter<T>)new ByteConverter();
    } else {
        return null;
    }

}
 
Example #2
Source File: ParamConverterUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the string-based representation of the value to the instance of particular type
 * using parameter converter provider and available parameter converter.
 * @param type type to convert from string-based representation
 * @param provider parameter converter provider to use
 * @param value the string-based representation to convert
 * @return instance of particular type converter from its string representation
 */
@SuppressWarnings("unchecked")
public static< T > T getValue(final Class< T > type, final ParamConverterProvider provider,
        final String value) {

    if (String.class.isAssignableFrom(type)) {
        return (T)value;
    }

    if (provider != null) {
        final ParamConverter< T > converter = provider.getConverter(type, null, new Annotation[0]);
        if (converter != null) {
            return converter.fromString(value);
        }
    }

    throw new IllegalArgumentException(String.format(
            "Unable to convert string '%s' to instance of class '%s': no appropriate converter provided",
            value, type.getName()));
}
 
Example #3
Source File: DummyParamProvider.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {

	if (rawType.getName().equals(Dummy.class.getName())) {
		return new ParamConverter<T>() {

			@Override
			public T fromString(String value) {
				Dummy dummy = new Dummy(value);
				return rawType.cast(dummy);
			}

			@Override
			public String toString(T myDummy) {
				if (myDummy == null) {
					return null;
				}
				return myDummy.toString();
			}
		};
	}
	return null;
}
 
Example #4
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
public <T> ParamConverter<T> createParameterHandler(Class<T> paramType,
                                                    Type genericType,
                                                    Annotation[] anns,
                                                    Message m) {

    anns = anns != null ? anns : new Annotation[]{};
    for (ProviderInfo<ParamConverterProvider> pi : paramConverters) {
        injectContextValues(pi, m);
        ParamConverter<T> converter = pi.getProvider().getConverter(paramType, genericType, anns);
        if (converter != null) {
            return converter;
        }
        pi.clearThreadLocalProxies();
    }
    return null;
}
 
Example #5
Source File: JavaTimeTypesParamConverterProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {

    if (rawType.equals(LocalDateTime.class)) {
        return (ParamConverter<T>) new LocalDateTimeConverter();
    } else if (rawType.equals(LocalDate.class)) {
        return (ParamConverter<T>) new LocalDateConverter();
    } else if (rawType.equals(LocalTime.class)) {
        return (ParamConverter<T>) new LocalTimeConverter();
    } else if (rawType.equals(OffsetDateTime.class)) {
        return (ParamConverter<T>) new OffsetDateTimeConverter();
    } else if (rawType.equals(OffsetTime.class)) {
        return (ParamConverter<T>) new OffsetTimeConverter();
    } else if (rawType.equals(ZonedDateTime.class)) {
        return (ParamConverter<T>) new ZonedDateTimeConverter();
    } else {
        return null;
    }
}
 
Example #6
Source File: ParamConverterProviderManager.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
public <T> ParamConverter getParamConverter(Class<?> rawType, Type baseType, Annotation[] annotations) {

		for (ParamConverterProvider paramConverterProvider : paramConverterProviders) {
			ParamConverter<T> paramConverter = paramConverterProvider.getConverter((Class<T>) rawType, baseType,
					annotations);

			if (paramConverter != null) {
				return paramConverter;
			}
		}

		return null;
	}
 
Example #7
Source File: PortletParamProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Boolean getBooleanParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Boolean> paramConverter = _getParamConverter(Boolean.class, field.getBaseType(),
			fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Boolean");
	}

	return null;
}
 
Example #8
Source File: DateParamConverterProvider.java    From datawave with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
    if (rawType.equals(Date.class)) {
        DateFormat format = FindAnnotation.findAnnotation(annotations, DateFormat.class);
        return (ParamConverter<T>) new DateFormatter(format);
    }
    return null;
}
 
Example #9
Source File: PortletParamProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Date getDateParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Date> paramConverter = _getParamConverter(Date.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Date");
	}

	return null;
}
 
Example #10
Source File: PortletParamProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Double getDoubleParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Double> paramConverter = _getParamConverter(Double.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Double");
	}

	return null;
}
 
Example #11
Source File: PortletParamProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Float getFloatParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Float> paramConverter = _getParamConverter(Float.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Float");
	}

	return null;
}
 
Example #12
Source File: PortletParamProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Integer getIntegerParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Integer> paramConverter = _getParamConverter(Integer.class, field.getBaseType(),
			fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Integer");
	}

	return null;
}
 
Example #13
Source File: PortletParamProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Long getLongParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Long> paramConverter = _getParamConverter(Long.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Long");
	}

	return Long.valueOf(value);
}
 
Example #14
Source File: PortletParamProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
private <T> ParamConverter _getParamConverter(Class<?> rawType, Type baseType, Annotation[] annotations) {

		for (ParamConverterProvider paramConverterProvider : paramConverterProviders) {

			ParamConverter<T> paramConverter = paramConverterProvider.getConverter((Class<T>) rawType, baseType,
					annotations);

			if (paramConverter != null) {
				return paramConverter;
			}
		}

		return null;
	}
 
Example #15
Source File: InjectionUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static <T> Optional<ParamConverter<T>> getParamConverter(Class<T> pClass,
        Type genericType, Annotation[] anns, Message message) {
    
    if (message != null) {
        ServerProviderFactory pf = ServerProviderFactory.getInstance(message);
        ParamConverter<T> pm = pf.createParameterHandler(pClass, genericType, anns, message);
        return Optional.ofNullable(pm);
    }
    
    return Optional.empty();
}
 
Example #16
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType,
                                          Annotation[] annotations) {
    if (rawType == MyType.class) {
        Type type = ((ParameterizedType)genericType).getActualTypeArguments()[0];
        @SuppressWarnings("unchecked")
        ParamConverter<T> converter = (ParamConverter<T>)this;
        if (type == Integer.class) {
            return converter;
        }
    }
    return null;
}
 
Example #17
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(Class<T> cls, Type arg1, Annotation[] arg2) {
    if (cls == Locale.class) {
        return (ParamConverter<T>)this;
    }
    return null;
}
 
Example #18
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(Class<T> cls, Type arg1, Annotation[] arg2) {
    if (cls == Query.class) {
        return (ParamConverter<T>)this;
    }
    return null;
}
 
Example #19
Source File: CustomerParameterHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(Class<T> cls, Type arg1, Annotation[] arg2) {
    if (Customer.class == cls) {
        return (ParamConverter<T>)this;
    }
    return null;
}
 
Example #20
Source File: ProviderFactoryTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testParameterHandlerProvider() throws Exception {
    ProviderFactory pf = ServerProviderFactory.getInstance();
    ParamConverterProvider h = new CustomerParameterHandler();
    pf.registerUserProvider(h);
    ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class, null,
                                                            new MessageImpl());
    assertSame(h2, h);
}
 
Example #21
Source File: DefaultParamConverterProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType,
        final Annotation[] annotations) {

    for (final Entry<Class<?>, ParamConverter<?>> entry: converters.entrySet()) {
        if (entry.getKey().isAssignableFrom(rawType)) {
            return (ParamConverter<T>)entry.getValue();
        }
    }

    return null;
}
 
Example #22
Source File: ParamConverterUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the instance of particular type into its string-based representation
 * using parameter converter provider and available parameter converter.
 * @param type type to convert to string-based representation
 * @param provider parameter converter provider to use
 * @param value the typed instance to convert to string representation
 * @return string-based representation of the instance of particular type
 */
public static< T > String getString(final Class< T > type, final ParamConverterProvider provider,
        final T value) {

    if (provider != null) {
        final ParamConverter< T > converter = provider.getConverter(type, null, new Annotation[0]);
        if (converter != null) {
            return converter.toString(value);
        }
    }

    return value == null ? null : value.toString();
}
 
Example #23
Source File: RESTService.java    From tomee with Apache License 2.0 5 votes vote down vote up
private static <T> boolean isProvider(final Class<T> clazz) {
    return MessageBodyReader.class.isAssignableFrom(clazz) ||
            MessageBodyWriter.class.isAssignableFrom(clazz) ||
            ParamConverter.class.isAssignableFrom(clazz) ||
            ContainerRequestFilter.class.isAssignableFrom(clazz) ||
            ContainerResponseFilter.class.isAssignableFrom(clazz) ||
            ReaderInterceptor.class.isAssignableFrom(clazz) ||
            WriterInterceptor.class.isAssignableFrom(clazz) ||
            ParamConverterProvider.class.isAssignableFrom(clazz) ||
            ContextResolver.class.isAssignableFrom(clazz) ||
            Feature.class.isAssignableFrom(clazz) ||
            new MetaAnnotatedClass<>(clazz).isAnnotationPresent(Provider.class);
}
 
Example #24
Source File: ModelRefConverterProvider.java    From openscoring with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation annotations[]){

	if((ModelRef.class).equals(rawType)){
		ParamConverter<ModelRef> paramConverter = new ModelRefConverter(getSecurityContext());

		return (ParamConverter)paramConverter;
	}

	return null;
}
 
Example #25
Source File: DateParamConverterProvider.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type baseType, Annotation[] annotations) {

	if (rawType == null) {
		return null;
	}

	if (rawType.equals(Date.class)) {
		return (ParamConverter<T>) new DateParamConverter(portletPreferences);
	}

	return null;
}
 
Example #26
Source File: OffsetDateTimeProvider.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public <T> ParamConverter<T> getConverter(Class<T> clazz, Type type, Annotation[] annotations) {
    if (clazz.getName().equals(OffsetDateTime.class.getName())) {
        return new ParamConverter<T>() {
            @SuppressWarnings("unchecked")
            public T fromString(String value) {
                return value != null ? (T) OffsetDateTime.parse(value) : null;
            }

            public String toString(T bean) {
                return bean != null ? bean.toString() : "";
            }
        };
    }
    return null;
}
 
Example #27
Source File: LocalDateProvider.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public <T> ParamConverter<T> getConverter(Class<T> clazz, Type type, Annotation[] annotations) {
    if (clazz.getName().equals(LocalDate.class.getName())) {
        return new ParamConverter<T>() {
            @SuppressWarnings("unchecked")
            public T fromString(String value) {
                return value!=null ? (T) LocalDate.parse(value) : null;
            }

            public String toString(T bean) {
                return bean!=null ? bean.toString() : "";
            }
        };
    }
    return null;
}
 
Example #28
Source File: MoshiParamConverterFactory.java    From jax-rs-moshi with Apache License 2.0 5 votes vote down vote up
@Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType,
    Annotation[] annotations) {
  for (Annotation annotation : annotations) {
    if (annotation instanceof Json) {
      JsonAdapter<T> adapter = moshi.adapter(genericType);
      return new MoshiParamConverter<>(adapter);
    }
  }
  return null;
}
 
Example #29
Source File: MoshiParamConverterFactoryTest.java    From jax-rs-moshi with Apache License 2.0 5 votes vote down vote up
@Test public void differentJsonAnnotationReturnsNull() {
  ParamConverter<String> converter =
      provider.getConverter(String.class, String.class, new Annotation[] {
          Annotations.other()
      });
  assertNull(converter);
}
 
Example #30
Source File: MoshiParamConverterFactoryTest.java    From jax-rs-moshi with Apache License 2.0 5 votes vote down vote up
@Test public void jsonAnnotationReturnsConverterClass() {
  ParamConverter<String> converter =
      provider.getConverter(String.class, String.class, new Annotation[] {
          Annotations.real()
      });
  String value = converter.fromString("\"hey\"");
  assertEquals("hey", value);
  String json = converter.toString("hey");
  assertEquals("\"hey\"", json);
}