Search in sources :

Example 11 with ControllerDTO

use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.

the class SiteInfoProvider method getSiteToSiteHttpPort.

/**
 * @return the port that the remote instance is listening on for
 * HTTP(S) site-to-site communication, or <code>null</code> if the remote instance
 * is not configured to allow site-to-site communications.
 *
 * @throws IOException if unable to communicate with the remote instance
 */
public Integer getSiteToSiteHttpPort() throws IOException {
    Integer listeningHttpPort;
    remoteInfoReadLock.lock();
    try {
        listeningHttpPort = this.siteToSiteHttpPort;
        if (listeningHttpPort != null && this.remoteRefreshTime > System.currentTimeMillis() - REMOTE_REFRESH_MILLIS) {
            return listeningHttpPort;
        }
    } finally {
        remoteInfoReadLock.unlock();
    }
    final ControllerDTO controller = refreshRemoteInfo();
    listeningHttpPort = controller.getRemoteSiteHttpListeningPort();
    return listeningHttpPort;
}
Also used : ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO)

Example 12 with ControllerDTO

use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.

the class SiteInfoProvider method refreshRemoteInfo.

private ControllerDTO refreshRemoteInfo() throws IOException {
    final ControllerDTO controller;
    final URI connectedClusterUrl;
    try (final SiteToSiteRestApiClient apiClient = createSiteToSiteRestApiClient(sslContext, proxy)) {
        controller = apiClient.getController(clusterUrls);
        try {
            connectedClusterUrl = new URI(apiClient.getBaseUrl());
        } catch (URISyntaxException e) {
            // This should not happen since apiClient has successfully communicated with this URL.
            throw new RuntimeException("Failed to parse connected cluster URL due to " + e);
        }
    }
    remoteInfoWriteLock.lock();
    try {
        this.siteToSitePort = controller.getRemoteSiteListeningPort();
        this.siteToSiteHttpPort = controller.getRemoteSiteHttpListeningPort();
        this.siteToSiteSecure = controller.isSiteToSiteSecure();
        this.activeClusterUrl = connectedClusterUrl;
        inputPortMap.clear();
        for (final PortDTO inputPort : controller.getInputPorts()) {
            inputPortMap.put(inputPort.getName(), inputPort.getId());
        }
        outputPortMap.clear();
        for (final PortDTO outputPort : controller.getOutputPorts()) {
            outputPortMap.put(outputPort.getName(), outputPort.getId());
        }
        this.remoteRefreshTime = System.currentTimeMillis();
    } finally {
        remoteInfoWriteLock.unlock();
    }
    return controller;
}
Also used : PortDTO(org.apache.nifi.web.api.dto.PortDTO) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 13 with ControllerDTO

use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.

the class SiteToSiteResource method getSiteToSiteDetails.

/**
 * Returns the details of this NiFi.
 *
 * @return A controllerEntity.
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Returns the details about this NiFi necessary to communicate via site to site", response = ControllerEntity.class, authorizations = { @Authorization(value = "Read - /site-to-site") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response getSiteToSiteDetails(@Context HttpServletRequest req) {
    authorizeSiteToSite();
    if (isReplicateRequest()) {
        return replicate(HttpMethod.GET);
    }
    // get the controller dto
    final ControllerDTO controller = serviceFacade.getSiteToSiteDetails();
    // build the response entity
    final ControllerEntity entity = new ControllerEntity();
    entity.setController(controller);
    if (isEmpty(req.getHeader(HttpHeaders.PROTOCOL_VERSION))) {
        // This indicates the client uses older NiFi version,
        // which strictly read JSON properties and fail with unknown properties.
        // Convert result entity so that old version clients can understand.
        logger.debug("Converting result to provide backward compatibility...");
        controller.setRemoteSiteHttpListeningPort(null);
    }
    // generate the response
    return noCache(Response.ok(entity)).build();
}
Also used : ControllerEntity(org.apache.nifi.web.api.entity.ControllerEntity) ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 14 with ControllerDTO

use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.

the class TestSiteToSiteResource method testGetControllerForOlderVersion.

@Test
public void testGetControllerForOlderVersion() throws Exception {
    final HttpServletRequest req = mock(HttpServletRequest.class);
    final NiFiServiceFacade serviceFacade = mock(NiFiServiceFacade.class);
    final ControllerEntity controllerEntity = new ControllerEntity();
    final ControllerDTO controller = new ControllerDTO();
    controllerEntity.setController(controller);
    controller.setRemoteSiteHttpListeningPort(8080);
    controller.setRemoteSiteListeningPort(9990);
    doReturn(controller).when(serviceFacade).getSiteToSiteDetails();
    final SiteToSiteResource resource = getSiteToSiteResource(serviceFacade);
    final Response response = resource.getSiteToSiteDetails(req);
    ControllerEntity resultEntity = (ControllerEntity) response.getEntity();
    assertEquals(200, response.getStatus());
    assertNull("remoteSiteHttpListeningPort should be null since older version doesn't recognize this field" + " and throws JSON mapping exception.", resultEntity.getController().getRemoteSiteHttpListeningPort());
    assertEquals("Other fields should be retained.", new Integer(9990), controllerEntity.getController().getRemoteSiteListeningPort());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) ControllerEntity(org.apache.nifi.web.api.entity.ControllerEntity) ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO) Test(org.junit.Test)

Aggregations

ControllerDTO (org.apache.nifi.web.api.dto.ControllerDTO)14 SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 LinkedHashSet (java.util.LinkedHashSet)4 PortDTO (org.apache.nifi.web.api.dto.PortDTO)4 ControllerEntity (org.apache.nifi.web.api.entity.ControllerEntity)3 URISyntaxException (java.net.URISyntaxException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Response (javax.ws.rs.core.Response)2 NiFiServiceFacade (org.apache.nifi.web.NiFiServiceFacade)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 CertificateException (java.security.cert.CertificateException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1