private <T> T parseParameter(String val, ClassMeta<T> c) throws ParseException { if (val == null) return null; // Shortcut - If we're returning a string and the value doesn't start with '$' or '(', then // just return the string since it's a plain value. if (c.getInnerClass() == String.class && val.length() > 0) { char x = val.charAt(0); if (x != '(' && x != '$' && x != '\u0000' && val.indexOf('~') == -1) return (T)val; } return urlEncodingParser.parseParameter(val, c); }
/** * Returns the specified path parameter converted to a POJO. * <p> * The type can be any POJO type convertable from a <code>String</code> (See <a class='doclink' href='package-summary.html#PojosConvertableFromString'>POJOs Convertable From Strings</a>). * * @param name The attribute name. * @param cm The class type to convert the attribute value to. * @param <T> The class type to convert the attribute value to. * @return The attribute value converted to the specified class type. * @throws ParseException */ public <T> T getPathParameter(String name, ClassMeta<T> cm) throws ParseException { Object attr = getPathParameter(name); T t = null; if (attr != null) t = urlEncodingParser.parseParameter(attr.toString(), cm); if (t == null && cm.isPrimitive()) return cm.getPrimitiveDefault(); return t; }
/** * Constructor. */ RestResponse(RestServlet servlet, RestRequest req, HttpServletResponse res) { super(res); this.request = req; this.servlet = servlet; for (Map.Entry<String,Object> e : servlet.getDefaultResponseHeaders().entrySet()) setHeader(e.getKey(), e.getValue().toString()); try { String passThroughHeaders = req.getHeader("x-response-headers"); if (passThroughHeaders != null) { ObjectMap m = servlet.getUrlEncodingParser().parseParameter(passThroughHeaders, ObjectMap.class); for (Map.Entry<String,Object> e : m.entrySet()) setHeader(e.getKey(), e.getValue().toString()); } } catch (Exception e1) { throw new RestException(SC_BAD_REQUEST, "Invalid format for header 'x-response-headers'. Must be in URL-encoded format.").initCause(e1); } }