Search in sources :

Example 1 with Resource

use of com.manydesigns.portofino.dispatcher.Resource in project Portofino by ManyDesigns.

the class DocumentedApiRoot method afterScan.

@Override
public void afterScan(OpenApiReader reader, OpenAPI openAPI) {
    final SubResourceReader subResourceReader = getSubResourceReader(openAPI);
    try {
        // TODO actions should be put in a special "inspection mode" to avoid checks (e.g. not-in-use-case),
        // hitting the DB or services, etc.
        Resource root = rootFactory.createRoot();
        root.setResourceContext(getResourceContext());
        initRoot(root);
        if (openAPI.getPaths() == null) {
            openAPI.setPaths(new Paths());
        }
        new DepthFirstVisitor(node -> {
            try {
                OpenAPI subApi = subResourceReader.readSubResource(node);
                openAPI.getPaths().putAll(subApi.getPaths());
            // TODO merge components too
            } catch (Exception e) {
                logger.error("Could not read node at " + node.getLocation(), e);
            }
        }).visit(root);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : ReaderListener(io.swagger.v3.jaxrs2.ReaderListener) WithParameters(com.manydesigns.portofino.dispatcher.WithParameters) DepthFirstVisitor(com.manydesigns.portofino.dispatcher.visitor.DepthFirstVisitor) Logger(org.slf4j.Logger) Reader(io.swagger.v3.jaxrs2.Reader) Parameter(io.swagger.v3.oas.models.parameters.Parameter) LoggerFactory(org.slf4j.LoggerFactory) RootFactory(com.manydesigns.portofino.dispatcher.RootFactory) Paths(io.swagger.v3.oas.models.Paths) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ResourceContext(javax.ws.rs.container.ResourceContext) Resource(com.manydesigns.portofino.dispatcher.Resource) OpenApiReader(io.swagger.v3.oas.integration.api.OpenApiReader) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) Resource(com.manydesigns.portofino.dispatcher.Resource) DepthFirstVisitor(com.manydesigns.portofino.dispatcher.visitor.DepthFirstVisitor) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 2 with Resource

use of com.manydesigns.portofino.dispatcher.Resource in project Portofino by ManyDesigns.

the class ActionsAction method getResource.

@Path("action")
public Resource getResource() {
    String actionPath = StringUtils.join(parameters, "/");
    Resource resource = getResource(actionPath);
    if (resource == null) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    return resource;
}
Also used : Resource(com.manydesigns.portofino.dispatcher.Resource)

Example 3 with Resource

use of com.manydesigns.portofino.dispatcher.Resource in project Portofino by ManyDesigns.

the class ActionsAction method getResourceAction.

protected AbstractResourceAction getResourceAction() {
    String actionPath = StringUtils.join(parameters, "/");
    Resource resource = getResource(actionPath);
    if (resource instanceof AbstractResourceAction) {
        return ((AbstractResourceAction) resource);
    } else {
        logger.error("Not a ResourceAction: " + resource);
        throw new WebApplicationException();
    }
}
Also used : Resource(com.manydesigns.portofino.dispatcher.Resource) AbstractResourceAction(com.manydesigns.portofino.resourceactions.AbstractResourceAction)

Example 4 with Resource

use of com.manydesigns.portofino.dispatcher.Resource in project Portofino by ManyDesigns.

the class ActionsAction method getResource.

public Resource getResource(String actionPath) {
    Resource resource = getRoot();
    if (actionPath.isEmpty()) {
        return resource;
    }
    String[] pathSegments = actionPath.split("/");
    for (String segment : pathSegments) {
        if (resource instanceof WithParameters) {
            WithParameters withParameters = (WithParameters) resource;
            if (withParameters.getParameters().size() < withParameters.getMinParameters()) {
                withParameters.consumeParameter(segment);
                continue;
            }
        }
        Object subResource = null;
        try {
            subResource = resource.getSubResource(segment);
        } catch (Exception e) {
            logger.debug("Could not load resource", e);
        }
        if (subResource instanceof Resource) {
            resource = (Resource) subResource;
        } else if (resource instanceof WithParameters) {
            WithParameters withParameters = (WithParameters) resource;
            if (withParameters.getParameters().size() < withParameters.getMaxParameters()) {
                withParameters.consumeParameter(segment);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
    return resource;
}
Also used : Resource(com.manydesigns.portofino.dispatcher.Resource) FileObject(org.apache.commons.vfs2.FileObject) WithParameters(com.manydesigns.portofino.dispatcher.WithParameters) FileSystemException(org.apache.commons.vfs2.FileSystemException)

Example 5 with Resource

use of com.manydesigns.portofino.dispatcher.Resource in project Portofino by ManyDesigns.

the class ApplicationRoot method start.

@Path("")
public Object start() throws Exception {
    Resource root = rootFactory.createRoot();
    resourceContext.initResource(root);
    return root.init();
}
Also used : Resource(com.manydesigns.portofino.dispatcher.Resource) Path(javax.ws.rs.Path)

Aggregations

Resource (com.manydesigns.portofino.dispatcher.Resource)5 WithParameters (com.manydesigns.portofino.dispatcher.WithParameters)2 RootFactory (com.manydesigns.portofino.dispatcher.RootFactory)1 DepthFirstVisitor (com.manydesigns.portofino.dispatcher.visitor.DepthFirstVisitor)1 AbstractResourceAction (com.manydesigns.portofino.resourceactions.AbstractResourceAction)1 Reader (io.swagger.v3.jaxrs2.Reader)1 ReaderListener (io.swagger.v3.jaxrs2.ReaderListener)1 OpenApiReader (io.swagger.v3.oas.integration.api.OpenApiReader)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 Paths (io.swagger.v3.oas.models.Paths)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Path (javax.ws.rs.Path)1 ResourceContext (javax.ws.rs.container.ResourceContext)1 FileObject (org.apache.commons.vfs2.FileObject)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1