/** * Creates the class-level guards associated with this servlet. * <p> * Subclasses can override this method to provide their own class-level guards for this servlet. * <p> * By default, returns the guards specified through the {@link RestResource#guards() @RestResource.guards()} annotation in child-to-parent order. * (i.e. guards on children will be called before guards on parents). * * @param properties Servlet-level properties returned by {@link #createProperties()}. * @return The new set of guards associated with this servet. * @throws RestServletException */ protected RestGuard[] createGuards(ObjectMap properties) throws RestServletException { List<RestGuard> l = new LinkedList<RestGuard>(); // Guards are loaded in child-to-parent order. for (RestResource r : restResourceAnnotationsChildFirst.values()) for (Class<? extends RestGuard> c : reverse(r.guards())) try { l.add(c.newInstance()); } catch (Exception e) { throw new RestServletException("Exception occurred while trying to instantiate RestGuard ''{0}''", c.getSimpleName()).initCause(e); } return l.toArray(new RestGuard[l.size()]); }
@Test public void testReverse() throws Exception { String[] s = null; assertNull(reverse(s)); s = new String[]{}; assertObjectEquals("[]", reverse(s)); s = new String[]{"a"}; assertObjectEquals("['a']", reverse(s)); s = new String[]{"a","b"}; assertObjectEquals("['b','a']", reverse(s)); s = new String[]{"a","b","c"}; assertObjectEquals("['c','b','a']", reverse(s)); }
/** * Creates the {@link EncoderGroup} for this servlet for handling various encoding schemes. * <p> * Subclasses can override this method to provide their own encoder group, typically by * appending to the group returned by <code><jk>super</jk>.createEncoders()</code>. * <p> * By default, returns a group containing {@link IdentityEncoder#INSTANCE} and all encoders * specified through {@link RestResource#encoders() @RestResource.encoders()} annotations in parent-to-child order. * * @param properties Servlet-level properties returned by {@link #createProperties()}. * @return The new encoder group associated with this servet. * @throws RestServletException */ protected EncoderGroup createEncoders(ObjectMap properties) throws RestServletException { EncoderGroup g = new EncoderGroup().append(IdentityEncoder.INSTANCE); // Encoders are loaded in parent-to-child order to allow overrides. for (RestResource r : restResourceAnnotationsParentFirst.values()) for (Class<? extends Encoder> c : reverse(r.encoders())) try { g.append(c); } catch (Exception e) { throw new RestServletException("Exception occurred while trying to instantiate Encoder ''{0}''", c.getSimpleName()).initCause(e); } return g; }
/** * Creates the parser group containing parsers used for parsing input into POJOs from HTTP requests. * <p> * Subclasses can override this method to provide their own set of parsers for this servlet. * They can do this by either creating a new {@link ParserGroup} from scratch, or appending to the * group returned by <code><jk>super</jk>.createParsers()</code>. * <p> * By default, returns the parsers defined through {@link RestResource#parsers() @RestResource.parsers()} on this class * and all parent classes. * * @param properties Servlet-level properties returned by {@link #createProperties()}. * @param beanFilters Servlet-level bean filters returned by {@link #createBeanFilters()}. * @param pojoSwaps Servlet-level POJO swaps returned by {@link #createPojoSwaps()}. * @return The group of parsers. * @throws Exception If parser group could not be constructed for any reason. */ protected ParserGroup createParsers(ObjectMap properties, Class<?>[] beanFilters, Class<?>[] pojoSwaps) throws Exception { ParserGroup g = new ParserGroup(); // Parsers are loaded in parent-to-child order to allow overrides. for (RestResource r : restResourceAnnotationsParentFirst.values()) for (Class<? extends Parser> p : reverse(r.parsers())) try { g.append(p); } catch (Exception e) { throw new RestServletException("Exception occurred while trying to instantiate Parser ''{0}''", p.getSimpleName()).initCause(e); } g.setProperties(properties); g.addBeanFilters(beanFilters).addPojoSwaps(pojoSwaps); return g; }
/** * Creates the serializer group containing serializers used for serializing output POJOs in HTTP responses. * <p> * Subclasses can override this method to provide their own set of serializers for this servlet. * They can do this by either creating a new {@link SerializerGroup} from scratch, or appending to the * group returned by <code><jk>super</jk>.createSerializers()</code>. * <p> * By default, returns the serializers defined through {@link RestResource#serializers() @RestResource.serializers()} on this class * and all parent classes. * * @param properties Servlet-level properties returned by {@link #createProperties()}. * @param beanFilters Servlet-level bean filters returned by {@link #createBeanFilters()}. * @param pojoSwaps Servlet-level POJO swaps returned by {@link #createPojoSwaps()}. * @return The group of serializers. * @throws Exception If serializer group could not be constructed for any reason. */ protected SerializerGroup createSerializers(ObjectMap properties, Class<?>[] beanFilters, Class<?>[] pojoSwaps) throws Exception { SerializerGroup g = new SerializerGroup(); // Serializers are loaded in parent-to-child order to allow overrides. for (RestResource r : restResourceAnnotationsParentFirst.values()) for (Class<? extends Serializer> c : reverse(r.serializers())) try { g.append(c); } catch (Exception e) { throw new RestServletException("Exception occurred while trying to instantiate Serializer ''{0}''", c.getSimpleName()).initCause(e); } g.setProperties(properties); g.addBeanFilters(beanFilters).addPojoSwaps(pojoSwaps); return g; }
responseHandlers(r.responseHandlers()); converters(r.converters()); guards(reverse(r.guards())); children(r.children()); beanFilters(false, merge(ObjectUtils.toType(psb.peek(BEAN_beanFilters), Object[].class), r.beanFilters()));
responseHandlers(r.responseHandlers()); converters(r.converters()); guards(reverse(r.guards())); children(r.children()); beanFilters(false, merge(ObjectUtils.toType(psb.peek(BEAN_beanFilters), Object[].class), r.beanFilters()));
responseHandlers(r.responseHandlers()); converters(r.converters()); guards(reverse(r.guards())); children(r.children()); beanFilters(false, merge(ObjectUtils.toType(psb.peek(BEAN_beanFilters), Object[].class), r.beanFilters()));