Search in sources :

Example 21 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class DocsModule method call.

@Override
public void call(EnunciateContext context) {
    try {
        File docsDir = getDocsDir();
        String subDir = getDocsSubdir();
        if (subDir != null) {
            docsDir = new File(docsDir, subDir);
        }
        if (!isUpToDateWithSources(docsDir)) {
            Set<String> facetIncludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetIncludes());
            facetIncludes.addAll(getFacetIncludes());
            Set<String> facetExcludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetExcludes());
            facetExcludes.addAll(getFacetExcludes());
            FacetFilter facetFilter = new FacetFilter(facetIncludes, facetExcludes);
            ApiRegistrationContext registrationContext = new DocsRegistrationContext(this.apiRegistry, facetFilter);
            List<ResourceApi> resourceApis = this.apiRegistry.getResourceApis(registrationContext);
            Set<Syntax> syntaxes = this.apiRegistry.getSyntaxes(registrationContext);
            List<ServiceApi> serviceApis = this.apiRegistry.getServiceApis(registrationContext);
            Set<Artifact> documentationArtifacts = findDocumentationArtifacts();
            if (syntaxes.isEmpty() && serviceApis.isEmpty() && resourceApis.isEmpty() && documentationArtifacts.isEmpty()) {
                warn("No documentation generated: there are no data types, services, or resources to document.");
                return;
            }
            // make sure the docs dir exists.
            docsDir.mkdirs();
            Map<String, Object> model = new HashMap<String, Object>();
            String intro = this.enunciate.getConfiguration().readDescription(context, false, registrationContext.getTagHandler());
            if (intro != null) {
                model.put("apiDoc", intro);
            }
            String copyright = this.enunciate.getConfiguration().getCopyright();
            if (copyright != null) {
                model.put("copyright", copyright);
            }
            String title = this.enunciate.getConfiguration().getTitle();
            model.put("title", title == null ? "Web Service API" : title);
            // extract out the documentation base
            String cssPath = buildBase(docsDir);
            if (cssPath != null) {
                model.put("cssFile", cssPath);
            }
            model.put("file", new FileDirective(docsDir, this.enunciate.getLogger()));
            model.put("apiRelativePath", getRelativePathToRootDir());
            model.put("includeApplicationPath", isIncludeApplicationPath());
            model.put("includeDataTypesHomeIndex", isIncludeDataTypesHomeIndex());
            model.put("favicon", getFavicon());
            // iterate through schemas and make sure the schema is copied to the docs dir
            for (Syntax syntax : syntaxes) {
                for (Namespace namespace : syntax.getNamespaces()) {
                    if (namespace.getSchemaFile() != null) {
                        namespace.getSchemaFile().writeTo(docsDir);
                    }
                }
            }
            model.put("data", syntaxes);
            for (ResourceApi resourceApi : resourceApis) {
                if (resourceApi.getWadlFile() != null) {
                    resourceApi.getWadlFile().writeTo(docsDir);
                }
            }
            model.put("resourceApis", resourceApis);
            InterfaceDescriptionFile swaggerUI = this.apiRegistry.getSwaggerUI();
            if (swaggerUI != null) {
                swaggerUI.writeTo(docsDir);
                model.put("swaggerUI", swaggerUI);
            }
            // iterate through wsdls and make sure the wsdl is copied to the docs dir
            for (ServiceApi serviceApi : serviceApis) {
                for (ServiceGroup serviceGroup : serviceApi.getServiceGroups()) {
                    if (serviceGroup.getWsdlFile() != null) {
                        serviceGroup.getWsdlFile().writeTo(docsDir);
                    }
                }
            }
            model.put("serviceApis", serviceApis);
            model.put("downloads", copyDocumentationArtifacts(documentationArtifacts, docsDir));
            model.put("indexPageName", getIndexPageName());
            model.put("disableMountpoint", isDisableRestMountpoint());
            model.put("additionalCssFiles", getAdditionalCss());
            model.put("disableResourceLinks", isDisableResourceLinks());
            processTemplate(getDocsTemplateURL(), model);
        } else {
            info("Skipping documentation source generation as everything appears up-to-date...");
        }
        this.enunciate.addArtifact(new FileArtifact(getName(), "docs", docsDir));
    } catch (IOException e) {
        throw new EnunciateException(e);
    } catch (TemplateException e) {
        throw new EnunciateException(e);
    }
}
Also used : ServiceGroup(com.webcohesion.enunciate.api.services.ServiceGroup) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) ServiceApi(com.webcohesion.enunciate.api.services.ServiceApi) EnunciateException(com.webcohesion.enunciate.EnunciateException) FileDirective(com.webcohesion.enunciate.util.freemarker.FileDirective) TemplateException(freemarker.template.TemplateException) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact) ClientLibraryJavaArtifact(com.webcohesion.enunciate.artifacts.ClientLibraryJavaArtifact) Artifact(com.webcohesion.enunciate.artifacts.Artifact) ClientLibraryArtifact(com.webcohesion.enunciate.artifacts.ClientLibraryArtifact) Namespace(com.webcohesion.enunciate.api.datatype.Namespace) ResourceApi(com.webcohesion.enunciate.api.resources.ResourceApi) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact) Syntax(com.webcohesion.enunciate.api.datatype.Syntax)

Example 22 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class XmlTypeFactory method loadPackageExplicitTypes.

/**
 * Load any explicit schema types specified by the package.
 *
 * @param pckg The package.
 * @param context The context.
 * @return Any explicit schema types specified by the package.
 */
protected static Map<String, XmlSchemaType> loadPackageExplicitTypes(PackageElement pckg, EnunciateJaxbContext context) {
    Map<String, XmlSchemaType> explicitTypes = new HashMap<String, XmlSchemaType>();
    XmlSchemaType schemaTypeInfo = pckg.getAnnotation(XmlSchemaType.class);
    XmlSchemaTypes schemaTypes = pckg.getAnnotation(XmlSchemaTypes.class);
    if ((schemaTypeInfo != null) || (schemaTypes != null)) {
        ArrayList<XmlSchemaType> allSpecifiedTypes = new ArrayList<XmlSchemaType>();
        if (schemaTypeInfo != null) {
            allSpecifiedTypes.add(schemaTypeInfo);
        }
        if (schemaTypes != null) {
            allSpecifiedTypes.addAll(Arrays.asList(schemaTypes.value()));
        }
        for (final XmlSchemaType specifiedType : allSpecifiedTypes) {
            DecoratedTypeMirror typeMirror = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return specifiedType.type();
                }
            }, context.getContext().getProcessingEnvironment(), XmlSchemaType.DEFAULT.class);
            if (typeMirror == null) {
                throw new EnunciateException(pckg.getQualifiedName() + ": a type must be specified in " + XmlSchemaType.class.getName() + " at the package-level.");
            }
            if (!(typeMirror instanceof DeclaredType)) {
                throw new EnunciateException(pckg.getQualifiedName() + ": only a declared type can be adapted.  Offending type: " + typeMirror);
            }
            explicitTypes.put(((TypeElement) ((DeclaredType) typeMirror).asElement()).getQualifiedName().toString(), specifiedType);
        }
    }
    return Collections.unmodifiableMap(explicitTypes);
}
Also used : DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) XmlSchemaType(javax.xml.bind.annotation.XmlSchemaType) XmlSchemaTypes(javax.xml.bind.annotation.XmlSchemaTypes) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) DeclaredType(javax.lang.model.type.DeclaredType)

Example 23 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class JaxwsModule method call.

@Override
public void call(EnunciateContext context) {
    jaxwsContext = new EnunciateJaxwsContext(this.jaxbModule.getJaxbContext(), isUseSourceParameterNames());
    boolean aggressiveWebMethodExcludePolicy = isAggressiveWebMethodExcludePolicy();
    Map<String, String> eiPaths = new HashMap<>();
    File sunJaxwsXmlFile = getSunJaxwsXmlFile();
    if (sunJaxwsXmlFile != null) {
        XMLConfiguration config;
        try {
            config = new XMLConfiguration(sunJaxwsXmlFile);
        } catch (ConfigurationException e) {
            throw new EnunciateException(e);
        }
        List<HierarchicalConfiguration> endpoints = config.configurationsAt("endpoint");
        for (HierarchicalConfiguration endpoint : endpoints) {
            String impl = endpoint.getString("[@implementation]", null);
            String urlPattern = endpoint.getString("[@url-pattern]", null);
            if (impl != null && urlPattern != null) {
                eiPaths.put(impl, urlPattern);
            }
        }
    }
    DataTypeDetectionStrategy detectionStrategy = getDataTypeDetectionStrategy();
    if (detectionStrategy != DataTypeDetectionStrategy.passive) {
        Set<? extends Element> elements = detectionStrategy == DataTypeDetectionStrategy.local ? context.getLocalApiElements() : context.getApiElements();
        for (Element declaration : elements) {
            try {
                if (declaration instanceof TypeElement) {
                    TypeElement element = (TypeElement) declaration;
                    XmlRegistry registryMetadata = declaration.getAnnotation(XmlRegistry.class);
                    if (registryMetadata != null) {
                        this.jaxbModule.addPotentialJaxbElement(element, new LinkedList<>());
                    }
                    if (isEndpointInterface(element)) {
                        EndpointInterface ei = new EndpointInterface(element, elements, aggressiveWebMethodExcludePolicy, jaxwsContext);
                        for (EndpointImplementation implementation : ei.getEndpointImplementations()) {
                            String urlPattern = eiPaths.get(implementation.getQualifiedName().toString());
                            if (urlPattern != null) {
                                if (!urlPattern.startsWith("/")) {
                                    urlPattern = "/" + urlPattern;
                                }
                                if (urlPattern.endsWith("/*")) {
                                    urlPattern = urlPattern.substring(0, urlPattern.length() - 2) + ei.getServiceName();
                                }
                                implementation.setPath(urlPattern);
                            }
                        }
                        jaxwsContext.add(ei);
                        addReferencedDataTypeDefinitions(ei);
                    }
                }
            } catch (RuntimeException e) {
                if (e.getClass().getName().endsWith("CompletionFailure")) {
                    throw new CompletionFailureException(Collections.singletonList(declaration), e);
                }
                throw e;
            }
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ImplicitChildElement(com.webcohesion.enunciate.modules.jaxb.model.ImplicitChildElement) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration) XmlRegistry(javax.xml.bind.annotation.XmlRegistry) CompletionFailureException(com.webcohesion.enunciate.CompletionFailureException) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) EnunciateException(com.webcohesion.enunciate.EnunciateException) File(java.io.File)

Example 24 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class ExampleUtils method loadCustomExample.

public static Example loadCustomExample(Syntax syntax, String tagName, DecoratedElement element, EnunciateContext context) {
    Example example = null;
    JavaDoc.JavaDocTagList tagList = element.getJavaDoc().get(tagName);
    if (tagList != null) {
        for (String value : tagList) {
            int firstSpace = JavaDoc.indexOfFirstWhitespace(value);
            String mediaType = value.substring(0, firstSpace);
            if (syntax.isAssignableToMediaType(mediaType) && (firstSpace + 1) < value.length()) {
                String specifiedExample = value.substring(firstSpace + 1).trim();
                Reader reader;
                try {
                    if (specifiedExample.startsWith("classpath:")) {
                        String classpathResource = specifiedExample.substring(10);
                        if (classpathResource.startsWith("/")) {
                            classpathResource = classpathResource.substring(1);
                        }
                        InputStream resource = context.getResourceAsStream(classpathResource);
                        if (resource == null) {
                            throw new IllegalArgumentException("Unable to find /" + classpathResource + " on the classpath.");
                        }
                        reader = new InputStreamReader(resource, "UTF-8");
                    } else if (specifiedExample.startsWith("file:")) {
                        File file = context.getConfiguration().resolveFile(specifiedExample.substring(5));
                        if (!file.exists()) {
                            throw new IllegalArgumentException("Unable to find " + specifiedExample.substring(5) + ".");
                        }
                        reader = new FileReader(file);
                    } else {
                        reader = new StringReader(specifiedExample);
                    }
                } catch (IOException e) {
                    throw new EnunciateException(e);
                }
                try {
                    example = syntax.parseExample(reader);
                } catch (Exception e) {
                    context.getLogger().warn("Unable to parse @%s tag at %s: %s", tagName, example, e.getMessage());
                }
            }
        }
    }
    return example;
}
Also used : JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) Example(com.webcohesion.enunciate.api.datatype.Example) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample)

Example 25 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class GWTJSONOverlayModule method readLibraryDescription.

/**
 * Reads a resource into string form.
 *
 * @return The string form of the resource.
 */
protected String readLibraryDescription(Map<String, Object> model) {
    model.put("sample_resource", findExampleResourceMethod());
    URL res = GWTJSONOverlayModule.class.getResource("library_description.fmt");
    try {
        return processTemplate(res, model);
    } catch (TemplateException e) {
        throw new EnunciateException(e);
    } catch (IOException e) {
        throw new EnunciateException(e);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

EnunciateException (com.webcohesion.enunciate.EnunciateException)53 URL (java.net.URL)21 TemplateException (freemarker.template.TemplateException)19 IOException (java.io.IOException)16 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)12 File (java.io.File)12 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)11 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)8 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)7 TypeElement (javax.lang.model.element.TypeElement)7 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)6 FindRootElementMethod (com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod)6 IsFacetExcludedMethod (com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod)6 VariableElement (javax.lang.model.element.VariableElement)6 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)5 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)5 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)5 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)5 AccessorOverridesAnotherMethod (com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod)5 FileDirective (com.webcohesion.enunciate.util.freemarker.FileDirective)5