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;
}
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;
}
}
Aggregations