SwitchYardRemotingServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ClassLoader setTCCL = null; boolean transactionPropagated = false; try { ServiceDomain domain = findDomain(request); ClassLoader loader = (ClassLoader) domain.getProperty(Deployment.CLASSLOADER_PROPERTY); setTCCL = Classes.setTCCL(loader); transactionPropagated = bridgeIncomingTransaction(request); RemoteMessage msg = _serializer.deserialize(request.getInputStream(), RemoteMessage.class); if (_log.isDebugEnabled()) { _log.debug("Remote servlet received request for service " + msg.getService()); } ServiceReference service = domain.getServiceReference(msg.getService()); SynchronousInOutHandler replyHandler = new SynchronousInOutHandler(); Exchange ex = msg.getOperation() == null ? service.createExchange(replyHandler) : service.createExchange(msg.getOperation(), replyHandler); Set<Credential> credentials = srce.extract(request); credentials.addAll(extractWebServiceSecurityCredentials(request, msg)); if (credentials != null && !credentials.isEmpty()) { SecurityContextManager scm = new SecurityContextManager(domain); scm.addCredentials(ex, credentials); } Message m = ex.createMessage(); if (msg.getContext() != null) { m.getContext().setProperties(msg.getContext().getProperties()); } m.setContent(msg.getContent()); if (_log.isDebugEnabled()) { _log.debug("Invoking service " + msg.getService()); } ex.send(m); RemoteMessage reply = null; if (ExchangePattern.IN_OUT.equals(ex.getPattern())) { replyHandler.waitForOut(); reply = createReplyMessage(ex); } else if (ExchangeState.FAULT.equals(ex.getState())) { reply = createReplyMessage(ex); } if (transactionPropagated) { bridgeOutgoingTransaction(); transactionPropagated = false; } if (reply != null) { OutputStream out = response.getOutputStream(); if (_log.isDebugEnabled()) { _log.debug("Writing reply message to HTTP response stream " + msg.getService()); } _serializer.serialize(reply, RemoteMessage.class, out); out.flush(); } else { response.setStatus(HttpServletResponse.SC_NO_CONTENT); if (_log.isDebugEnabled()) { _log.debug("No content to return for invocation of " + msg.getService()); } } } catch (SwitchYardException syEx) { if (_log.isDebugEnabled()) { _log.debug("Failed to process remote invocation", syEx); } RemoteMessage reply = new RemoteMessage(); reply.setFault(true); reply.setContent(syEx); _serializer.serialize(reply, RemoteMessage.class, response.getOutputStream()); response.getOutputStream().flush(); } finally { if (transactionPropagated) { bridgeOutgoingTransaction(); } if (setTCCL != null) { Classes.setTCCL(setTCCL); } } }  void doPost(HttpServletRequest request, HttpServletResponse response); void setEndpointPublisher(RemoteEndpointPublisher endpointPublisher);  }
@Test public void missingServiceName() { try { when(request.getHeader(HttpInvoker.SERVICE_HEADER)).thenReturn(null); servlet.doPost(request, response); Assert.fail("Expected failure due to missing service name on message"); } catch (Exception ex) { } }
@Test public void noOperationName() throws Exception { domain.registerServiceReference(TEST_SERVICE, new InOnlyService()); domain.registerService(TEST_SERVICE, new InOnlyService(), new MockHandler()); RemoteMessage msg = new RemoteMessage() .setService(TEST_SERVICE); setRequestMessage(msg); servlet.doPost(request, response); }
@Test public void noOperationNameMultipleOperations() throws Exception { MockHandler handler = new MockHandler(); domain.registerServiceReference(TEST_SERVICE, JavaService.fromClass(MyInterface.class)); domain.registerService(TEST_SERVICE, JavaService.fromClass(MyInterface.class), handler); RemoteMessage msg = new RemoteMessage() .setService(TEST_SERVICE); setRequestMessage(msg); try { servlet.doPost(request, response); Assert.fail("No operation was supplied with multiple service operations - this should fail!"); } catch (Exception ex) { } }
@Test public void operationNameMultipleOperations() throws Exception { MockHandler handler = new MockHandler(); domain.registerServiceReference(TEST_SERVICE, JavaService.fromClass(MyInterface.class)); domain.registerService(TEST_SERVICE, JavaService.fromClass(MyInterface.class), handler); RemoteMessage msg = new RemoteMessage() .setService(TEST_SERVICE) .setOperation("bar"); setRequestMessage(msg); servlet.doPost(request, response); Exchange ex = handler.getMessages().poll(300, TimeUnit.MILLISECONDS); Assert.assertNotNull(ex); Assert.assertEquals("bar", ex.getContract().getConsumerOperation().getName()); }