Search in sources :

Example 11 with NiFiWebConfigurationContext

use of org.apache.nifi.web.NiFiWebConfigurationContext in project nifi by apache.

the class JettyServer method start.

@Override
public void start() {
    try {
        ExtensionManager.discoverExtensions(systemBundle, bundles);
        ExtensionManager.logClassLoaderMapping();
        DocGenerator.generate(props, extensionMapping);
        // start the server
        server.start();
        // ensure everything started successfully
        for (Handler handler : server.getChildHandlers()) {
            // see if the handler is a web app
            if (handler instanceof WebAppContext) {
                WebAppContext context = (WebAppContext) handler;
                // cause it to be unavailable
                if (context.getUnavailableException() != null) {
                    startUpFailure(context.getUnavailableException());
                }
            }
        }
        // this must be done after starting the server (and ensuring there were no start up failures)
        if (webApiContext != null) {
            // give the web api the component ui extensions
            final ServletContext webApiServletContext = webApiContext.getServletHandler().getServletContext();
            webApiServletContext.setAttribute("nifi-ui-extensions", componentUiExtensions);
            // get the application context
            final WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(webApiServletContext);
            // component ui extensions
            if (CollectionUtils.isNotEmpty(componentUiExtensionWebContexts)) {
                final NiFiWebConfigurationContext configurationContext = webApplicationContext.getBean("nifiWebConfigurationContext", NiFiWebConfigurationContext.class);
                for (final WebAppContext customUiContext : componentUiExtensionWebContexts) {
                    // set the NiFi context in each custom ui servlet context
                    final ServletContext customUiServletContext = customUiContext.getServletHandler().getServletContext();
                    customUiServletContext.setAttribute("nifi-web-configuration-context", configurationContext);
                    // add the security filter to any ui extensions wars
                    final FilterHolder securityFilter = webApiContext.getServletHandler().getFilter("springSecurityFilterChain");
                    if (securityFilter != null) {
                        customUiContext.addFilter(securityFilter, "/*", EnumSet.allOf(DispatcherType.class));
                    }
                }
            }
            // content viewer extensions
            if (CollectionUtils.isNotEmpty(contentViewerWebContexts)) {
                for (final WebAppContext contentViewerContext : contentViewerWebContexts) {
                    // add the security filter to any content viewer  wars
                    final FilterHolder securityFilter = webApiContext.getServletHandler().getFilter("springSecurityFilterChain");
                    if (securityFilter != null) {
                        contentViewerContext.addFilter(securityFilter, "/*", EnumSet.allOf(DispatcherType.class));
                    }
                }
            }
            // content viewer controller
            if (webContentViewerContext != null) {
                final ContentAccess contentAccess = webApplicationContext.getBean("contentAccess", ContentAccess.class);
                // add the content access
                final ServletContext webContentViewerServletContext = webContentViewerContext.getServletHandler().getServletContext();
                webContentViewerServletContext.setAttribute("nifi-content-access", contentAccess);
                final FilterHolder securityFilter = webApiContext.getServletHandler().getFilter("springSecurityFilterChain");
                if (securityFilter != null) {
                    webContentViewerContext.addFilter(securityFilter, "/*", EnumSet.allOf(DispatcherType.class));
                }
            }
        }
        // ensure the web document war was loaded and provide the extension mapping
        if (webDocsContext != null) {
            final ServletContext webDocsServletContext = webDocsContext.getServletHandler().getServletContext();
            webDocsServletContext.setAttribute("nifi-extension-mapping", extensionMapping);
        }
        // in a cluster
        if (props.isNode()) {
            FlowService flowService = null;
            try {
                logger.info("Loading Flow...");
                ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(webApiContext.getServletContext());
                flowService = ctx.getBean("flowService", FlowService.class);
                // start and load the flow
                flowService.start();
                flowService.load(null);
                logger.info("Flow loaded successfully.");
            } catch (BeansException | LifeCycleStartException | IOException | FlowSerializationException | FlowSynchronizationException | UninheritableFlowException e) {
                // ensure the flow service is terminated
                if (flowService != null && flowService.isRunning()) {
                    flowService.stop(false);
                }
                logger.error("Unable to load flow due to: " + e, e);
                // cannot wrap the exception as they are not defined in a classloader accessible to the caller
                throw new Exception("Unable to load flow due to: " + e);
            }
        }
        // dump the application url after confirming everything started successfully
        dumpUrls();
    } catch (Exception ex) {
        startUpFailure(ex);
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ContentAccess(org.apache.nifi.web.ContentAccess) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) UninheritableFlowException(org.apache.nifi.controller.UninheritableFlowException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) SocketException(java.net.SocketException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) WebApplicationContext(org.springframework.web.context.WebApplicationContext) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) UninheritableFlowException(org.apache.nifi.controller.UninheritableFlowException) ServletContext(javax.servlet.ServletContext) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) DispatcherType(javax.servlet.DispatcherType) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) FlowService(org.apache.nifi.services.FlowService) BeansException(org.springframework.beans.BeansException)

Example 12 with NiFiWebConfigurationContext

use of org.apache.nifi.web.NiFiWebConfigurationContext in project nifi by apache.

the class ProcessorResource method getDetails.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("/details")
public Response getDetails(@QueryParam("processorId") final String processorId) {
    final NiFiWebConfigurationContext nifiWebContext = getWebConfigurationContext();
    final ComponentDetails componentDetails = ProcessorWebUtils.getComponentDetails(nifiWebContext, processorId, request);
    final Response.ResponseBuilder response = ProcessorWebUtils.applyCacheControl(Response.ok(componentDetails));
    return response.build();
}
Also used : Response(javax.ws.rs.core.Response) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) ComponentDetails(org.apache.nifi.web.ComponentDetails) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 13 with NiFiWebConfigurationContext

use of org.apache.nifi.web.NiFiWebConfigurationContext in project nifi by apache.

the class TestProcessorResource method testSetProperties.

@Test
public void testSetProperties() {
    final NiFiWebConfigurationContext niFiWebConfigurationContext = mock(NiFiWebConfigurationContext.class);
    final Map<String, String> properties = new HashMap<>();
    properties.put("jolt-transform", "jolt-transform-chain");
    final ComponentDetails componentDetails = new ComponentDetails.Builder().properties(properties).build();
    Mockito.when(servletContext.getAttribute(Mockito.anyString())).thenReturn(niFiWebConfigurationContext);
    Mockito.when(niFiWebConfigurationContext.updateComponent(any(NiFiWebConfigurationRequestContext.class), any(String.class), any(Map.class))).thenReturn(componentDetails);
    Response response = client().target(getBaseUri()).path("/standard/processor/properties").queryParam("processorId", "1").queryParam("clientId", "1").queryParam("revisionId", "1").request().put(Entity.json(JsonUtils.toJsonString(properties)));
    assertNotNull(response);
    JsonNode jsonNode = response.readEntity(JsonNode.class);
    assertNotNull(jsonNode);
    assertTrue(jsonNode.get("properties").get("jolt-transform").asText().equals("jolt-transform-chain"));
}
Also used : Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) NiFiWebConfigurationRequestContext(org.apache.nifi.web.NiFiWebConfigurationRequestContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) ComponentDetails(org.apache.nifi.web.ComponentDetails) HashMap(java.util.HashMap) Map(java.util.Map) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 14 with NiFiWebConfigurationContext

use of org.apache.nifi.web.NiFiWebConfigurationContext in project nifi by apache.

the class TestProcessorResource method testGetProcessorDetails.

@Test
public void testGetProcessorDetails() {
    final NiFiWebConfigurationContext niFiWebConfigurationContext = mock(NiFiWebConfigurationContext.class);
    final Map<String, String> allowableValues = new HashMap<>();
    final ComponentDescriptor descriptor = new ComponentDescriptor.Builder().name("test-name").allowableValues(allowableValues).build();
    final Map<String, ComponentDescriptor> descriptors = new HashMap<>();
    descriptors.put("jolt-transform", descriptor);
    final ComponentDetails componentDetails = new ComponentDetails.Builder().name("mytransform").type("org.apache.nifi.processors.standard.JoltTransformJSON").descriptors(descriptors).build();
    Mockito.when(servletContext.getAttribute(Mockito.anyString())).thenReturn(niFiWebConfigurationContext);
    Mockito.when(niFiWebConfigurationContext.getComponentDetails(any(NiFiWebRequestContext.class))).thenReturn(componentDetails);
    JsonNode value = client().target(getBaseUri()).path("/standard/processor/details").queryParam("processorId", "1").request().get(JsonNode.class);
    assertNotNull(value);
    try {
        assertTrue(value.get("name").asText().equals("mytransform"));
    } catch (Exception e) {
        fail("Failed due to: " + e.toString());
    }
}
Also used : HashMap(java.util.HashMap) ComponentDescriptor(org.apache.nifi.web.ComponentDescriptor) JsonNode(com.fasterxml.jackson.databind.JsonNode) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) ComponentDetails(org.apache.nifi.web.ComponentDetails) NiFiWebRequestContext(org.apache.nifi.web.NiFiWebRequestContext) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 15 with NiFiWebConfigurationContext

use of org.apache.nifi.web.NiFiWebConfigurationContext in project nifi by apache.

the class TestTransformJSONResource method testValidateWithCustomSpec.

@Test
public void testValidateWithCustomSpec() {
    final NiFiWebConfigurationContext niFiWebConfigurationContext = mock(NiFiWebConfigurationContext.class);
    final Map<String, String> properties = new HashMap<>();
    properties.put("jolt-transform", "jolt-transform-custom");
    final ComponentDetails componentDetails = new ComponentDetails.Builder().properties(properties).build();
    Mockito.when(servletContext.getAttribute(Mockito.anyString())).thenReturn(niFiWebConfigurationContext);
    Mockito.when(niFiWebConfigurationContext.getComponentDetails(any(NiFiWebRequestContext.class))).thenReturn(componentDetails);
    JoltSpecificationDTO joltSpecificationDTO = new JoltSpecificationDTO("jolt-transform-custom", "[{ \"operation\": \"default\", \"spec\":{ \"custom-id\" :4 }}]");
    joltSpecificationDTO.setCustomClass("TestCustomJoltTransform");
    joltSpecificationDTO.setModules("src/test/resources/TestTransformJSONResource/TestCustomJoltTransform.jar");
    ValidationDTO validate = client().target(getBaseUri()).path("/standard/transformjson/validate").request().post(Entity.json(joltSpecificationDTO), ValidationDTO.class);
    assertNotNull(validate);
    assertTrue(validate.isValid());
}
Also used : HashMap(java.util.HashMap) JoltSpecificationDTO(org.apache.nifi.web.standard.api.transformjson.dto.JoltSpecificationDTO) ValidationDTO(org.apache.nifi.web.standard.api.transformjson.dto.ValidationDTO) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) ComponentDetails(org.apache.nifi.web.ComponentDetails) NiFiWebRequestContext(org.apache.nifi.web.NiFiWebRequestContext) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

NiFiWebConfigurationContext (org.apache.nifi.web.NiFiWebConfigurationContext)20 Path (javax.ws.rs.Path)10 Produces (javax.ws.rs.Produces)10 ComponentDetails (org.apache.nifi.web.ComponentDetails)10 NiFiWebRequestContext (org.apache.nifi.web.NiFiWebRequestContext)9 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)8 Criteria (org.apache.nifi.update.attributes.Criteria)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)6 Rule (org.apache.nifi.update.attributes.Rule)6 NiFiWebConfigurationRequestContext (org.apache.nifi.web.NiFiWebConfigurationRequestContext)6 JerseyTest (org.glassfish.jersey.test.JerseyTest)6 GET (javax.ws.rs.GET)5 RuleDTO (org.apache.nifi.update.attributes.dto.RuleDTO)5 Consumes (javax.ws.rs.Consumes)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 JoltSpecificationDTO (org.apache.nifi.web.standard.api.transformjson.dto.JoltSpecificationDTO)4 ValidationDTO (org.apache.nifi.web.standard.api.transformjson.dto.ValidationDTO)4 NotFoundException (javax.ws.rs.NotFoundException)3 PUT (javax.ws.rs.PUT)3