/** * Here we will modify serializer such that it has two modes: default handling when no JsonView * is enabled; and other (custom) when viess are enabled. Note that we could also just have * forced serialization for all cases. */ @Override protected void processViews(SerializationConfig config, BeanSerializerBuilder builder) { // Let's use default serializer modification as the baseline super.processViews(config, builder); /* And only change handling of that one bean (more likely, * you would want to handle all classes in a package, or with * some name -- this would be less work than having separate * custom serializer for all classes) */ BasicBeanDescription beanDesc = builder.getBeanDescription(); if (beanDesc.getBeanClass() == Bean.class) { List<BeanPropertyWriter> props = builder.getProperties(); BeanPropertyWriter[] writers = props.toArray(new BeanPropertyWriter[props.size()]); for (int i = 0; i < writers.length; ++i) { String pname = writers[i].getName(); if ("secret".equals(pname)) { // remove serializer, filters it out writers[i] = null; } else if ("name".equals(pname)) { // This one we'll just upper case for fun writers[i] = new UpperCasingWriter(writers[i]); } } // Important: update builder with filtered property definitions builder.setFilteredProperties(writers); } }
/** * Set a new cookie * * @param name Cookie name * @param value Cookie value * @param maxAge Cookie duration (-1 for a transient cookie and 0 for a cookie that expires now) * @param path Cookie path * @param domain Cookie domain * @param secure Whether the cookie is secured (for HTTPS requests) * @param httpOnly Whether the cookie is HTTP only (i.e. not accessible from client-side * JavaScript code) */ public void setCookie( String name, String value, int maxAge, String path, String domain, boolean secure, boolean httpOnly) { cookies.add(new Cookie(name, value, maxAge, path, domain, secure, httpOnly)); }
private Object success(Class<?> clazz, HttpInputMessage inputMessage) throws JsonParseException, IOException { // Note, parsing code used from ektorp project JsonParser jp = objectMapper.getJsonFactory().createJsonParser(inputMessage.getBody()); if (jp.nextToken() != JsonToken.START_OBJECT) { throw new RuntimeException("Expected data to start with an Object"); } Map<String, Integer> fields = readHeaderFields(jp); List result; if (fields.containsKey(TOTAL_ROWS_FIELD_NAME)) { int totalRows = fields.get(TOTAL_ROWS_FIELD_NAME); if (totalRows == 0) { return Collections.emptyList(); } result = new ArrayList(totalRows); } else { result = new ArrayList(); } ParseState state = new ParseState(); Object first = parseFirstRow(jp, state, clazz); if (first == null) { return Collections.emptyList(); } else { result.add(first); } while (jp.getCurrentToken() != null) { skipToField(jp, state.docFieldName, state); if (atEndOfRows(jp)) { return result; } result.add(jp.readValueAs(clazz)); endRow(jp, state); } return result; }
/** * Discard cookies along this result<br> * For example: * * <pre> * response().discardCookies("theme"); * </pre> * * @param names Names of the cookies to discard */ public void discardCookies(String... names) { discardedCookies.addAll(Arrays.asList(names)); }