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));
}
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")));
}
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;
}
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 );
// ]
}
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);
}
}
Aggregations