Search in sources :

Example 1 with ResourceDocType

use of org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType 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 ResourceDocType

use of org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType 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 3 with ResourceDocType

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

the class WadlGeneratorResourceDocSupport method init.

public void init() throws Exception {
    if (resourceDocFile == null && resourceDocStream == null) {
        throw new IllegalStateException("Neither the resourceDocFile nor the resourceDocStream" + " is set, one of both is required.");
    }
    delegate.init();
    try (final InputStream inputStream = resourceDocFile != null ? new FileInputStream(resourceDocFile) : resourceDocStream) {
        final ResourceDocType resourceDocType = WadlUtils.unmarshall(inputStream, saxFactoryProvider.get(), ResourceDocType.class);
        resourceDoc = new ResourceDocAccessor(resourceDocType);
    } finally {
        resourceDocFile = null;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ResourceDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType) FileInputStream(java.io.FileInputStream)

Aggregations

ResourceDocType (org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType)3 JAXBContext (javax.xml.bind.JAXBContext)2 Marshaller (javax.xml.bind.Marshaller)2 ClassDocType (org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType)2 MethodDocType (org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType)2 ClassDoc (com.sun.javadoc.ClassDoc)1 MethodDoc (com.sun.javadoc.MethodDoc)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)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