use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.
the class TestDataTransferResource method testCreateTransactionUnauthorized.
@Test
public void testCreateTransactionUnauthorized() throws Exception {
final HttpServletRequest req = createCommonHttpServletRequest();
final DataTransferResource resource = getDataTransferResource();
final HttpFlowFileServerProtocol serverProtocol = resource.getHttpFlowFileServerProtocol(null);
doThrow(new HandshakeException(ResponseCode.UNAUTHORIZED, "Unauthorized.")).when(serverProtocol).handshake(any());
final ServletContext context = null;
final UriInfo uriInfo = null;
final InputStream inputStream = null;
final Response response = resource.createPortTransaction("input-ports", "port-id", req, context, uriInfo, inputStream);
TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
assertEquals(401, response.getStatus());
assertEquals(ResponseCode.UNAUTHORIZED.getCode(), resultEntity.getResponseCode());
}
use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.
the class RemoteResourceFactory method receiveResourceNegotiation.
public static <T extends VersionedRemoteResource> T receiveResourceNegotiation(final Class<T> cls, final DataInputStream dis, final DataOutputStream dos, final Class<?>[] constructorArgClasses, final Object[] constructorArgs) throws IOException, HandshakeException {
final String resourceClassName = dis.readUTF();
final T resource;
try {
@SuppressWarnings("unchecked") final Class<T> resourceClass = (Class<T>) Class.forName(resourceClassName);
if (!cls.isAssignableFrom(resourceClass)) {
throw new HandshakeException("Expected to negotiate a Versioned Resource of type " + cls.getName() + " but received class name of " + resourceClassName);
}
final Constructor<T> ctr = resourceClass.getConstructor(constructorArgClasses);
resource = ctr.newInstance(constructorArgs);
} catch (final Throwable t) {
dos.write(ABORT);
final String errorMsg = "Unable to instantiate Versioned Resource of type " + resourceClassName;
dos.writeUTF(errorMsg);
dos.flush();
throw new HandshakeException(errorMsg);
}
final int version = dis.readInt();
final VersionNegotiator negotiator = resource.getVersionNegotiator();
if (negotiator.isVersionSupported(version)) {
dos.write(RESOURCE_OK);
dos.flush();
negotiator.setVersion(version);
return resource;
} else {
final Integer preferred = negotiator.getPreferredVersion(version);
if (preferred == null) {
dos.write(ABORT);
dos.flush();
throw new HandshakeException("Unable to negotiate an acceptable version of the resource " + resourceClassName);
}
dos.write(DIFFERENT_RESOURCE_VERSION);
dos.writeInt(preferred);
dos.flush();
return receiveResourceNegotiation(cls, dis, dos, constructorArgClasses, constructorArgs);
}
}
use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.
the class SocketFlowFileServerProtocol method doHandshake.
@Override
protected HandshakeProperties doHandshake(Peer peer) throws IOException, HandshakeException {
HandshakeProperties confirmed = new HandshakeProperties();
final CommunicationsSession commsSession = peer.getCommunicationsSession();
final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream());
final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
confirmed.setCommsIdentifier(dis.readUTF());
if (versionNegotiator.getVersion() >= 3) {
String transitUriPrefix = dis.readUTF();
if (!transitUriPrefix.endsWith("/")) {
transitUriPrefix = transitUriPrefix + "/";
}
confirmed.setTransitUriPrefix(transitUriPrefix);
}
final Map<String, String> properties = new HashMap<>();
final int numProperties = dis.readInt();
for (int i = 0; i < numProperties; i++) {
final String propertyName = dis.readUTF();
final String propertyValue = dis.readUTF();
properties.put(propertyName, propertyValue);
}
// evaluate the properties received
boolean responseWritten = false;
try {
validateHandshakeRequest(confirmed, peer, properties);
} catch (HandshakeException e) {
ResponseCode handshakeResult = e.getResponseCode();
if (handshakeResult.containsMessage()) {
handshakeResult.writeResponse(dos, e.getMessage());
} else {
handshakeResult.writeResponse(dos);
}
switch(handshakeResult) {
case UNAUTHORIZED:
case PORT_NOT_IN_VALID_STATE:
case PORTS_DESTINATION_FULL:
responseWritten = true;
break;
default:
throw e;
}
}
// send "OK" response
if (!responseWritten) {
ResponseCode.PROPERTIES_OK.writeResponse(dos);
}
return confirmed;
}
use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.
the class SocketFlowFileServerProtocol method negotiateCodec.
@Override
public FlowFileCodec negotiateCodec(final Peer peer) throws IOException, ProtocolException {
if (!handshakeCompleted) {
throw new IllegalStateException("Handshake has not been completed");
}
if (shutdown) {
throw new IllegalStateException("Protocol is shutdown");
}
logger.debug("{} Negotiating Codec with {} using {}", new Object[] { this, peer, peer.getCommunicationsSession() });
final CommunicationsSession commsSession = peer.getCommunicationsSession();
final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream());
final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
if (port == null) {
RemoteResourceFactory.rejectCodecNegotiation(dis, dos, "Cannot transfer FlowFiles because no port was specified");
}
// Negotiate the FlowFileCodec to use.
try {
negotiatedFlowFileCodec = RemoteResourceFactory.receiveCodecNegotiation(dis, dos);
logger.debug("{} Negotiated Codec {} with {}", new Object[] { this, negotiatedFlowFileCodec, peer });
return negotiatedFlowFileCodec;
} catch (final HandshakeException e) {
throw new ProtocolException(e.toString());
}
}
use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.
the class TestHttpFlowFileServerProtocol method testPortDestinationFull.
@Test
public void testPortDestinationFull() throws Exception {
final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
final Peer peer = getDefaultPeer();
((HttpServerCommunicationsSession) peer.getCommunicationsSession()).putHandshakeParam(HandshakeProperty.PORT_IDENTIFIER, "port-identifier");
final ProcessGroup processGroup = mock(ProcessGroup.class);
final RootGroupPort port = mock(RootGroupPort.class);
final PortAuthorizationResult authResult = mock(PortAuthorizationResult.class);
doReturn(true).when(processGroup).isRootGroup();
doReturn(port).when(processGroup).getOutputPort("port-identifier");
doReturn(authResult).when(port).checkUserAuthorization(any(String.class));
doReturn(true).when(authResult).isAuthorized();
doReturn(true).when(port).isValid();
doReturn(true).when(port).isRunning();
final Set<Connection> connections = new HashSet<>();
final Connection connection = mock(Connection.class);
connections.add(connection);
doReturn(connections).when(port).getConnections();
final FlowFileQueue flowFileQueue = mock(FlowFileQueue.class);
doReturn(flowFileQueue).when(connection).getFlowFileQueue();
doReturn(true).when(flowFileQueue).isFull();
serverProtocol.setRootProcessGroup(processGroup);
try {
serverProtocol.handshake(peer);
fail();
} catch (final HandshakeException e) {
assertEquals(ResponseCode.PORTS_DESTINATION_FULL, e.getResponseCode());
}
assertFalse(serverProtocol.isHandshakeSuccessful());
}
Aggregations