Search in sources :

Example 36 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class PropertyTopologyBuilderTest method testBuildSuccessfulForProviderProperty.

@Test
public void testBuildSuccessfulForProviderProperty() {
    PropertyTopologyBuilder propertyTopologyBuilder = new PropertyTopologyBuilder();
    propertyTopologyBuilder.addProperty(new Property("topology.gateway.provider.authentication.ShiroProvider.enabled", "value"));
    Topology topology = propertyTopologyBuilder.build();
    assertThat(topology, notNullValue());
    assertThat(topology.getProviders().size(), is(1));
    assertThat(topology.getProviders().iterator().next().isEnabled(), is(false));
}
Also used : Topology(org.apache.knox.gateway.topology.Topology) Property(org.apache.knox.gateway.topology.builder.property.Property) Test(org.junit.Test)

Example 37 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class TopologyMarshallerTest method testTopologyMarshalling.

@Test
public void testTopologyMarshalling() throws Exception {
    Topology topology = new Topology();
    Application app = new Application();
    app.setName("test-app-name");
    topology.addApplication(app);
    StringWriter writer = new StringWriter();
    String xml;
    Map<String, Object> properties = new HashMap<>(2);
    properties.put("eclipselink-oxm-xml", "org/apache/knox/gateway/topology/topology_binding-xml.xml");
    properties.put("eclipselink.media-type", "application/xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Topology.class.getPackage().getName(), Topology.class.getClassLoader(), properties);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(topology, writer);
    writer.close();
    xml = writer.toString();
    assertThat(the(xml), hasXPath("/topology/application/name", returningAString(), is("test-app-name")));
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) JAXBContext(javax.xml.bind.JAXBContext) Topology(org.apache.knox.gateway.topology.Topology) XpathReturnType.returningAString(org.xmlmatchers.xpath.XpathReturnType.returningAString) Application(org.apache.knox.gateway.topology.Application) Test(org.junit.Test)

Example 38 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class DeploymentFactory method toStringAsset.

private static Asset toStringAsset(Topology topology) {
    StringWriter writer = new StringWriter();
    String xml;
    try {
        Map<String, Object> properties = new HashMap<>(2);
        properties.put("eclipselink-oxm-xml", "org/apache/knox/gateway/topology/topology_binding-xml.xml");
        properties.put("eclipselink.media-type", "application/xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Topology.class.getPackage().getName(), Topology.class.getClassLoader(), properties);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(topology, writer);
        writer.close();
        xml = writer.toString();
    } catch (IOException e) {
        throw new DeploymentException("Failed to marshall topology.", e);
    } catch (JAXBException e) {
        throw new DeploymentException("Failed to marshall topology.", e);
    }
    StringAsset asset = new StringAsset(xml);
    return asset;
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Topology(org.apache.knox.gateway.topology.Topology) IOException(java.io.IOException)

Example 39 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class GatewayFilter method doFilter.

@SuppressWarnings("unchecked")
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
    HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
    // TODO: The resulting pathInfo + query needs to be added to the servlet context somehow so that filters don't need to rebuild it.  This is done in HttpClientDispatch right now for example.
    String servlet = httpRequest.getServletPath();
    String path = httpRequest.getPathInfo();
    String query = httpRequest.getQueryString();
    String requestPath = (servlet == null ? "" : servlet) + (path == null ? "" : path);
    String requestPathWithQuery = requestPath + (query == null ? "" : "?" + query);
    Template pathWithQueryTemplate;
    try {
        pathWithQueryTemplate = Parser.parseLiteral(requestPathWithQuery);
    } catch (URISyntaxException e) {
        throw new ServletException(e);
    }
    String contextWithPathAndQuery = httpRequest.getContextPath() + requestPathWithQuery;
    LOG.receivedRequest(httpRequest.getMethod(), requestPath);
    servletRequest.setAttribute(AbstractGatewayFilter.SOURCE_REQUEST_URL_ATTRIBUTE_NAME, pathWithQueryTemplate);
    servletRequest.setAttribute(AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME, contextWithPathAndQuery);
    Matcher<Chain>.Match match = chains.match(pathWithQueryTemplate);
    // if there was no match then look for a default service for the topology
    if (match == null) {
        Topology topology = (Topology) servletRequest.getServletContext().getAttribute("org.apache.knox.gateway.topology");
        if (topology != null) {
            String defaultServicePath = topology.getDefaultServicePath();
            if (defaultServicePath != null) {
                try {
                    String newPathWithQuery = defaultServicePath + "/" + pathWithQueryTemplate;
                    match = chains.match(Parser.parseLiteral(newPathWithQuery));
                    String origUrl = ((HttpServletRequest) servletRequest).getRequestURL().toString();
                    String url = origUrl;
                    if (path.equals("/")) {
                        url += defaultServicePath;
                    } else {
                        int index = origUrl.indexOf(path);
                        url = origUrl.substring(0, index) + "/" + defaultServicePath + path;
                    }
                    String contextPath = defaultServicePath;
                    servletRequest = new ForwardedRequest((HttpServletRequest) servletRequest, contextPath, url);
                } catch (URISyntaxException e) {
                    throw new ServletException(e);
                }
            }
        }
    }
    assignCorrelationRequestId();
    // Populate Audit/correlation parameters
    AuditContext auditContext = auditService.getContext();
    auditContext.setTargetServiceName(match == null ? null : match.getValue().getResourceRole());
    auditContext.setRemoteIp(getRemoteAddress(servletRequest));
    auditContext.setRemoteHostname(servletRequest.getRemoteHost());
    auditor.audit(Action.ACCESS, contextWithPathAndQuery, ResourceType.URI, ActionOutcome.UNAVAILABLE, RES.requestMethod(((HttpServletRequest) servletRequest).getMethod()));
    if (match != null) {
        Chain chain = match.getValue();
        servletRequest.setAttribute(AbstractGatewayFilter.TARGET_SERVICE_ROLE, chain.getResourceRole());
        try {
            chain.doFilter(servletRequest, servletResponse);
        } catch (IOException | RuntimeException | ThreadDeath | ServletException e) {
            LOG.failedToExecuteFilter(e);
            auditor.audit(Action.ACCESS, contextWithPathAndQuery, ResourceType.URI, ActionOutcome.FAILURE);
            throw e;
        } catch (Throwable e) {
            LOG.failedToExecuteFilter(e);
            auditor.audit(Action.ACCESS, contextWithPathAndQuery, ResourceType.URI, ActionOutcome.FAILURE);
            throw new ServletException(e);
        } finally {
            // Make sure to destroy the correlationContext to prevent threading issues
            CorrelationServiceFactory.getCorrelationService().detachContext();
        }
    } else {
        LOG.failedToMatchPath(requestPath);
        httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
        // Make sure to destroy the correlationContext to prevent threading issues
        CorrelationServiceFactory.getCorrelationService().detachContext();
    }
// KAM[ Don't do this or the Jetty default servlet will overwrite any response setup by the filter.
// filterChain.doFilter( servletRequest, servletResponse );
// ]
}
Also used : FilterChain(javax.servlet.FilterChain) Matcher(org.apache.knox.gateway.util.urltemplate.Matcher) HttpServletResponse(javax.servlet.http.HttpServletResponse) URISyntaxException(java.net.URISyntaxException) Topology(org.apache.knox.gateway.topology.Topology) IOException(java.io.IOException) AuditContext(org.apache.knox.gateway.audit.api.AuditContext) Template(org.apache.knox.gateway.util.urltemplate.Template) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException)

Example 40 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class GatewayServer method cleanupTopologyDeployments.

private void cleanupTopologyDeployments() {
    File deployDir = new File(config.getGatewayDeploymentDir());
    TopologyService ts = getGatewayServices().getService(GatewayServices.TOPOLOGY_SERVICE);
    for (Topology topology : ts.getTopologies()) {
        cleanupTopologyDeployments(deployDir, topology);
    }
}
Also used : Topology(org.apache.knox.gateway.topology.Topology) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) TopologyService(org.apache.knox.gateway.services.topology.TopologyService)

Aggregations

Topology (org.apache.knox.gateway.topology.Topology)52 Test (org.junit.Test)36 HashMap (java.util.HashMap)23 Service (org.apache.knox.gateway.topology.Service)22 File (java.io.File)20 Provider (org.apache.knox.gateway.topology.Provider)20 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)17 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)10 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)9 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)9 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)9 IOException (java.io.IOException)8 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)8 URL (java.net.URL)7 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)7 Application (org.apache.knox.gateway.topology.Application)7 Param (org.apache.knox.gateway.topology.Param)7 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)7 Document (org.w3c.dom.Document)7 Digester (org.apache.commons.digester3.Digester)6