Search in sources :

Example 1 with ClassDocType

use of org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType in project jersey by jersey.

the class WadlGeneratorResourceDocSupportTest method wadlIsGeneratedWithUnknownCustomParameterAnnotation.

@Test
public void wadlIsGeneratedWithUnknownCustomParameterAnnotation() throws JAXBException {
    /* Set up a ClassDocType that has something for a custom-annotated parameter */
    ClassDocType cdt = new ClassDocType();
    cdt.setClassName(TestResource.class.getName());
    MethodDocType mdt = new MethodDocType();
    mdt.setMethodName("method");
    cdt.getMethodDocs().add(mdt);
    ParamDocType pdt = new ParamDocType("x", "comment about x");
    mdt.getParamDocs().add(pdt);
    AnnotationDocType adt = new AnnotationDocType();
    adt.setAnnotationTypeName(CustomParam.class.getName());
    adt.getAttributeDocs().add(new NamedValueType("value", "x"));
    pdt.getAnnotationDocs().add(adt);
    ResourceDocType rdt = new ResourceDocType();
    rdt.getDocs().add(cdt);
    /* Generate WADL for that class */
    WadlGenerator wg = new WadlGeneratorResourceDocSupport(new WadlGeneratorImpl(), rdt);
    WadlBuilder wb = new WadlBuilder(wg, false, null);
    Resource resource = Resource.from(TestResource.class);
    ApplicationDescription app = wb.generate(Collections.singletonList(resource));
    /* Confirm that it can be marshalled without error */
    StringWriter sw = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(Application.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(app.getApplication(), sw);
}
Also used : WadlGeneratorImpl(org.glassfish.jersey.server.wadl.internal.WadlGeneratorImpl) Marshaller(javax.xml.bind.Marshaller) ClassDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType) NamedValueType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.NamedValueType) Resource(org.glassfish.jersey.server.model.Resource) AnnotationDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.AnnotationDocType) WadlBuilder(org.glassfish.jersey.server.wadl.internal.WadlBuilder) JAXBContext(javax.xml.bind.JAXBContext) ApplicationDescription(org.glassfish.jersey.server.wadl.internal.ApplicationDescription) WadlGenerator(org.glassfish.jersey.server.wadl.WadlGenerator) StringWriter(java.io.StringWriter) MethodDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType) ResourceDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType) WadlGeneratorResourceDocSupport(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.WadlGeneratorResourceDocSupport) ParamDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ParamDocType) Test(org.junit.Test)

Example 2 with ClassDocType

use of org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType in project jersey by jersey.

the class WadlGeneratorResourceDocSupport method createResource.

/**
     * @param r Jersey resource component for which the WADL reource is to be created.
     * @param path path where the resource is exposed.
     * @return the enhanced {@link com.sun.research.ws.wadl.Resource}.
     * @see org.glassfish.jersey.server.wadl.WadlGenerator#createResource(org.glassfish.jersey.server.model.Resource, String)
     */
public Resource createResource(final org.glassfish.jersey.server.model.Resource r, final String path) {
    final Resource result = delegate.createResource(r, path);
    for (final Class<?> resourceClass : r.getHandlerClasses()) {
        final ClassDocType classDoc = resourceDoc.getClassDoc(resourceClass);
        if (classDoc != null && !isEmpty(classDoc.getCommentText())) {
            final Doc doc = new Doc();
            doc.getContent().add(classDoc.getCommentText());
            result.getDoc().add(doc);
        }
    }
    return result;
}
Also used : ClassDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType) Resource(com.sun.research.ws.wadl.Resource) Doc(com.sun.research.ws.wadl.Doc)

Example 3 with ClassDocType

use of org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType in project jersey by jersey.

the class ResourceDoclet method start.

/**
     * Start the doclet.
     *
     * @param root the root JavaDoc document.
     * @return true if no exception is thrown.
     */
public static boolean start(final RootDoc root) {
    final String output = getOptionArg(root.options(), OPTION_OUTPUT);
    final String classpath = getOptionArg(root.options(), OPTION_CLASSPATH);
    // LOG.info( "Have classpath: " + classpath );
    final String[] classpathElements = classpath.split(File.pathSeparator);
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    final ClassLoader ncl = new Loader(classpathElements, ResourceDoclet.class.getClassLoader());
    Thread.currentThread().setContextClassLoader(ncl);
    final String docProcessorOption = getOptionArg(root.options(), OPTION_DOC_PROCESSORS);
    final String[] docProcessors = docProcessorOption != null ? docProcessorOption.split(":") : null;
    final DocProcessorWrapper docProcessor = new DocProcessorWrapper();
    try {
        if (docProcessors != null && docProcessors.length > 0) {
            final Class<?> clazz = Class.forName(docProcessors[0], true, Thread.currentThread().getContextClassLoader());
            final Class<? extends DocProcessor> dpClazz = clazz.asSubclass(DocProcessor.class);
            docProcessor.add(dpClazz.newInstance());
        }
    } catch (final Exception e) {
        LOG.log(Level.SEVERE, "Could not load docProcessors " + docProcessorOption, e);
    }
    try {
        final ResourceDocType result = new ResourceDocType();
        final ClassDoc[] classes = root.classes();
        for (final ClassDoc classDoc : classes) {
            LOG.fine("Writing class " + classDoc.qualifiedTypeName());
            final ClassDocType classDocType = new ClassDocType();
            classDocType.setClassName(classDoc.qualifiedTypeName());
            classDocType.setCommentText(classDoc.commentText());
            docProcessor.processClassDoc(classDoc, classDocType);
            for (final MethodDoc methodDoc : classDoc.methods()) {
                final MethodDocType methodDocType = new MethodDocType();
                methodDocType.setMethodName(methodDoc.name());
                methodDocType.setMethodSignature(methodDoc.signature());
                methodDocType.setCommentText(methodDoc.commentText());
                docProcessor.processMethodDoc(methodDoc, methodDocType);
                addParamDocs(methodDoc, methodDocType, docProcessor);
                addRequestRepresentationDoc(methodDoc, methodDocType);
                addResponseDoc(methodDoc, methodDocType);
                classDocType.getMethodDocs().add(methodDocType);
            }
            result.getDocs().add(classDocType);
        }
        try {
            final Class<?>[] clazzes = getJAXBContextClasses(result, docProcessor);
            final JAXBContext c = JAXBContext.newInstance(clazzes);
            final Marshaller m = c.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            final OutputStream out = new BufferedOutputStream(new FileOutputStream(output));
            final String[] cdataElements = getCDataElements(docProcessor);
            final XMLSerializer serializer = getXMLSerializer(out, cdataElements);
            m.marshal(result, serializer);
            out.close();
            LOG.info("Wrote " + output);
        } catch (final Exception e) {
            LOG.log(Level.SEVERE, "Could not serialize ResourceDoc.", e);
            return false;
        }
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
    return true;
}
Also used : Marshaller(javax.xml.bind.Marshaller) XMLSerializer(org.apache.xml.serialize.XMLSerializer) ClassDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) JAXBContext(javax.xml.bind.JAXBContext) MalformedURLException(java.net.MalformedURLException) MethodDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType) MethodDoc(com.sun.javadoc.MethodDoc) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) ResourceDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType) BufferedOutputStream(java.io.BufferedOutputStream) ClassDoc(com.sun.javadoc.ClassDoc)

Example 4 with ClassDocType

use of org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType in project jersey by jersey.

the class ResourceDocAccessor method getMethodDoc.

public MethodDocType getMethodDoc(Class<?> resourceClass, Method method) {
    if (resourceClass == null || method == null) {
        return null;
    }
    final ClassDocType classDoc = getClassDoc(resourceClass);
    if (classDoc == null) {
        return null;
    }
    MethodDocType candidate = null;
    int candidateCount = 0;
    final String methodName = method.getName();
    final String methodSignature = computeSignature(method);
    for (MethodDocType methodDocType : classDoc.getMethodDocs()) {
        if (methodName.equals(methodDocType.getMethodName())) {
            candidateCount++;
            if (candidate == null) {
                candidate = methodDocType;
            }
            final String docMethodSignature = methodDocType.getMethodSignature();
            if (docMethodSignature != null && docMethodSignature.equals(methodSignature)) {
                return methodDocType;
            }
        }
    }
    if (candidate != null && candidateCount > 1 && LOGGER.isLoggable(Level.CONFIG)) {
        LOGGER.config(LocalizationMessages.WADL_RESOURCEDOC_AMBIGUOUS_METHOD_ENTRIES(resourceClass.getName(), methodName, methodSignature, candidateCount));
    }
    return candidate;
}
Also used : ClassDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType) MethodDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType)

Aggregations

ClassDocType (org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType)4 MethodDocType (org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType)3 JAXBContext (javax.xml.bind.JAXBContext)2 Marshaller (javax.xml.bind.Marshaller)2 ResourceDocType (org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType)2 ClassDoc (com.sun.javadoc.ClassDoc)1 MethodDoc (com.sun.javadoc.MethodDoc)1 Doc (com.sun.research.ws.wadl.Doc)1 Resource (com.sun.research.ws.wadl.Resource)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 URLClassLoader (java.net.URLClassLoader)1 XMLSerializer (org.apache.xml.serialize.XMLSerializer)1 Resource (org.glassfish.jersey.server.model.Resource)1 WadlGenerator (org.glassfish.jersey.server.wadl.WadlGenerator)1 ApplicationDescription (org.glassfish.jersey.server.wadl.internal.ApplicationDescription)1 WadlBuilder (org.glassfish.jersey.server.wadl.internal.WadlBuilder)1