Java Code Examples for javax.ws.rs.ext.ParamConverter#fromString()

The following examples show how to use javax.ws.rs.ext.ParamConverter#fromString() . 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: 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 2
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);
}
 
Example 3
Source File: MoshiParamConverterFactoryTest.java    From jax-rs-moshi with Apache License 2.0 5 votes vote down vote up
@Test public void jsonAnnotationReturnsConverterParameterized() {
  Type genericType = Types.newParameterizedType(List.class, String.class);
  ParamConverter<List<String>> converter =
      (ParamConverter) provider.getConverter(List.class, genericType, new Annotation[] {
          Annotations.real()
      });
  List<String> value = converter.fromString("[\"hey\"]");
  assertEquals(singletonList("hey"), value);
  String json = converter.toString(singletonList("hey"));
  assertEquals("[\"hey\"]", json);
}
 
Example 4
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 5
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 6
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 7
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 8
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 9
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 10
Source File: JavaTimeTypesParamConverterProviderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void invalidLocalDate() {
    ParamConverter<LocalDate> converter =
            provider.getConverter(LocalDate.class, LocalDate.class, null);
    converter.fromString("invalid");
}