1 17 package org.apache.servicemix.components.http; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.messaging.ExchangeStatus; 21 import javax.jbi.messaging.InOnly; 22 import javax.jbi.messaging.InOut; 23 import javax.jbi.messaging.MessageExchangeFactory; 24 import javax.jbi.messaging.NormalizedMessage; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.xml.transform.TransformerException ; 29 import java.io.IOException ; 30 31 37 public class HttpInOutBinding extends HttpBindingSupport { 38 39 private boolean defaultInOut = true; 40 41 public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException , 42 JBIException { 43 if (isInOutRequest(request, response)) { 45 processInOut(request, response); 46 } 47 else { 48 processInOnly(request, response); 49 } 50 } 51 52 public void processInOut(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException , 53 JBIException { 54 MessageExchangeFactory factory = getExchangeFactory(); 55 InOut exchange = factory.createInOutExchange(); 56 NormalizedMessage inMessage = exchange.createMessage(); 57 try { 58 getMarshaler().toNMS(exchange, inMessage, request); 59 exchange.setInMessage(inMessage); 60 boolean result = getDeliveryChannel().sendSync(exchange); 61 if (result) { 62 if (exchange.getStatus() == ExchangeStatus.ERROR) { 63 if (exchange.getError() != null) { 64 throw new ServletException (exchange.getError()); 65 } else { 66 throw new ServletException ("Exchange status is ERROR"); 67 } 68 } 69 getMarshaler().toResponse(exchange, exchange.getOutMessage(), response); 70 } 71 done(exchange); 72 response.setStatus(HttpServletResponse.SC_OK); 73 } 74 catch (IOException e) { 75 fail(exchange, e); 76 outputException(response, e); 77 } 78 catch (TransformerException e) { 79 fail(exchange, e); 80 outputException(response, e); 81 } 82 } 83 84 public void processInOnly(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException , 85 JBIException { 86 MessageExchangeFactory factory = getExchangeFactory(); 87 InOnly exchange = factory.createInOnlyExchange(); 88 NormalizedMessage inMessage = exchange.createMessage(); 89 try { 90 HttpMarshaler marshaler = getMarshaler(); 91 marshaler.toNMS(exchange, inMessage, request); 92 exchange.setInMessage(inMessage); 93 getDeliveryChannel().sendSync(exchange); 95 response.setStatus(HttpServletResponse.SC_OK); 96 } 97 catch (IOException e) { 98 fail(exchange, e); 99 outputException(response, e); 100 } 101 } 102 103 public boolean isDefaultInOut() { 106 return defaultInOut; 107 } 108 109 112 public void setDefaultInOut(boolean defaultInOut) { 113 this.defaultInOut = defaultInOut; 114 } 115 116 119 122 protected boolean isInOutRequest(HttpServletRequest request, HttpServletResponse response) { 123 return isDefaultInOut(); 124 } 125 126 127 } 128 | Popular Tags |