Search in sources :

Example 1 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class EnunciateJaxrsContext method getResourceGroupsByPath.

public List<ResourceGroup> getResourceGroupsByPath(ApiRegistrationContext registrationContext) {
    Map<String, PathBasedResourceGroupImpl> resourcesByPath = new HashMap<String, PathBasedResourceGroupImpl>();
    FacetFilter facetFilter = registrationContext.getFacetFilter();
    for (RootResource rootResource : rootResources) {
        if (!facetFilter.accept(rootResource)) {
            continue;
        }
        for (ResourceMethod method : rootResource.getResourceMethods(true)) {
            if (facetFilter.accept(method)) {
                com.webcohesion.enunciate.metadata.rs.ServiceContextRoot context = method.getAnnotation(com.webcohesion.enunciate.metadata.rs.ServiceContextRoot.class);
                com.webcohesion.enunciate.modules.jaxrs.model.Resource resource = method.getParent();
                while (context == null && resource != null) {
                    context = resource.getAnnotation(com.webcohesion.enunciate.metadata.rs.ServiceContextRoot.class);
                    resource = resource.getParent();
                }
                String path = method.getFullpath();
                PathBasedResourceGroupImpl resourceGroup = resourcesByPath.get(path);
                if (resourceGroup == null) {
                    String contextPath = context != null ? JaxrsModule.sanitizeContextPath(context.value()) : this.relativeContextPath;
                    resourceGroup = new PathBasedResourceGroupImpl(contextPath, path, new ArrayList<Resource>());
                    resourcesByPath.put(path, resourceGroup);
                }
                resourceGroup.getResources().add(new ResourceImpl(method, resourceGroup, registrationContext));
            }
        }
    }
    ArrayList<ResourceGroup> resourceGroups = new ArrayList<ResourceGroup>(resourcesByPath.values());
    Collections.sort(resourceGroups, new ResourceGroupComparator(this.pathSortStrategy));
    return resourceGroups;
}
Also used : RootResource(com.webcohesion.enunciate.modules.jaxrs.model.RootResource) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) PathBasedResourceGroupImpl(com.webcohesion.enunciate.modules.jaxrs.api.impl.PathBasedResourceGroupImpl) ResourceImpl(com.webcohesion.enunciate.modules.jaxrs.api.impl.ResourceImpl) ResourceMethod(com.webcohesion.enunciate.modules.jaxrs.model.ResourceMethod) ResourceGroup(com.webcohesion.enunciate.api.resources.ResourceGroup)

Example 2 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class JavaXMLClientModule method generateClientSources.

protected File generateClientSources() {
    File sourceDir = getSourceDir();
    sourceDir.mkdirs();
    Map<String, Object> model = new HashMap<String, Object>();
    Map<String, String> conversions = getClientPackageConversions();
    EnunciateJaxbContext jaxbContext = this.jaxbModule.getJaxbContext();
    model.put("packageFor", new ClientPackageForMethod(conversions, this.context));
    model.put("classnameFor", new ClientClassnameForMethod(conversions, jaxbContext));
    model.put("simpleNameFor", new SimpleNameForMethod(new ClientClassnameForMethod(conversions, jaxbContext, true)));
    model.put("file", new FileDirective(sourceDir, this.enunciate.getLogger()));
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    model.put("annotationValue", new AnnotationValueMethod());
    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);
    model.put("isFacetExcluded", new IsFacetExcludedMethod(facetFilter));
    boolean upToDate = isUpToDateWithSources(sourceDir);
    if (!upToDate) {
        try {
            debug("Generating the Java client classes...");
            HashMap<String, WebFault> allFaults = new HashMap<String, WebFault>();
            AntPatternMatcher matcher = new AntPatternMatcher();
            matcher.setPathSeparator(".");
            if (this.jaxwsModule != null) {
                Set<String> seeAlsos = new TreeSet<String>();
                // for each endpoint interface.
                for (WsdlInfo wsdlInfo : this.jaxwsModule.getJaxwsContext().getWsdls().values()) {
                    for (EndpointInterface ei : wsdlInfo.getEndpointInterfaces()) {
                        if (facetFilter.accept(ei)) {
                            for (WebMethod webMethod : ei.getWebMethods()) {
                                if (facetFilter.accept(webMethod)) {
                                    for (WebMessage webMessage : webMethod.getMessages()) {
                                        if (webMessage instanceof RequestWrapper) {
                                            model.put("message", webMessage);
                                            processTemplate(getTemplateURL("client-request-bean.fmt"), model);
                                            seeAlsos.add(getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), ((RequestWrapper) webMessage).getRequestBeanName()));
                                        } else if (webMessage instanceof ResponseWrapper) {
                                            model.put("message", webMessage);
                                            processTemplate(getTemplateURL("client-response-bean.fmt"), model);
                                            seeAlsos.add(getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), ((ResponseWrapper) webMessage).getResponseBeanName()));
                                        } else if (webMessage instanceof WebFault) {
                                            WebFault fault = (WebFault) webMessage;
                                            allFaults.put(fault.getQualifiedName().toString(), fault);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // gather the annotation information and process the possible beans for each web fault.
                for (WebFault webFault : allFaults.values()) {
                    boolean implicit = webFault.isImplicitSchemaElement();
                    String faultBean = implicit ? getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), webFault.getImplicitFaultBeanQualifiedName()) : new ClientClassnameForMethod(conversions, jaxbContext).convert(webFault.getExplicitFaultBeanType());
                    seeAlsos.add(faultBean);
                    if (implicit) {
                        model.put("fault", webFault);
                        processTemplate(getTemplateURL("client-fault-bean.fmt"), model);
                    }
                }
                model.put("seeAlsoBeans", seeAlsos);
                model.put("baseUri", this.enunciate.getConfiguration().getApplicationRoot());
                for (WsdlInfo wsdlInfo : this.jaxwsModule.getJaxwsContext().getWsdls().values()) {
                    if (wsdlInfo.getWsdlFile() == null) {
                        throw new EnunciateException("WSDL " + wsdlInfo.getId() + " doesn't have a filename.");
                    }
                    for (EndpointInterface ei : wsdlInfo.getEndpointInterfaces()) {
                        if (facetFilter.accept(ei)) {
                            model.put("endpointInterface", ei);
                            model.put("wsdlFileName", wsdlInfo.getFilename());
                            processTemplate(getTemplateURL("client-endpoint-interface.fmt"), model);
                            processTemplate(getTemplateURL("client-soap-endpoint-impl.fmt"), model);
                        }
                    }
                }
                for (WebFault webFault : allFaults.values()) {
                    if (useServerSide(webFault, matcher)) {
                        copyServerSideType(sourceDir, webFault);
                    } else {
                        TypeElement superFault = (TypeElement) ((DeclaredType) webFault.getSuperclass()).asElement();
                        if (superFault != null && allFaults.containsKey(superFault.getQualifiedName().toString()) && allFaults.get(superFault.getQualifiedName().toString()).isImplicitSchemaElement()) {
                            model.put("superFault", allFaults.get(superFault.getQualifiedName().toString()));
                        } else {
                            model.remove("superFault");
                        }
                        model.put("fault", webFault);
                        processTemplate(getTemplateURL("client-web-fault.fmt"), model);
                    }
                }
            }
            for (SchemaInfo schemaInfo : this.jaxbModule.getJaxbContext().getSchemas().values()) {
                for (TypeDefinition typeDefinition : schemaInfo.getTypeDefinitions()) {
                    if (facetFilter.accept(typeDefinition)) {
                        if (useServerSide(typeDefinition, matcher)) {
                            copyServerSideType(sourceDir, typeDefinition);
                        } else {
                            model.put("rootEl", this.jaxbModule.getJaxbContext().findElementDeclaration(typeDefinition));
                            model.put("type", typeDefinition);
                            URL template = typeDefinition.isEnum() ? typeDefinition instanceof QNameEnumTypeDefinition ? getTemplateURL("client-qname-enum-type.fmt") : getTemplateURL("client-enum-type.fmt") : typeDefinition.isSimple() ? getTemplateURL("client-simple-type.fmt") : getTemplateURL("client-complex-type.fmt");
                            processTemplate(template, model);
                        }
                    }
                }
                for (Registry registry : schemaInfo.getRegistries()) {
                    model.put("registry", registry);
                    processTemplate(getTemplateURL("client-registry.fmt"), model);
                }
            }
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping generation of Java client sources as everything appears up-to-date...");
    }
    context.setProperty(LIRBARY_DESCRIPTION_PROPERTY, readLibraryDescription(model));
    return sourceDir;
}
Also used : QNameEnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.QNameEnumTypeDefinition) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) QNameEnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.QNameEnumTypeDefinition) EnunciateException(com.webcohesion.enunciate.EnunciateException) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) TemplateException(freemarker.template.TemplateException) TypeElement(javax.lang.model.element.TypeElement) Registry(com.webcohesion.enunciate.modules.jaxb.model.Registry) EnunciateJaxbContext(com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext) AntPatternMatcher(com.webcohesion.enunciate.util.AntPatternMatcher) JavaFileObject(javax.tools.JavaFileObject) WsdlInfo(com.webcohesion.enunciate.modules.jaxws.WsdlInfo)

Example 3 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class DataTypeExampleImpl method build.

private void build(ObjectNode node, ObjectTypeDefinition type, @Nonnull ObjectTypeDefinition sourceType, Context context) {
    if (context.stack.size() > 2) {
        // don't go deeper than 2 for fear of the OOM (see https://github.com/stoicflame/enunciate/issues/139).
        return;
    }
    if (type.getTypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
        if (type.getTypeIdProperty() != null) {
            node.put(type.getTypeIdProperty(), sourceType.getTypeIdValue());
        }
    }
    JsonNode override = findExampleOverride(type, type.getContext().getContext().getLogger());
    if (override != null) {
        if (override instanceof ObjectNode) {
            node.putAll((ObjectNode) override);
            return;
        } else {
            type.getContext().getContext().getLogger().warn("JSON example override of %s can't be used because it's not a JSON object.", type.getQualifiedName());
        }
    }
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    for (Member member : type.getMembers()) {
        if (node.has(member.getName())) {
            continue;
        }
        if (!facetFilter.accept(member)) {
            continue;
        }
        if (ElementUtils.findDeprecationMessage(member, null) != null) {
            continue;
        }
        JsonNode memberOverride = findExampleOverride(member, type.getContext().getContext().getLogger());
        if (memberOverride != null) {
            node.put(member.getName(), memberOverride);
            continue;
        }
        String example = null;
        String example2 = null;
        JsonType exampleType = null;
        JavaDoc.JavaDocTagList tags = getDocumentationExampleTags(member);
        if (tags != null && tags.size() > 0) {
            String tag = tags.get(0).trim();
            example = tag.isEmpty() ? null : tag;
            example2 = example;
            if (tags.size() > 1) {
                tag = tags.get(1).trim();
                example2 = tag.isEmpty() ? null : tag;
            }
        }
        tags = member.getJavaDoc().get("documentationType");
        if (tags != null && tags.size() > 0) {
            String tag = tags.get(0).trim();
            if (!tag.isEmpty()) {
                TypeElement typeElement = type.getContext().getContext().getProcessingEnvironment().getElementUtils().getTypeElement(tag);
                if (typeElement != null) {
                    exampleType = JsonTypeFactory.getJsonType(typeElement.asType(), type.getContext());
                } else {
                    type.getContext().getContext().getLogger().warn("Invalid documentation type %s.", tag);
                }
            }
        }
        DocumentationExample documentationExample = getDocumentationExample(member);
        if (documentationExample != null) {
            if (documentationExample.exclude()) {
                continue;
            }
            example = documentationExample.value();
            example = "##default".equals(example) ? null : example;
            example2 = documentationExample.value2();
            example2 = "##default".equals(example2) ? null : example2;
            TypeMirror typeHint = TypeHintUtils.getTypeHint(documentationExample.type(), type.getContext().getContext().getProcessingEnvironment(), null);
            if (typeHint != null) {
                exampleType = JsonTypeFactory.getJsonType(typeHint, type.getContext());
            }
        }
        String specifiedTypeInfoValue = findSpecifiedTypeInfoValue(member, type.getQualifiedName().toString(), type);
        if (specifiedTypeInfoValue != null) {
            example = specifiedTypeInfoValue;
            example2 = specifiedTypeInfoValue;
        }
        String configuredExample = getConfiguredExample(member);
        if (configuredExample != null) {
            example = configuredExample;
            example2 = configuredExample;
        }
        if (context.currentIndex % 2 > 0) {
            // if our index is odd, switch example 1 and example 2.
            String placeholder = example2;
            example2 = example;
            example = placeholder;
        }
        if (member.getChoices().size() > 1) {
            if (member.isCollectionType()) {
                final ArrayNode exampleNode = JsonNodeFactory.instance.arrayNode();
                for (Member choice : member.getChoices()) {
                    JsonType jsonType = exampleType == null ? choice.getJsonType() : exampleType;
                    String choiceName = choice.getName();
                    if ("".equals(choiceName)) {
                        choiceName = "...";
                    }
                    if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.WRAPPER_ARRAY) {
                        ArrayNode wrapperNode = JsonNodeFactory.instance.arrayNode();
                        wrapperNode.add(choiceName);
                        wrapperNode.add(exampleNode(jsonType, example, example2, context));
                        exampleNode.add(wrapperNode);
                    } else if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.WRAPPER_OBJECT) {
                        ObjectNode wrapperNode = JsonNodeFactory.instance.objectNode();
                        wrapperNode.put(choiceName, exampleNode(jsonType, example, example2, context));
                        exampleNode.add(wrapperNode);
                    } else {
                        JsonNode itemNode = exampleNode(jsonType, example, example2, context);
                        if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
                            if (member.getSubtypeIdProperty() != null && itemNode instanceof ObjectNode) {
                                ((ObjectNode) itemNode).put(member.getSubtypeIdProperty(), "...");
                            }
                        } else if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.EXTERNAL_PROPERTY) {
                            if (member.getSubtypeIdProperty() != null) {
                                node.put(member.getSubtypeIdProperty(), "...");
                            }
                        }
                        node.put(member.getName(), exampleNode);
                        exampleNode.add(itemNode);
                    }
                }
            } else {
                for (Member choice : member.getChoices()) {
                    JsonNode exampleNode;
                    JsonType jsonType = exampleType == null ? choice.getJsonType() : exampleType;
                    String choiceName = choice.getName();
                    if ("".equals(choiceName)) {
                        choiceName = "...";
                    }
                    if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.WRAPPER_ARRAY) {
                        ArrayNode wrapperNode = JsonNodeFactory.instance.arrayNode();
                        wrapperNode.add(choiceName);
                        wrapperNode.add(exampleNode(jsonType, example, example2, context));
                        exampleNode = wrapperNode;
                    } else if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.WRAPPER_OBJECT) {
                        ObjectNode wrapperNode = JsonNodeFactory.instance.objectNode();
                        wrapperNode.put(choiceName, exampleNode(jsonType, example, example2, context));
                        exampleNode = wrapperNode;
                    } else {
                        exampleNode = exampleNode(jsonType, example, example2, context);
                        if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
                            if (member.getSubtypeIdProperty() != null && exampleNode instanceof ObjectNode) {
                                ((ObjectNode) exampleNode).put(member.getSubtypeIdProperty(), "...");
                            }
                        } else if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.EXTERNAL_PROPERTY) {
                            if (member.getSubtypeIdProperty() != null) {
                                node.put(member.getSubtypeIdProperty(), "...");
                            }
                        }
                    }
                    node.put(member.getName(), exampleNode);
                }
            }
        } else {
            JsonType jsonType = exampleType == null ? member.getJsonType() : exampleType;
            node.put(member.getName(), exampleNode(jsonType, example, example2, context));
        }
    }
    JsonType supertype = type.getSupertype();
    if (supertype instanceof JsonClassType && ((JsonClassType) supertype).getTypeDefinition() instanceof ObjectTypeDefinition) {
        build(node, (ObjectTypeDefinition) ((JsonClassType) supertype).getTypeDefinition(), sourceType, context);
    }
    if (type.getWildcardMember() != null && ElementUtils.findDeprecationMessage(type.getWildcardMember(), null) == null && !ExampleUtils.isExcluded(type.getWildcardMember())) {
        node.put("extension1", "...");
        node.put("extension2", "...");
    }
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) TypeElement(javax.lang.model.element.TypeElement) JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) JsonNode(org.codehaus.jackson.JsonNode) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayNode(org.codehaus.jackson.node.ArrayNode)

Example 4 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class EnumDataTypeImpl method getValues.

@Override
public List<? extends Value> getValues() {
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    List<EnumValue> enumValues = this.typeDefinition.getEnumValues();
    ArrayList<Value> values = new ArrayList<Value>(enumValues.size());
    for (EnumValue enumValue : enumValues) {
        if (enumValue.getValue() != null) {
            if (!facetFilter.accept(enumValue)) {
                continue;
            }
            values.add(createValue(enumValue));
        }
    }
    return values;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) EnumValue(com.webcohesion.enunciate.modules.jackson.model.EnumValue) Value(com.webcohesion.enunciate.api.datatype.Value) EnumValue(com.webcohesion.enunciate.modules.jackson.model.EnumValue) ArrayList(java.util.ArrayList)

Example 5 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class SyntaxImpl method getTypes.

@Override
public List<? extends DataType> getTypes() {
    Collection<TypeDefinition> typeDefinitions = this.context.getTypeDefinitions();
    ArrayList<DataType> dataTypes = new ArrayList<DataType>();
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    for (TypeDefinition typeDefinition : typeDefinitions) {
        if (!facetFilter.accept(typeDefinition)) {
            continue;
        }
        if (typeDefinition instanceof ObjectTypeDefinition) {
            dataTypes.add(new ObjectDataTypeImpl((ObjectTypeDefinition) typeDefinition, registrationContext));
        } else if (typeDefinition instanceof EnumTypeDefinition) {
            dataTypes.add(new EnumDataTypeImpl((EnumTypeDefinition) typeDefinition, registrationContext));
        }
    }
    Collections.sort(dataTypes, new Comparator<DataType>() {

        @Override
        public int compare(DataType o1, DataType o2) {
            return o1.getLabel().compareTo(o2.getLabel());
        }
    });
    return dataTypes;
}
Also used : ObjectTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.ObjectTypeDefinition) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) EnumTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.EnumTypeDefinition) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) ObjectTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.ObjectTypeDefinition) EnumTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.EnumTypeDefinition)

Aggregations

FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)33 EnunciateException (com.webcohesion.enunciate.EnunciateException)11 TemplateException (freemarker.template.TemplateException)10 URL (java.net.URL)10 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)7 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)7 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)7 ResourceGroup (com.webcohesion.enunciate.api.resources.ResourceGroup)6 File (java.io.File)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 TypeElement (javax.lang.model.element.TypeElement)6 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)5 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)5 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)5 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)5 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)5 FileDirective (com.webcohesion.enunciate.util.freemarker.FileDirective)5 TypeMirror (javax.lang.model.type.TypeMirror)5 EnunciateJaxbContext (com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext)4