Search in sources :

Example 11 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class TopologiesResource method uploadProviderConfiguration.

@PUT
@Consumes({ APPLICATION_XML, APPLICATION_JSON, TEXT_PLAIN })
@Path(SINGLE_PROVIDERCONFIG_API_PATH)
public Response uploadProviderConfiguration(@PathParam("name") String name, @Context HttpHeaders headers, String content) {
    Response response = null;
    GatewayServices gs = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    TopologyService ts = gs.getService(GatewayServices.TOPOLOGY_SERVICE);
    File existing = getExistingConfigFile(ts.getProviderConfigurations(), name);
    boolean isUpdate = (existing != null);
    // If it's an update, then use the matching existing filename; otherwise, use the media type to determine the file
    // extension.
    String filename = isUpdate ? existing.getName() : getFileNameForResource(name, headers);
    if (ts.deployProviderConfiguration(filename, content)) {
        try {
            if (isUpdate) {
                response = Response.noContent().build();
            } else {
                response = created(new URI(buildHref(request))).build();
            }
        } catch (URISyntaxException e) {
            log.invalidResourceURI(e.getInput(), e.getReason(), e);
            response = status(Response.Status.BAD_REQUEST).entity("{ \"error\" : \"Failed to deploy provider configuration " + name + "\" }").build();
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) GatewayServices(org.apache.knox.gateway.services.GatewayServices) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 12 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class TopologiesResource method getTopologies.

@GET
@Produces({ APPLICATION_JSON, APPLICATION_XML })
@Path(TOPOLOGIES_API_PATH)
public SimpleTopologyWrapper getTopologies() {
    GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    ArrayList<SimpleTopology> st = new ArrayList<SimpleTopology>();
    GatewayConfig conf = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
    for (org.apache.knox.gateway.topology.Topology t : ts.getTopologies()) {
        st.add(getSimpleTopology(t, conf));
    }
    Collections.sort(st, new TopologyComparator());
    SimpleTopologyWrapper stw = new SimpleTopologyWrapper();
    for (SimpleTopology t : st) {
        stw.topologies.add(t);
    }
    return stw;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) ArrayList(java.util.ArrayList) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 13 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class TopologiesResource method getProviderConfiguration.

@GET
@Produces({ APPLICATION_XML, APPLICATION_JSON, TEXT_PLAIN })
@Path(SINGLE_PROVIDERCONFIG_API_PATH)
public Response getProviderConfiguration(@PathParam("name") String name) {
    Response response;
    GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    File providerConfigFile = null;
    for (File pc : ts.getProviderConfigurations()) {
        // If the file name matches the specified id
        if (FilenameUtils.getBaseName(pc.getName()).equals(name)) {
            providerConfigFile = pc;
            break;
        }
    }
    if (providerConfigFile != null) {
        byte[] content = null;
        try {
            content = FileUtils.readFileToByteArray(providerConfigFile);
            response = ok().entity(content).build();
        } catch (IOException e) {
            log.failedToReadConfigurationFile(providerConfigFile.getAbsolutePath(), e);
            response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
        }
    } else {
        response = Response.status(Response.Status.NOT_FOUND).build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) GatewayServices(org.apache.knox.gateway.services.GatewayServices) IOException(java.io.IOException) File(java.io.File) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 14 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class TopologiesResource method deleteProviderConfiguration.

@DELETE
@Produces(APPLICATION_JSON)
@Path(SINGLE_PROVIDERCONFIG_API_PATH)
public Response deleteProviderConfiguration(@PathParam("name") String name) {
    Response response;
    GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    if (ts.deleteProviderConfiguration(name)) {
        response = ok().entity("{ \"deleted\" : \"provider config " + name + "\" }").build();
    } else {
        response = notModified().build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) GatewayServices(org.apache.knox.gateway.services.GatewayServices) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Example 15 with GatewayServices

use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.

the class TopologiesResource method getSimpleDescriptors.

@GET
@Produces({ APPLICATION_JSON })
@Path(DESCRIPTORS_API_PATH)
public HrefListing getSimpleDescriptors() {
    HrefListing listing = new HrefListing();
    listing.setHref(buildHref(request));
    GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    List<HrefListItem> descriptors = new ArrayList<>();
    TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    for (File descriptor : ts.getDescriptors()) {
        String id = FilenameUtils.getBaseName(descriptor.getName());
        descriptors.add(new HrefListItem(buildHref(id, request), descriptor.getName()));
    }
    listing.setItems(descriptors);
    return listing;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) ArrayList(java.util.ArrayList) File(java.io.File) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

GatewayServices (org.apache.knox.gateway.services.GatewayServices)75 Test (org.junit.Test)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 ServletContext (javax.servlet.ServletContext)22 Principal (java.security.Principal)21 JWTokenAuthority (org.apache.knox.gateway.services.security.token.JWTokenAuthority)21 AliasService (org.apache.knox.gateway.services.security.AliasService)20 HttpServletResponse (javax.servlet.http.HttpServletResponse)19 JWT (org.apache.knox.gateway.services.security.token.impl.JWT)18 Response (javax.ws.rs.core.Response)17 JWTToken (org.apache.knox.gateway.services.security.token.impl.JWTToken)17 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)17 HashMap (java.util.HashMap)14 Path (javax.ws.rs.Path)12 File (java.io.File)11 PrintWriter (java.io.PrintWriter)11 StringWriter (java.io.StringWriter)11 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)11 PrimaryPrincipal (org.apache.knox.gateway.security.PrimaryPrincipal)11 TokenResource (org.apache.knox.gateway.service.knoxtoken.TokenResource)11