Search in sources :

Example 6 with BadRequestException

use of org.apache.nifi.remote.exception.BadRequestException in project nifi by apache.

the class ApplicationResource method negotiateTransportProtocolVersion.

// -----------------
// HTTP site to site
// -----------------
protected Integer negotiateTransportProtocolVersion(final HttpServletRequest req, final VersionNegotiator transportProtocolVersionNegotiator) throws BadRequestException {
    String protocolVersionStr = req.getHeader(HttpHeaders.PROTOCOL_VERSION);
    if (isEmpty(protocolVersionStr)) {
        throw new BadRequestException("Protocol version was not specified.");
    }
    final Integer requestedProtocolVersion;
    try {
        requestedProtocolVersion = Integer.valueOf(protocolVersionStr);
    } catch (NumberFormatException e) {
        throw new BadRequestException("Specified protocol version was not in a valid number format: " + protocolVersionStr);
    }
    Integer protocolVersion;
    if (transportProtocolVersionNegotiator.isVersionSupported(requestedProtocolVersion)) {
        return requestedProtocolVersion;
    } else {
        protocolVersion = transportProtocolVersionNegotiator.getPreferredVersion(requestedProtocolVersion);
    }
    if (protocolVersion == null) {
        throw new BadRequestException("Specified protocol version is not supported: " + protocolVersionStr);
    }
    return protocolVersion;
}
Also used : BadRequestException(org.apache.nifi.remote.exception.BadRequestException)

Example 7 with BadRequestException

use of org.apache.nifi.remote.exception.BadRequestException in project nifi by apache.

the class DataTransferResource method receiveFlowFiles.

@POST
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.TEXT_PLAIN)
@Path("input-ports/{portId}/transactions/{transactionId}/flow-files")
@ApiOperation(value = "Transfer flow files to the input port", response = String.class, authorizations = { @Authorization(value = "Write - /data-transfer/input-ports/{uuid}") })
@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 = 404, message = "The specified resource could not be found."), @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."), @ApiResponse(code = 503, message = "NiFi instance is not ready for serving request, or temporarily overloaded. Retrying the same request later may be successful") })
public Response receiveFlowFiles(@ApiParam(value = "The input port id.", required = true) @PathParam("portId") String portId, @PathParam("transactionId") String transactionId, @Context HttpServletRequest req, @Context ServletContext context, InputStream inputStream) {
    // authorize access
    serviceFacade.authorizeAccess(lookup -> {
        authorizeDataTransfer(lookup, ResourceType.InputPort, portId);
    });
    final ValidateRequestResult validationResult = validateResult(req, portId, transactionId);
    if (validationResult.errResponse != null) {
        return validationResult.errResponse;
    }
    logger.debug("receiveFlowFiles request: portId={}", portId);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Peer peer = constructPeer(req, inputStream, out, portId, transactionId);
    final int transportProtocolVersion = validationResult.transportProtocolVersion;
    try {
        HttpFlowFileServerProtocol serverProtocol = initiateServerProtocol(req, peer, transportProtocolVersion);
        int numOfFlowFiles = serverProtocol.getPort().receiveFlowFiles(peer, serverProtocol);
        logger.debug("finished receiving flow files, numOfFlowFiles={}", numOfFlowFiles);
        if (numOfFlowFiles < 1) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Client should send request when there is data to send. There was no flow file sent.").build();
        }
    } catch (HandshakeException e) {
        return responseCreator.handshakeExceptionResponse(e);
    } catch (NotAuthorizedException e) {
        return responseCreator.unauthorizedResponse(e);
    } catch (BadRequestException | RequestExpiredException e) {
        return responseCreator.badRequestResponse(e);
    } catch (Exception e) {
        return responseCreator.unexpectedErrorResponse(portId, e);
    }
    String serverChecksum = ((HttpServerCommunicationsSession) peer.getCommunicationsSession()).getChecksum();
    return responseCreator.acceptedResponse(transactionManager, serverChecksum, transportProtocolVersion);
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) Peer(org.apache.nifi.remote.Peer) StandardHttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.StandardHttpFlowFileServerProtocol) HttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol) RequestExpiredException(org.apache.nifi.remote.exception.RequestExpiredException) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) NotAuthorizedException(org.apache.nifi.remote.exception.NotAuthorizedException) BadRequestException(org.apache.nifi.remote.exception.BadRequestException) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) WebApplicationException(javax.ws.rs.WebApplicationException) AccessDeniedException(org.apache.nifi.authorization.AccessDeniedException) NotAuthorizedException(org.apache.nifi.remote.exception.NotAuthorizedException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) RequestExpiredException(org.apache.nifi.remote.exception.RequestExpiredException) BadRequestException(org.apache.nifi.remote.exception.BadRequestException) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

BadRequestException (org.apache.nifi.remote.exception.BadRequestException)7 IOException (java.io.IOException)6 NotAuthorizedException (org.apache.nifi.remote.exception.NotAuthorizedException)5 RequestExpiredException (org.apache.nifi.remote.exception.RequestExpiredException)5 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnknownHostException (java.net.UnknownHostException)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 ProcessException (org.apache.nifi.processor.exception.ProcessException)3 ProtocolException (org.apache.nifi.remote.exception.ProtocolException)3 TransmissionDisabledException (org.apache.nifi.remote.exception.TransmissionDisabledException)3 GET (javax.ws.rs.GET)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 AccessDeniedException (org.apache.nifi.authorization.AccessDeniedException)2 Peer (org.apache.nifi.remote.Peer)2 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)2 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)2