Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * http://glassfish.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 package com.sun.jersey.server.wadl;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 
This class implements the algorithm how the wadl is built for one or more com.sun.jersey.api.model.AbstractResource classes. Wadl artifacts are created by a WadlGenerator. Created on: Jun 18, 2008

Author(s):
Marc Hadley
Martin Grotzke
Version:
$Id$
 
 public class WadlBuilder {
 
     private WadlGenerator _wadlGenerator;
 
     public WadlBuilder() {
         this(new WadlGeneratorJAXBGrammarGenerator());
     }
 
     public WadlBuilder(WadlGenerator wadlGenerator) {
          = wadlGenerator;
     }

    
Generate WADL for a set of resources.

Parameters:
resources the set of resources
Returns:
the JAXB WADL application bean
    public ApplicationDescription generate(Set<AbstractResourceresources) {
        Application wadlApplication = .createApplication();
        Resources wadlResources = .createResources();
        // for each resource
        for (AbstractResource r : resources) {
            Resource wadlResource = generateResource(rnull);
            wadlResources.getResource().add(wadlResource);
        }
        wadlApplication.getResources().add(wadlResources);
        addVersion(wadlApplication);
        
        // Build any external grammars
        
        WadlGenerator.ExternalGrammarDefinition external =
            .createExternalGrammar();
        //
        
        ApplicationDescription description = new ApplicationDescription(wadlApplicationexternal);
        
        // Attach the data to the parts of the model
        
        .attachTypes(description);
         
        // Return the description of the application
        
        
        return description;
    }

    
Generate WADL for a resource.

Parameters:
resource the resource
description the overall application description so we can
Returns:
the JAXB WADL application bean
    public Application generate(
            ApplicationDescription description,
            AbstractResource resource) {
        Application wadlApplication = .createApplication();
        Resources wadlResources = .createResources();
        Resource wadlResource = generateResource(resourcenull);
        wadlResources.getResource().add(wadlResource);
        wadlApplication.getResources().add(wadlResources);
        addVersion(wadlApplication);
        
        // Attach the data to the parts of the model
        
        .attachTypes(description);
        
        // Return the WADL
        
        return wadlApplication;
    }

    
Generate WADL for a virtual subresource resulting from sub resource methods.

Parameters:
description the overall application description so we can
resource the parent resource
path the value of the methods path annotations
Returns:
the JAXB WADL application bean
    public Application generate(
            ApplicationDescription description,
            AbstractResource resourceString path) {
        Application wadlApplication = .createApplication();
        Resources wadlResources = .createResources();
        Resource wadlResource = generateSubResource(resourcepath);
        wadlResources.getResource().add(wadlResource);
        wadlApplication.getResources().add(wadlResources);
        addVersion(wadlApplication);
        
        // Attach the data to the parts of the model
        
        .attachTypes(description);
        
        // Return the WADL
        
        return wadlApplication;
    }
    private void addVersion(Application wadlApplication) {
        // Include Jersey version as doc element with generatedBy attribute
        Doc d = new Doc();
        d.getOtherAttributes().put(new QName("http://jersey.java.net/""generatedBy""jersey"),
                BuildId.getBuildId());
        wadlApplication.getDoc().add(0, d);
    }
    private com.sun.research.ws.wadl.Method generateMethod(AbstractResource rfinal Map<StringParamwadlResourceParamsfinal AbstractResourceMethod m) {
        com.sun.research.ws.wadl.Method wadlMethod = .createMethod(rm);
        // generate the request part
        Request wadlRequest = generateRequest(rmwadlResourceParams);
        if (wadlRequest != null) {
            wadlMethod.setRequest(wadlRequest);
        }
        // generate the response part
        final List<Responseresponses = generateResponses(rm);
        if(responses != null) {
            wadlMethod.getResponse().addAll(responses);
        }
        return wadlMethod;
    }
                                    Map<StringParamwadlResourceParams) {
        if (m.getParameters().isEmpty()) {
            return null;
        }
        Request wadlRequest = .createRequest(rm);
        for (Parameter p : m.getParameters()) {
            if (p.getSource() == ..) {
                for (MediaType mediaType : m.getSupportedInputTypes()) {
                    setRepresentationForMediaType(rmmediaTypewadlRequest);
                }
            } else if (p.getAnnotation().annotationType() == FormParam.class) {
                // Use application/x-www-form-urlencoded if no @Consumes
                List<MediaTypesupportedInputTypes = m.getSupportedInputTypes();
                if (supportedInputTypes.isEmpty()
                        || (supportedInputTypes.size() == 1 && supportedInputTypes.get(0).isWildcardType())) {
                    supportedInputTypes = Collections.singletonList(.);
                }
                for (MediaType mediaType : supportedInputTypes) {
                    final Representation wadlRepresentation = setRepresentationForMediaType(rmmediaTypewadlRequest);
                    if (getParamByName(wadlRepresentation.getParam(), p.getSourceName()) == null) {
                        final Param wadlParam = generateParam(rmp);
                        if (wadlParam != null) {
                            wadlRepresentation.getParam().add(wadlParam);
                        }
                    }
                }
            } else if (p.getAnnotation().annotationType().getName().equals("com.sun.jersey.multipart.FormDataParam")) { // jersey-multipart support
                // Use multipart/form-data if no @Consumes
                List<MediaTypesupportedInputTypes = m.getSupportedInputTypes();
                if (supportedInputTypes.isEmpty()
                        || (supportedInputTypes.size() == 1 && supportedInputTypes.get(0).isWildcardType())) {
                    supportedInputTypes = Collections.singletonList(.);
                }
                for (MediaType mediaType : supportedInputTypes) {
                    final Representation wadlRepresentation = setRepresentationForMediaType(rmmediaTypewadlRequest);
                    if (getParamByName(wadlRepresentation.getParam(), p.getSourceName()) == null) {
                        final Param wadlParam = generateParam(rmp);
                        if (wadlParam != null) {
                            wadlRepresentation.getParam().add(wadlParam);
                        }
                    }
                }
            } else {
                Param wadlParam = generateParam(rmp);
                if (wadlParam == null) {
                    continue;
                }
                if (wadlParam.getStyle() == . || wadlParam.getStyle() == .) {
                    wadlResourceParams.put(wadlParam.getName(), wadlParam);
                } else {
                    wadlRequest.getParam().add(wadlParam);
                }
            }
        }
        if (wadlRequest.getRepresentation().size() + wadlRequest.getParam().size() == 0) {
            return null;
        } else {
            return wadlRequest;
        }
    }
    private Param getParamByName(final List<Paramparamsfinal String name) {
        for (Param param : params) {
            if (param.getName().equals(name)) {
                return param;
            }
        }
        return null;
    }

    
Create the wadl com.sun.research.ws.wadl.Representation for the specified javax.ws.rs.core.MediaType if not yet existing for the wadl com.sun.research.ws.wadl.Request and return it.

Parameters:
r the resource
m the resource method
mediaType an accepted media type of the resource method
wadlRequest the wadl request the wadl representation is to be created for (if not yet existing).
Returns:
the wadl request representation for the specified javax.ws.rs.core.MediaType.
Author(s):
Martin Grotzke
                                                             final AbstractResourceMethod mMediaType mediaType,
                                                             Request wadlRequest) {
        Representation wadlRepresentation = getRepresentationByMediaType(wadlRequest.getRepresentation(), mediaType);
        if (wadlRepresentation == null) {
            wadlRepresentation = .createRequestRepresentation(rmmediaType);
            wadlRequest.getRepresentation().add(wadlRepresentation);
        }
        return wadlRepresentation;
    }
            final List<RepresentationrepresentationsMediaType mediaType) {
        for (Representation representation : representations) {
            if (mediaType.toString().equals(representation.getMediaType())) {
                return representation;
            }
        }
        return null;
    }
    private Param generateParam(AbstractResource rAbstractMethod mfinal Parameter p) {
        if (p.getSource() == .. || p.getSource() == ..) {
            return null;
        }
        Param wadlParam = .createParam(rmp);
        return wadlParam;
    }
    private Resource generateResource(AbstractResource rString path) {
        return generateResource(rpath, Collections.<Class<?>>emptySet());
    }
    private Resource generateResource(AbstractResource rString pathSet<Class<?>> visitedClasses) {
        Resource wadlResource = .createResource(rpath);
        // prevent infinite recursion
        if (visitedClasses.contains(r.getResourceClass())) {
            return wadlResource;
        } else {
            visitedClasses = new HashSet<Class<?>>(visitedClasses);
            visitedClasses.add(r.getResourceClass());
        }
        Map<StringParamwadlResourceParams = new HashMap<StringParam>();
        // add resource field/setter parameters that are associated with the resource PATH template
        List<ParameterizedfieldsOrSetters = new LinkedList<Parameterized>();
        if (r.getFields() != null) {
            fieldsOrSetters.addAll(r.getFields());
        }
        if (r.getSetterMethods() != null) {
            fieldsOrSetters.addAll(r.getSetterMethods());
        }
        for (Parameterized f : fieldsOrSetters) {
            for (Parameter fp : f.getParameters()) {
                Param wadlParam = generateParam(rnullfp);
                if (wadlParam != null) {
                    wadlResource.getParam().add(wadlParam);
                }
            }
        }
        // for each resource method
        for (AbstractResourceMethod m : r.getResourceMethods()) {
            com.sun.research.ws.wadl.Method wadlMethod = generateMethod(rwadlResourceParamsm);
            wadlResource.getMethodOrResource().add(wadlMethod);
        }
        // add method parameters that are associated with the resource PATH template
        for (Param wadlParam : wadlResourceParams.values()) {
            wadlResource.getParam().add(wadlParam);
        }
        // for each sub-resource method
        Map<StringResourcewadlSubResources = new HashMap<StringResource>();
        Map<StringMap<StringParam>> wadlSubResourcesParams =
                new HashMap<StringMap<StringParam>>();
        for (AbstractSubResourceMethod m : r.getSubResourceMethods()) {
            // find or create sub resource for uri template
            String template = m.getPath().getValue();
            Resource wadlSubResource = wadlSubResources.get(template);
            Map<StringParamwadlSubResourceParams = wadlSubResourcesParams.get(template);
            if (wadlSubResource == null) {
                wadlSubResource = new Resource();
                wadlSubResource.setPath(template);
                wadlSubResources.put(templatewadlSubResource);
                wadlSubResourceParams = new HashMap<StringParam>();
                wadlSubResourcesParams.put(templatewadlSubResourceParams);
                wadlResource.getMethodOrResource().add(wadlSubResource);
            }
            com.sun.research.ws.wadl.Method wadlMethod = generateMethod(rwadlSubResourceParamsm);
            wadlSubResource.getMethodOrResource().add(wadlMethod);
        }
        // add parameters that are associated with each sub-resource method PATH template
        for (Map.Entry<StringResourcee : wadlSubResources.entrySet()) {
            String template = e.getKey();
            Resource wadlSubResource = e.getValue();
            Map<StringParamwadlSubResourceParams = wadlSubResourcesParams.get(template);
            for (Param wadlParam : wadlSubResourceParams.values()) {
                wadlSubResource.getParam().add(wadlParam);
            }
        }
        // for each sub resource locator
        for (AbstractSubResourceLocator l : r.getSubResourceLocators()) {
            AbstractResource subResource = IntrospectionModeller.createResource(
                    l.getMethod().getReturnType());
            Resource wadlSubResource = generateResource(subResource,
                    l.getPath().getValue(), visitedClasses);
            wadlResource.getMethodOrResource().add(wadlSubResource);
            for (Parameter p : l.getParameters()) {
                Param wadlParam = generateParam(rlp);
                if (wadlParam != null && wadlParam.getStyle() == .) {
                    wadlSubResource.getParam().add(wadlParam);
                }
            }
        }
        return wadlResource;
    }
    private Resource generateSubResource(AbstractResource rString path) {
        Resource wadlResource = new Resource();
        if (r.isRootResource()) {
            StringBuilder b = new StringBuilder(r.getPath().getValue());
            if (!(r.getPath().getValue().endsWith("/") || path.startsWith("/"))) {
                b.append("/");
            }
            b.append(path);
            wadlResource.setPath(b.toString());
        }
        // for each sub-resource method
        Map<StringParamwadlSubResourceParams = new HashMap<StringParam>();
        for (AbstractSubResourceMethod m : r.getSubResourceMethods()) {
            // find or create sub resource for uri template
            String template = m.getPath().getValue();
            if (!template.equals(path)) {
                continue;
            }
            com.sun.research.ws.wadl.Method wadlMethod = generateMethod(rwadlSubResourceParamsm);
            wadlResource.getMethodOrResource().add(wadlMethod);
        }
        // add parameters that are associated with each sub-resource method PATH template
        for (Param wadlParam : wadlSubResourceParams.values()) {
            wadlResource.getParam().add(wadlParam);
        }
        return wadlResource;
    }
        if (m.getMethod().getReturnType() == void.class) {
            return null;
        }
        return .createResponses(rm);
    }
New to GrepCode? Check out our FAQ X