public void writeResponse(final DataOutputStream out) throws IOException { if (containsMessage()) { throw new IllegalArgumentException("ResponseCode " + code + " expects an explanation"); } out.write(getCodeSequence()); out.flush(); }
public static ResponseCode fromSequence(final byte[] value) { final int code = value[3] & 0xFF; return fromCode(code); }
public Response badRequestResponse(Exception e) { if (logger.isDebugEnabled()) { logger.debug("Client sent a bad request. {}", e.getMessage()); } TransactionResultEntity entity = new TransactionResultEntity(); entity.setResponseCode(ResponseCode.ABORT.getCode()); entity.setMessage(e.getMessage()); entity.setFlowFileSent(0); return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(entity).build(); }
public static Response read(final DataInputStream in) throws IOException, ProtocolException { final ResponseCode code = ResponseCode.readCode(in); final String message = code.containsMessage() ? in.readUTF() : null; return new Response(code, message); }
String clientChecksum = commSession.getChecksum(); logger.debug("readTransactionResponse. clientChecksum={}", clientChecksum); ResponseCode.CONFIRM_TRANSACTION.writeResponse(new DataOutputStream(bos), clientChecksum); break; case TRANSACTION_CONFIRMED: logger.debug("readTransactionResponse. finishing."); ResponseCode.TRANSACTION_FINISHED.writeResponse(new DataOutputStream(bos)); break; logger.debug("readTransactionResponse. returning CONTINUE_TRANSACTION."); ResponseCode.CONTINUE_TRANSACTION.writeResponse(new DataOutputStream(bos)); break; case TRANSACTION_CONFIRMED: if(responseCode.containsMessage()){ responseCode.writeResponse(new DataOutputStream(bos), ""); } else { responseCode.writeResponse(new DataOutputStream(bos));
if(StringUtils.isEmpty(commSession.getChecksum())){ ResponseCode.CONTINUE_TRANSACTION.writeResponse(dos); } else { ResponseCode.CONFIRM_TRANSACTION.writeResponse(dos, ""); } else { TransactionResultEntity transactionResult = apiClient.commitReceivingFlowFiles(transactionUrl, ResponseCode.CONFIRM_TRANSACTION, commSession.getChecksum()); ResponseCode responseCode = ResponseCode.fromCode(transactionResult.getResponseCode()); if(responseCode.containsMessage()){ String message = transactionResult.getMessage(); responseCode.writeResponse(dos, message == null ? "" : message); } else { responseCode.writeResponse(dos); ResponseCode.CONFIRM_TRANSACTION.writeResponse(dos, commSession.getChecksum()); break; case TRANSACTION_CONFIRMED: TransactionResultEntity resultEntity = apiClient.commitTransferFlowFiles(transactionUrl, ResponseCode.CONFIRM_TRANSACTION); ResponseCode responseCode = ResponseCode.fromCode(resultEntity.getResponseCode()); if(responseCode.containsMessage()){ responseCode.writeResponse(dos, resultEntity.getMessage()); } else { responseCode.writeResponse(dos);
public TransactionResultEntity commitReceivingFlowFiles(final String transactionUrl, final ResponseCode clientResponse, final String checksum) throws IOException { logger.debug("Sending commitReceivingFlowFiles request to transactionUrl: {}, clientResponse={}, checksum={}", transactionUrl, clientResponse, checksum); stopExtendingTtl(); final StringBuilder urlBuilder = new StringBuilder(transactionUrl).append("?responseCode=").append(clientResponse.getCode()); if (ResponseCode.CONFIRM_TRANSACTION.equals(clientResponse)) { urlBuilder.append("&checksum=").append(checksum); } final HttpDelete delete = createDelete(urlBuilder.toString()); delete.setHeader("Accept", "application/json"); delete.setHeader(HttpHeaders.PROTOCOL_VERSION, String.valueOf(transportProtocolVersionNegotiator.getVersion())); setHandshakeProperties(delete); try (CloseableHttpResponse response = getHttpClient().execute(delete)) { final int responseCode = response.getStatusLine().getStatusCode(); logger.debug("commitReceivingFlowFiles responseCode={}", responseCode); try (InputStream content = response.getEntity().getContent()) { switch (responseCode) { case RESPONSE_CODE_OK: return readResponse(content); case RESPONSE_CODE_BAD_REQUEST: return readResponse(content); default: throw handleErrResponse(responseCode, content); } } } }
if (responseCode == null) { inputErrMessage = "responseCode is required."; } else if (ResponseCode.BAD_CHECKSUM.getCode() != responseCode && ResponseCode.CONFIRM_TRANSACTION.getCode() != responseCode && ResponseCode.CANCEL_TRANSACTION.getCode() != responseCode) { inputErrMessage = "responseCode " + responseCode + " is invalid. "; entity.setResponseCode(ResponseCode.ABORT.getCode()); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); if (ResponseCode.CANCEL_TRANSACTION.getCode() == responseCode) { return cancelTransaction(transactionId, entity); commsSession.setResponseCode(ResponseCode.fromCode(responseCode)); entity.setResponseCode(commsSession.getResponseCode().getCode()); entity.setFlowFileSent(flowFileSent); if (ResponseCode.BAD_CHECKSUM.getCode() == responseCode && e.getMessage().contains("Received a BadChecksum response")) { entity.setResponseCode(ResponseCode.CANCEL_TRANSACTION.getCode()); return noCache(Response.ok(entity)).build(); } else {
@Override protected void writeTransactionResponse(ResponseCode response, String explanation) throws IOException { if(explanation == null){ response.writeResponse(dos); } else { response.writeResponse(dos, explanation); } } }
} catch (HandshakeException e) { ResponseCode handshakeResult = e.getResponseCode(); if(handshakeResult.containsMessage()){ handshakeResult.writeResponse(dos, e.getMessage()); } else { handshakeResult.writeResponse(dos); ResponseCode.PROPERTIES_OK.writeResponse(dos);
if (responseCode == null) { inputErrMessage = "responseCode is required."; } else if (ResponseCode.CONFIRM_TRANSACTION.getCode() != responseCode && ResponseCode.CANCEL_TRANSACTION.getCode() != responseCode) { inputErrMessage = "responseCode " + responseCode + " is invalid. "; entity.setResponseCode(ResponseCode.ABORT.getCode()); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); if (ResponseCode.CANCEL_TRANSACTION.getCode() == responseCode) { return cancelTransaction(transactionId, entity); entity.setResponseCode(ResponseCode.CONFIRM_TRANSACTION.getCode()); entity.setFlowFileSent(flowFileSent); HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession(); logger.error("Failed to process the request", e); if (ResponseCode.BAD_CHECKSUM.equals(commsSession.getResponseCode())) { entity.setResponseCode(commsSession.getResponseCode().getCode()); entity.setMessage(e.getMessage());
protected void writeTransactionResponse(boolean isTransfer, ResponseCode response, CommunicationsSession commsSession, String explanation) throws IOException { final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream()); if (explanation == null) { response.writeResponse(dos); } else { response.writeResponse(dos, explanation); } }
String clientChecksum = commSession.getChecksum(); logger.debug("readTransactionResponse. clientChecksum={}", clientChecksum); ResponseCode.CONFIRM_TRANSACTION.writeResponse(new DataOutputStream(bos), clientChecksum); break; case TRANSACTION_CONFIRMED: logger.debug("readTransactionResponse. finishing."); ResponseCode.TRANSACTION_FINISHED.writeResponse(new DataOutputStream(bos)); break; logger.debug("readTransactionResponse. returning CONTINUE_TRANSACTION."); ResponseCode.CONTINUE_TRANSACTION.writeResponse(new DataOutputStream(bos)); break; case TRANSACTION_CONFIRMED: if(responseCode.containsMessage()){ responseCode.writeResponse(new DataOutputStream(bos), ""); } else { responseCode.writeResponse(new DataOutputStream(bos));
public Response unexpectedErrorResponse(String portId, Exception e) { logger.error("Unexpected exception occurred. portId={}", portId); logger.error("Exception detail:", e); TransactionResultEntity entity = new TransactionResultEntity(); entity.setResponseCode(ResponseCode.ABORT.getCode()); entity.setMessage("Server encountered an exception."); entity.setFlowFileSent(0); return Response.serverError().entity(entity).type(MediaType.APPLICATION_JSON_TYPE).build(); }
public void writeResponse(final DataOutputStream out, final String explanation) throws IOException { if (!containsMessage()) { throw new IllegalArgumentException("ResponseCode " + code + " does not expect an explanation"); } out.write(getCodeSequence()); out.writeUTF(explanation); out.flush(); }
protected void writeTransactionResponse(boolean isTransfer, ResponseCode response, CommunicationsSession commsSession, String explanation) throws IOException { final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream()); if (explanation == null) { response.writeResponse(dos); } else { response.writeResponse(dos, explanation); } }
logger.debug("{} Canceling transaction. explanation={}", this, explanation); TransactionResultEntity resultEntity = apiClient.commitReceivingFlowFiles(transactionUrl, ResponseCode.CANCEL_TRANSACTION, null); ResponseCode cancelResponse = ResponseCode.fromCode(resultEntity.getResponseCode()); switch (cancelResponse) { case CANCEL_TRANSACTION: case BAD_CHECKSUM: { TransactionResultEntity resultEntity = apiClient.commitTransferFlowFiles(transactionUrl, ResponseCode.BAD_CHECKSUM); ResponseCode badChecksumCancelResponse = ResponseCode.fromCode(resultEntity.getResponseCode()); switch (badChecksumCancelResponse) { case CANCEL_TRANSACTION: logger.debug("{} Canceling transaction.", this); TransactionResultEntity resultEntity = apiClient.commitTransferFlowFiles(transactionUrl, ResponseCode.CANCEL_TRANSACTION); ResponseCode cancelResponse = ResponseCode.fromCode(resultEntity.getResponseCode()); switch (cancelResponse) { case CANCEL_TRANSACTION:
} catch (HandshakeException e) { ResponseCode handshakeResult = e.getResponseCode(); if(handshakeResult.containsMessage()){ handshakeResult.writeResponse(dos, e.getMessage()); } else { handshakeResult.writeResponse(dos); ResponseCode.PROPERTIES_OK.writeResponse(dos);
public Response unexpectedErrorResponse(String portId, String transactionId, Exception e) { logger.error("Unexpected exception occurred. portId={}, transactionId={}", portId, transactionId); logger.error("Exception detail:", e); TransactionResultEntity entity = new TransactionResultEntity(); entity.setResponseCode(ResponseCode.ABORT.getCode()); entity.setMessage("Server encountered an exception."); entity.setFlowFileSent(0); return Response.serverError().entity(entity).type(MediaType.APPLICATION_JSON_TYPE).build(); }
private IOException handleErrResponse(final int responseCode, final InputStream in) throws IOException { if (in == null) { return new IOException("Unexpected response code: " + responseCode); } final TransactionResultEntity errEntity = readResponse(in); final ResponseCode errCode = ResponseCode.fromCode(errEntity.getResponseCode()); switch (errCode) { case UNKNOWN_PORT: return new UnknownPortException(errEntity.getMessage()); case PORT_NOT_IN_VALID_STATE: return new PortNotRunningException(errEntity.getMessage()); default: switch (responseCode) { case RESPONSE_CODE_FORBIDDEN : return new HandshakeException(errEntity.getMessage()); default: return new IOException("Unexpected response code: " + responseCode + " errCode:" + errCode + " errMessage:" + errEntity.getMessage()); } } }