public static ValueConvertor createValueConvertor(Class<?> type, Type genericType) { if (type.isArray()) { return createArrayValueConvertor(type.getComponentType(), genericType); } return createConcreteValueConvertor(type, genericType); }
protected BoundInjectable(ParamType paramType, String name, Class<?> type, Type genericType, Annotation[] annotations, Member member) { super(paramType, type, genericType, annotations, member); this.name = name; this.convertor = ValueConvertor.createValueConvertor(type, genericType); this.defaultValue = null; this.encoded = false; }
public static ValueConvertor createConcreteValueConvertor(Class<?> classType, Type genericType) { // if (classType.equals(List.class) // && // PathSegment.class.equals(GenericsUtils.getGenericParamType(genericType))) // { // return new DummyConvertor(); // } else if (classType.equals(List.class)) { return new ListConvertor(getSingleValueConvertor(GenericsUtils .getGenericParamType(genericType))); } else if (classType.equals(SortedSet.class)) { return new SortedSetConvertor(getSingleValueConvertor(GenericsUtils .getGenericParamType(genericType))); } else if (classType.equals(Set.class)) { return new SetConvertor(getSingleValueConvertor(GenericsUtils .getGenericParamType(genericType))); } else if (classType.isEnum()) { return getEnumValueConvertor(classType); } else { return getSingleValueConvertor(classType); } }
public ArrayValueConvertor(ValueConvertor concrete, Class<?> type) { this.concrete = concrete; this.type = concrete.getConcreteType(type); }
private static ValueConvertor createArrayValueConvertor(Class<?> componentType, Type genericType) { ValueConvertor concreteConvertor = createConcreteValueConvertor(componentType, genericType); return new ArrayValueConvertor(concreteConvertor, componentType); }
private static ValueConvertor getSingleValueConvertor(Class<?> classType) { if (classType.equals(String.class)) { return new StringConvertor(); } else if (classType.equals(Character.class)) { return new CharacterConvertor(); } else if (classType.isPrimitive()) { return new PrimitiveConvertor(classType); } else if (classType.equals(PathSegment.class)) { return new PathSegmentConvertor(); } else { return getComplexValueConverter(classType); } }
protected Collection<Object> convertCollection(List<String> values, Collection<Object> out) { for (String string : values) { out.add(converter.convert(string)); } return out; } }
public static ValueConvertor createValueConvertor(Class<?> type) { return createValueConvertor(type, null); }
@Override public Object convert(String value) throws WebApplicationException { Object[] array = (Object[])Array.newInstance(type, 1); array[0] = concrete.convert(value); return null; }
@Override public Object convert(List<String> value) throws WebApplicationException { if (value == null || value.size() == 0) { return Array.newInstance(type, 0); } Object array = Array.newInstance(type, value.size()); for (int i = 0; i < value.size(); ++i) { Array.set(array, i, concrete.convert(value.get(i))); } return array; } }
@Override public Object getValue(RuntimeContext runtimeContext) throws IOException { if (runtimeContext == null) { return null; } UriInfo uriInfo = runtimeContext.getUriInfo(); MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters(false); List<String> values = queryParameters.get(getName()); if (values == null) { values = new LinkedList<String>(); } if (values.size() == 0 && hasDefaultValue()) { values.add(getDefaultValue()); } decodeValues(values); // we found query parameter values with the specified name try { return getConvertor().convert(values); } catch (ConversionException e) { throw new WebApplicationException(e.getCause(), Response.Status.NOT_FOUND); } }
@Override public Object getValue(RuntimeContext runtimeContext) throws IOException { if (runtimeContext == null) { return null; } List<String> allValues = new ArrayList<String>(); List<PathSegment> segments = runtimeContext.getAttribute(SearchResult.class).getData().getMatchedURIs().get(0); // get the matrix parameter only from the last segment PathSegment segment = segments.get(segments.size() - 1); MultivaluedMap<String, String> matrixParameters = segment.getMatrixParameters(); List<String> values = matrixParameters.get(getName()); if (values != null) { allValues.addAll(values); } if (allValues.size() == 0 && hasDefaultValue()) { allValues.add(getDefaultValue()); } decodeValues(allValues); // we found matrix parameters with the specified name try { return getConvertor().convert(allValues); } catch (ConversionException e) { throw new WebApplicationException(e.getCause(), Response.Status.NOT_FOUND); } } }
@Override public Object getValue(RuntimeContext runtimeContext) throws IOException { if (runtimeContext == null) { return null; } // for all headers HttpHeaders httpHeaders = runtimeContext.getHttpHeaders(); List<String> values = httpHeaders.getRequestHeader(getName()); if (values == null) { values = new LinkedList<String>(); } if (values.size() == 0 && hasDefaultValue()) { values.add(getDefaultValue()); } try { return getConvertor().convert(values); } catch (ConversionException e) { logger.error(Messages.getMessage("conversionError", this, values), e); throw new WebApplicationException(e.getCause(), Response.Status.BAD_REQUEST); } } }
return getConvertor().convert(values); } catch (ConversionException e) {
return getConvertor().convert(value); } catch (ConversionException e) { logger.error(Messages.getMessage("conversionError", this, value), e);