Esempio n. 1
0
  /**
   * @param rmd see {@link ResourceMethodDescriptor}
   * @param wadlResourceParams for adding parameters which must be in parent
   * @return {@link org.exoplatform.services.rest.wadl.research.Request}
   */
  private org.exoplatform.services.rest.wadl.research.Request processRequest(
      ResourceMethodDescriptor rmd, Map<String, Param> wadlResourceParams) {
    org.exoplatform.services.rest.wadl.research.Request wadlRequest = wadlGenerator.createRequest();
    for (MethodParameter methodParameter : rmd.getMethodParameters()) {
      if (methodParameter.getAnnotation() == null) {
        for (MediaType mediaType : rmd.consumes()) {
          RepresentationType wadlRepresentation =
              wadlGenerator.createRequestRepresentation(mediaType);
          wadlRequest.getRepresentation().add(wadlRepresentation);
        }
      }
      Param wadlParam = processParam(methodParameter);
      if (wadlParam != null) {
        if (wadlParam.getStyle() == ParamStyle.QUERY || wadlParam.getStyle() == ParamStyle.HEADER
        /* || wadlParam.getStyle() == ParamStyle.MATRIX */ ) {
          wadlRequest.getParam().add(wadlParam);
        } else {
          // If matrix or path template parameter then add in map for add in
          // parent element
          wadlResourceParams.put(wadlParam.getName(), wadlParam);
        }
      }
    }

    // NOTE If there are no any representation and parameters then request is
    // null.
    return wadlRequest.getRepresentation().isEmpty() && wadlRequest.getParam().isEmpty()
        ? null
        : wadlRequest;
  }
Esempio n. 2
0
 @Test
 public void testMultipleRootResources() throws Exception {
   WadlGenerator wg = new WadlGenerator();
   wg.setDefaultMediaType(WadlGenerator.WADL_TYPE.toString());
   ClassResourceInfo cri1 =
       ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
   ClassResourceInfo cri2 =
       ResourceUtils.createClassResourceInfo(Orders.class, Orders.class, true, true);
   List<ClassResourceInfo> cris = new ArrayList<ClassResourceInfo>();
   cris.add(cri1);
   cris.add(cri2);
   Message m = mockMessage("http://localhost:8080/baz", "", WadlGenerator.WADL_QUERY, cris);
   Response r = handleRequest(wg, m);
   assertEquals(
       WadlGenerator.WADL_TYPE.toString(),
       r.getMetadata().getFirst(HttpHeaders.CONTENT_TYPE).toString());
   String wadl = r.getEntity().toString();
   Document doc = StaxUtils.read(new StringReader(wadl));
   checkGrammars(
       doc.getDocumentElement(), "thebook", "books", "thebook2s", "thebook2", "thechapter");
   List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 2);
   checkBookStoreInfo(els.get(0), "prefix1:thebook", "prefix1:thebook2", "prefix1:thechapter");
   Element orderResource = els.get(1);
   assertEquals("/orders", orderResource.getAttribute("path"));
 }
Esempio n. 3
0
  @Test
  public void testCustomSchemaWithImportJaxbContextPrefixes() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setSchemaLocations(Collections.singletonList("classpath:/books.xsd"));

    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/bar", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    List<Element> grammarEls =
        DOMUtils.getChildrenWithName(doc.getDocumentElement(), WadlGenerator.WADL_NS, "grammars");
    assertEquals(1, grammarEls.size());
    List<Element> schemasEls =
        DOMUtils.getChildrenWithName(grammarEls.get(0), Constants.URI_2001_SCHEMA_XSD, "schema");
    assertEquals(1, schemasEls.size());
    assertEquals("http://books", schemasEls.get(0).getAttribute("targetNamespace"));
    List<Element> elementEls =
        DOMUtils.getChildrenWithName(schemasEls.get(0), Constants.URI_2001_SCHEMA_XSD, "element");
    assertEquals(1, elementEls.size());
    assertTrue(checkElement(elementEls, "books", "books"));

    List<Element> complexTypesEls =
        DOMUtils.getChildrenWithName(
            schemasEls.get(0), Constants.URI_2001_SCHEMA_XSD, "complexType");
    assertEquals(1, complexTypesEls.size());
    assertTrue(checkComplexType(complexTypesEls, "books"));

    List<Element> importEls =
        DOMUtils.getChildrenWithName(schemasEls.get(0), Constants.URI_2001_SCHEMA_XSD, "import");
    assertEquals(1, importEls.size());
    assertEquals(
        "http://localhost:8080/baz/book1.xsd", importEls.get(0).getAttribute("schemaLocation"));
  }
Esempio n. 4
0
 @Test
 public void testTwoSchemasSameNs() throws Exception {
   WadlGenerator wg = new WadlGenerator();
   wg.setApplicationTitle("My Application");
   wg.setNamespacePrefix("ns");
   ClassResourceInfo cri =
       ResourceUtils.createClassResourceInfo(TestResource.class, TestResource.class, true, true);
   Message m = mockMessage("http://localhost:8080/baz", "/", WadlGenerator.WADL_QUERY, cri);
   Response r = handleRequest(wg, m);
   checkResponse(r);
   Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
   checkDocs(doc.getDocumentElement(), "My Application", "", "");
   List<Element> grammarEls =
       DOMUtils.getChildrenWithName(doc.getDocumentElement(), WadlGenerator.WADL_NS, "grammars");
   assertEquals(1, grammarEls.size());
   List<Element> schemasEls =
       DOMUtils.getChildrenWithName(grammarEls.get(0), Constants.URI_2001_SCHEMA_XSD, "schema");
   assertEquals(2, schemasEls.size());
   assertEquals("http://example.com/test", schemasEls.get(0).getAttribute("targetNamespace"));
   assertEquals("http://example.com/test", schemasEls.get(1).getAttribute("targetNamespace"));
   List<Element> reps =
       DOMUtils.findAllElementsByTagNameNS(
           doc.getDocumentElement(), WadlGenerator.WADL_NS, "representation");
   assertEquals(2, reps.size());
   assertEquals("ns1:testCompositeObject", reps.get(0).getAttribute("element"));
   assertEquals("ns1:testCompositeObject", reps.get(1).getAttribute("element"));
 }
Esempio n. 5
0
  /**
   * Process {@link AbstractResourceDescriptor} for build its WADL representation.
   *
   * @param resourceDescriptor see {@link AbstractResourceDescriptor}
   * @param baseURI base URI of resource, e. g. servlet context
   * @return {@link Application}
   */
  public Application process(AbstractResourceDescriptor resourceDescriptor, URI baseURI) {
    // Root component of WADL representation
    Application wadlApp = wadlGenerator.createApplication();
    // Container for resources
    Resources wadlResources = wadlGenerator.createResources();
    if (baseURI != null) wadlResources.setBase(baseURI.toString());

    org.exoplatform.services.rest.wadl.research.Resource wadlResource =
        processResource(resourceDescriptor);
    wadlResources.getResource().add(wadlResource);

    wadlApp.setResources(wadlResources);
    return wadlApp;
  }
Esempio n. 6
0
  /**
   * @param resourceDescriptor see {@link AbstractResourceDescriptor}
   * @return see {@link WadlGenerator#createResponse()}
   */
  private org.exoplatform.services.rest.wadl.research.Resource processResource(
      AbstractResourceDescriptor resourceDescriptor) {
    org.exoplatform.services.rest.wadl.research.Resource wadlResource =
        wadlGenerator.createResource(resourceDescriptor);

    // Keeps common parameters for resource.
    Map<String, Param> wadlResourceParams = new HashMap<String, Param>();

    ResourceMethodMap<ResourceMethodDescriptor> resourceMethods =
        resourceDescriptor.getResourceMethods();
    for (List<ResourceMethodDescriptor> l : resourceMethods.values()) {
      for (ResourceMethodDescriptor rmd : l) {
        org.exoplatform.services.rest.wadl.research.Method wadlMethod =
            processMethod(rmd, wadlResourceParams);
        if (wadlMethod == null) continue;
        wadlResource.getMethodOrResource().add(wadlMethod);
      }
    }

    // Add parameters to a resource
    for (Param p : wadlResourceParams.values()) wadlResource.getParam().add(p);

    processSubResourceMethods(wadlResource, resourceDescriptor);

    processSubResourceLocators(wadlResource, resourceDescriptor);

    return wadlResource;
  }
Esempio n. 7
0
  @Test
  public void testCustomSchemaJaxbContextPrefixes() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setSchemaLocations(Collections.singletonList("classpath:/book1.xsd"));

    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m =
        mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkGrammars(doc.getDocumentElement(), "thebook", "thebook2", "thechapter");
    List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
    checkBookStoreInfo(els.get(0), "prefix1:thebook", "prefix1:thebook2", "prefix1:thechapter");
  }
Esempio n. 8
0
  @Test
  public void testExternalSchemaCustomPrefix() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setExternalLinks(Collections.singletonList("http://books"));
    wg.setUseJaxbContextForQnames(false);

    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m =
        mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkGrammarsWithLinks(doc.getDocumentElement(), Collections.singletonList("http://books"));
    List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
    checkBookStoreInfo(els.get(0), "p1:thesuperbook", "p1:thesuperbook2", "p1:thesuperchapter");
  }
Esempio n. 9
0
 @Test
 public void testSingleRootResource() throws Exception {
   WadlGenerator wg = new WadlGenerator();
   wg.setApplicationTitle("My Application");
   wg.setNamespacePrefix("ns");
   ClassResourceInfo cri =
       ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
   Message m =
       mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
   Response r = handleRequest(wg, m);
   checkResponse(r);
   Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
   checkDocs(doc.getDocumentElement(), "My Application", "", "");
   checkGrammars(
       doc.getDocumentElement(), "thebook", "books", "thebook2s", "thebook2", "thechapter");
   List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
   checkBookStoreInfo(els.get(0), "ns1:thebook", "ns1:thebook2", "ns1:thechapter", "ns1:books");
 }
Esempio n. 10
0
  /**
   * @param methodParameter see {@link MethodParameter}
   * @return {@link Param}
   */
  private Param processParam(MethodParameter methodParameter) {
    Param wadlParam = null;
    // Skip parameters without annotation (entity) and parameters with javax.ws.rs.core.Context.
    // Context parameter dependent of environment and not used in WADL representation
    if (methodParameter.getAnnotation() != null
        && methodParameter.getAnnotation().annotationType() != javax.ws.rs.core.Context.class)
      wadlParam = wadlGenerator.createParam(methodParameter);

    return wadlParam;
  }
Esempio n. 11
0
  /**
   * Process sub-resource methods.
   *
   * @param wadlResource see {@link org.exoplatform.services.rest.wadl.research.Resource}
   * @param resourceDescriptor see {@link AbstractResourceDescriptor}
   */
  private void processSubResourceMethods(
      org.exoplatform.services.rest.wadl.research.Resource wadlResource,
      AbstractResourceDescriptor resourceDescriptor) {

    // Keeps common parameter for sub-resource.
    Map<String, Map<String, Param>> wadlCommonSubResourceParams =
        new HashMap<String, Map<String, Param>>();
    // Mapping resource path to resource.
    Map<String, org.exoplatform.services.rest.wadl.research.Resource> wadlSubResources =
        new HashMap<String, org.exoplatform.services.rest.wadl.research.Resource>();

    SubResourceMethodMap subresourceMethods = resourceDescriptor.getSubResourceMethods();
    for (ResourceMethodMap<SubResourceMethodDescriptor> rmm : subresourceMethods.values()) {
      for (List<SubResourceMethodDescriptor> l : rmm.values()) {
        for (SubResourceMethodDescriptor srmd : l) {
          String path = srmd.getPathValue().getPath();
          org.exoplatform.services.rest.wadl.research.Resource wadlSubResource =
              wadlSubResources.get(path);
          // There is no any resource for 'path' yet.
          if (wadlSubResource == null) {
            wadlSubResource = wadlGenerator.createResource(path);
            Map<String, Param> wadlResourceParams = new HashMap<String, Param>();
            org.exoplatform.services.rest.wadl.research.Method wadlMethod =
                processMethod(srmd, wadlResourceParams);
            if (wadlMethod == null) continue;
            wadlSubResource.getMethodOrResource().add(wadlMethod);
            // Remember sub-resource and parameters.
            wadlSubResources.put(path, wadlSubResource);
            wadlCommonSubResourceParams.put(path, wadlResourceParams);
          } else {
            // Get parameters for sub-resource that was created by one of previous
            // iteration.
            Map<String, Param> wadlResourceParams = wadlCommonSubResourceParams.get(path);
            // Add new method.
            org.exoplatform.services.rest.wadl.research.Method wadlMethod =
                processMethod(srmd, wadlResourceParams);
            if (wadlMethod == null) continue;
            wadlSubResource.getMethodOrResource().add(wadlMethod);
          }
        }
      }
    }
    // Add sub-resources to the root resource.
    for (Map.Entry<String, org.exoplatform.services.rest.wadl.research.Resource> entry :
        wadlSubResources.entrySet()) {
      String path = entry.getKey();
      org.exoplatform.services.rest.wadl.research.Resource wadlSubResource = entry.getValue();

      for (Param wadlSubParam : wadlCommonSubResourceParams.get(path).values())
        wadlSubResource.getParam().add(wadlSubParam);

      wadlResource.getMethodOrResource().add(wadlSubResource);
    }
  }
Esempio n. 12
0
  /**
   * @param rmd see {@link ResourceMethodDescriptor}
   * @return {@link org.exoplatform.services.rest.wadl.research.Response}
   */
  private org.exoplatform.services.rest.wadl.research.Response processResponse(
      ResourceMethodDescriptor rmd) {
    org.exoplatform.services.rest.wadl.research.Response wadlResponse = null;
    if (rmd.getResponseType() != void.class) {
      wadlResponse = wadlGenerator.createResponse();
      for (MediaType mediaType : rmd.produces()) {
        RepresentationType wadlRepresentation =
            wadlGenerator.createResponseRepresentation(mediaType);
        // Element can represent normal response or fault response
        JAXBElement<RepresentationType> wadlRepresentationElement =
            new JAXBElement<RepresentationType>(
                new QName("http://research.sun.com/wadl/2006/10", "representation"),
                RepresentationType.class,
                wadlRepresentation);

        wadlResponse.getRepresentationOrFault().add(wadlRepresentationElement);
      }
    }

    return wadlResponse;
  }
Esempio n. 13
0
  /**
   * @param rmd see {@link ResourceMethodDescriptor}
   * @param wadlResourceParams for adding parameters which must be in parent
   * @return {@link org.exoplatform.services.rest.wadl.research.Method}
   */
  private org.exoplatform.services.rest.wadl.research.Method processMethod(
      ResourceMethodDescriptor rmd, Map<String, Param> wadlResourceParams) {
    org.exoplatform.services.rest.wadl.research.Method wadlMethod = wadlGenerator.createMethod(rmd);
    // See description of this in
    // BaseWadlGeneratorImpl.createMethod(ResourceMethodDescriptor)
    if (wadlMethod == null) return null;

    org.exoplatform.services.rest.wadl.research.Request wadlRequest =
        processRequest(rmd, wadlResourceParams);
    if (wadlRequest != null) wadlMethod.setRequest(wadlRequest);

    org.exoplatform.services.rest.wadl.research.Response wadlResponse = processResponse(rmd);
    if (wadlResponse != null) wadlMethod.setResponse(wadlResponse);

    return wadlMethod;
  }
Esempio n. 14
0
 private Response handleRequest(WadlGenerator wg, Message m) {
   wg.doFilter(new ContainerRequestContextImpl(m, true, false), m);
   return m.getExchange().get(Response.class);
 }