public static OMElement serializeHandlerConfiguration(HandlerConfigurationBean bean) { OMFactory factory = OMAbstractFactory.getOMFactory(); OMElement handler = factory.createOMElement("handler", null); handler.addAttribute(factory.createOMAttribute("class", null, bean.getHandlerClass())); if (bean.getTenant() != null) { handler.addAttribute(factory.createOMAttribute("tenant", null, bean.getTenant())); } StringBuilder sb = new StringBuilder(); for (String method : bean.getMethods()) { if (method != null && method.length() > 0) { sb.append(method).append(","); } } // Remove last "," if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); handler.addAttribute(factory.createOMAttribute("methods", null, sb.toString())); } for (String property : bean.getPropertyList()) { OMElement temp = factory.createOMElement("property", null); temp.addAttribute(factory.createOMAttribute("name", null, property)); OMElement xmlProperty = bean.getXmlProperties().get(property); if (xmlProperty != null) { // The serialization happens by adding the whole XML property value to the // bean. // Therefore if it is a XML property, we take that whole element. handler.addChild(xmlProperty); } else { String nonXMLProperty = bean.getNonXmlProperties().get(property); if (nonXMLProperty != null) { temp.setText(nonXMLProperty); handler.addChild(temp); } } } OMElement filter = factory.createOMElement("filter", null); filter.addAttribute( factory.createOMAttribute("class", null, bean.getFilter().getFilterClass())); for (String property : bean.getFilter().getPropertyList()) { OMElement temp = factory.createOMElement("property", null); temp.addAttribute(factory.createOMAttribute("name", null, property)); OMElement xmlProperty = bean.getFilter().getXmlProperties().get(property); if (xmlProperty != null) { temp.addAttribute(factory.createOMAttribute("type", null, "xml")); temp.addChild(xmlProperty); filter.addChild(temp); } else { String nonXMLProperty = bean.getFilter().getNonXmlProperties().get(property); if (nonXMLProperty != null) { temp.setText(nonXMLProperty); filter.addChild(temp); } } } handler.addChild(filter); return handler; }
public static boolean generateHandler(Registry configSystemRegistry, String resourceFullPath) throws RegistryException, XMLStreamException { RegistryContext registryContext = configSystemRegistry.getRegistryContext(); if (registryContext == null) { return false; } Resource resource = configSystemRegistry.get(resourceFullPath); if (resource != null) { String content = null; if (resource.getContent() != null) { content = RegistryUtils.decodeBytes((byte[]) resource.getContent()); } if (content != null) { OMElement handler = AXIOMUtil.stringToOM(content); if (handler != null) { OMElement dummy = OMAbstractFactory.getOMFactory().createOMElement("dummy", null); dummy.addChild(handler); try { configSystemRegistry.beginTransaction(); boolean status = RegistryConfigurationProcessor.updateHandler( dummy, configSystemRegistry.getRegistryContext(), HandlerLifecycleManager.USER_DEFINED_HANDLER_PHASE); configSystemRegistry.commitTransaction(); return status; } catch (Exception e) { configSystemRegistry.rollbackTransaction(); throw new RegistryException("Unable to add handler", e); } } } } return false; }
public void sendUsingMTOM(String fileName, String targetEPR) throws IOException { OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0"); OMElement payload = factory.createOMElement("uploadFileUsingMTOM", ns); OMElement request = factory.createOMElement("request", ns); OMElement image = factory.createOMElement("image", ns); FileDataSource fileDataSource = new FileDataSource(new File(fileName)); DataHandler dataHandler = new DataHandler(fileDataSource); OMText textData = factory.createOMText(dataHandler, true); image.addChild(textData); request.addChild(image); payload.addChild(request); ServiceClient serviceClient = new ServiceClient(); Options options = new Options(); options.setTo(new EndpointReference(targetEPR)); options.setAction("urn:uploadFileUsingMTOM"); options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE); options.setCallTransportCleanup(true); serviceClient.setOptions(options); OMElement response = serviceClient.sendReceive(payload); Assert.assertTrue( response .toString() .contains( "<m:testMTOM xmlns:m=\"http://services.samples/xsd\">" + "<m:test1>testMTOM</m:test1></m:testMTOM>")); }
private OMElement getTextElement(String content) { OMFactory factory = OMAbstractFactory.getOMFactory(); OMElement textElement = factory.createOMElement(TEXT_ELEMENT); if (content == null) { content = ""; } textElement.setText(content); return textElement; }
public static MessageContext sendUsingSwA(String fileName, String targetEPR) throws IOException { Options options = new Options(); options.setTo(new EndpointReference(targetEPR)); options.setAction("urn:uploadFileUsingSwA"); options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP); MessageContext mc = new MessageContext(); System.out.println("Sending file : " + fileName + " as SwA"); FileDataSource fileDataSource = new FileDataSource(new File(fileName)); DataHandler dataHandler = new DataHandler(fileDataSource); String attachmentID = mc.addAttachment(dataHandler); SOAPFactory factory = OMAbstractFactory.getSOAP11Factory(); SOAPEnvelope env = factory.getDefaultEnvelope(); OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); OMElement payload = factory.createOMElement("uploadFileUsingSwA", ns); OMElement request = factory.createOMElement("request", ns); OMElement imageId = factory.createOMElement("imageId", ns); imageId.setText(attachmentID); request.addChild(imageId); payload.addChild(request); env.getBody().addChild(payload); mc.setEnvelope(env); mepClient.addMessageContext(mc); mepClient.execute(true); MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE); SOAPBody body = response.getEnvelope().getBody(); String imageContentId = body.getFirstChildWithName( new QName("http://services.samples/xsd", "uploadFileUsingSwAResponse")) .getFirstChildWithName(new QName("http://services.samples/xsd", "response")) .getFirstChildWithName(new QName("http://services.samples/xsd", "imageId")) .getText(); Attachments attachment = response.getAttachmentMap(); dataHandler = attachment.getDataHandler(imageContentId); File tempFile = File.createTempFile("swa-", ".gif"); FileOutputStream fos = new FileOutputStream(tempFile); dataHandler.writeTo(fos); fos.flush(); fos.close(); System.out.println("Saved response to file : " + tempFile.getAbsolutePath()); return response; }
public static OMElement addExtraElements(OMElement data, HttpServletRequest request) { OMFactory fac = OMAbstractFactory.getOMFactory(); // adding required fields at the top of the xml which will help to easily read in service side OMElement operation = fac.createOMElement("operation", null); OMElement currentName = fac.createOMElement("currentName", null); OMElement currentNamespace = fac.createOMElement("currentNamespace", null); String operationValue = request.getParameter("operation"); if (operationValue != null) { operation.setText(operationValue); data.addChild(operation); } String name = request.getParameter("currentname"); if (name != null) { currentName.setText(name); data.addChild(currentName); } String namespace = request.getParameter("currentnamespace"); if (namespace != null) { currentNamespace.setText(namespace); data.addChild(currentNamespace); } return data; }
/** CEP Manager service which keeps track of Storm receivers and CEP publishers */ public class ManagerService { private static Logger log = Logger.getLogger(ManagerService.class); private OMFactory factory = OMAbstractFactory.getOMFactory(); private OMNamespace OMNamespace = factory.createOMNamespace(ManagerServiceConstants.NAMESPACE, "ns"); private HashMap<String, Set<Endpoint>> stormReceivers = new HashMap<String, Set<Endpoint>>(); private HashMap<String, Set<Endpoint>> cepPublishers = new HashMap<String, Set<Endpoint>>(); private static String getKey(String executionPlanName, String tenantId) { return executionPlanName + ":" + tenantId; } private static void insertToCollection( HashMap<String, Set<Endpoint>> collection, String key, Endpoint endpoint) { Set<Endpoint> endpointSet = collection.get(key); if (endpointSet == null) { endpointSet = new HashSet<Endpoint>(); collection.put(key, endpointSet); } endpointSet.add(endpoint); } public void registerStormReceiver(OMElement request) throws XMLStreamException { request.build(); request.detach(); OMElement tenantId = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_TENANT_ID)); OMElement executionPlan = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_EXEC_PLAN)); OMElement hostName = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_HOST_NAME)); OMElement port = request.getFirstChildWithName( new QName(ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_PORT)); String key = getKey(executionPlan.getText(), tenantId.getText()); int portNumber = Integer.parseInt(port.getText()); insertToCollection( stormReceivers, key, new Endpoint(portNumber, hostName.getText(), Endpoint.ENDPOINT_TYPE_STORM_RECEIVER)); log.info( "Registering Storm Receiver for " + key + " at " + hostName.getText() + ":" + port.getText()); } public void registerCepPublisher(OMElement request) throws XMLStreamException { request.build(); request.detach(); OMElement tenantId = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_TENANT_ID)); OMElement executionPlan = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_EXEC_PLAN)); OMElement hostName = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_HOST_NAME)); OMElement port = request.getFirstChildWithName( new QName(ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_PORT)); String key = getKey(executionPlan.getText(), tenantId.getText()); int portNumber = Integer.parseInt(port.getText()); insertToCollection( cepPublishers, key, new Endpoint(portNumber, hostName.getText(), Endpoint.ENDPOINT_TYPE_CEP_PUBLISHER)); log.info( "Registering CEP Publisher for " + key + " at " + hostName.getText() + ":" + port.getText()); } public OMElement getStormReceiver(OMElement request) throws XMLStreamException { request.build(); request.detach(); OMElement tenantId = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_TENANT_ID)); OMElement executionPlan = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_EXEC_PLAN)); OMElement requesterIp = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_REQUESTER_IP)); String key = getKey(executionPlan.getText(), tenantId.getText()); log.info("Storm receiver requested for " + key); Set<Endpoint> endpointSet = stormReceivers.get(key); Endpoint selectedEndpoint = selectEndpoint(endpointSet, requesterIp.getText()); OMElement response = factory.createOMElement( ManagerServiceConstants.ELEMENT_STORM_RECEIVER_RESPONSE, OMNamespace); if (selectedEndpoint != null) { OMElement hostNameElement = factory.createOMElement(ManagerServiceConstants.ELEMENT_HOST_NAME, OMNamespace); OMElement portElement = factory.createOMElement(ManagerServiceConstants.ELEMENT_PORT, OMNamespace); OMText hostNameText = factory.createOMText(hostNameElement, selectedEndpoint.getHostName()); OMText portText = factory.createOMText(portElement, Integer.toString(selectedEndpoint.getPort())); hostNameElement.addChild(hostNameText); portElement.addChild(portText); response.addChild(hostNameElement); response.addChild(portElement); log.info( "Returning Storm Receiver :" + selectedEndpoint.getHostName() + ":" + selectedEndpoint.getPort()); } else { log.warn("No Storm receiver registered " + key); } return response; } public OMElement getCEPPublisher(OMElement request) throws XMLStreamException { request.build(); request.detach(); OMElement tenantId = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_TENANT_ID)); OMElement executionPlan = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_EXEC_PLAN)); OMElement requesterIp = request.getFirstChildWithName( new QName( ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_REQUESTER_IP)); String key = getKey(executionPlan.getText(), tenantId.getText()); log.info("CEP Publisher requested for " + key); Set<Endpoint> endpointSet = cepPublishers.get(key); Endpoint selectedEndpoint = selectEndpoint(endpointSet, requesterIp.getText()); OMElement response = factory.createOMElement( ManagerServiceConstants.ELEMENT_CEP_PUBLISHER_RESPONSE, OMNamespace); if (selectedEndpoint != null) { OMElement hostNameElement = factory.createOMElement(ManagerServiceConstants.ELEMENT_HOST_NAME, OMNamespace); OMElement portElement = factory.createOMElement(ManagerServiceConstants.ELEMENT_PORT, OMNamespace); OMText hostNameText = factory.createOMText(hostNameElement, selectedEndpoint.getHostName()); OMText portText = factory.createOMText(portElement, Integer.toString(selectedEndpoint.getPort())); hostNameElement.addChild(hostNameText); portElement.addChild(portText); response.addChild(hostNameElement); response.addChild(portElement); log.info( "Returning CEP Publisher:" + selectedEndpoint.getHostName() + ":" + selectedEndpoint.getPort()); } else { log.warn("No CEP publishers registered " + key); } return response; } private Endpoint selectEndpoint(Set<Endpoint> endpointSet, String requesterIp) { Endpoint selectedEndpoint = null; if (endpointSet != null && !endpointSet.isEmpty()) { // If there's a storm receiver/cep publisher in the same host as requester IP select it if ("".equals(requesterIp) == false) { for (Endpoint endpoint : endpointSet) { if (endpoint.getHostName().equals(requesterIp)) { selectedEndpoint = endpoint; if (log.isDebugEnabled()) { log.debug( "Selecting" + endpoint.toString() + " since it's in the same host as the requester"); } break; } } } // If there are no endpoints in the same host. Select the endpoint with lease number of // connections if (selectedEndpoint == null) { int minConnectionCount = Integer.MAX_VALUE; for (Endpoint endpoint : endpointSet) { if (log.isDebugEnabled()) { log.debug( "Endpoint " + endpoint.toString() + " has " + endpoint.getConnectionCount() + " connections."); } if (endpoint.getConnectionCount() < minConnectionCount) { minConnectionCount = endpoint.getConnectionCount(); selectedEndpoint = endpoint; } } } selectedEndpoint.setConnectionCount(selectedEndpoint.getConnectionCount() + 1); } return selectedEndpoint; } }
/** * Actual transformation of the current message into a fault message * * @param synCtx the current message context * @param soapVersion SOAP version of the resulting fault desired * @param synLog the Synapse log to use */ private void makeSOAPFault(MessageContext synCtx, int soapVersion, SynapseLog synLog) { if (synLog.isTraceOrDebugEnabled()) { synLog.traceOrDebug("Creating a SOAP " + (soapVersion == SOAP11 ? "1.1" : "1.2") + " fault"); } // get the correct SOAP factory to be used SOAPFactory factory = (soapVersion == SOAP11 ? OMAbstractFactory.getSOAP11Factory() : OMAbstractFactory.getSOAP12Factory()); // create the SOAP fault document and envelope OMDocument soapFaultDocument = factory.createOMDocument(); SOAPEnvelope faultEnvelope = factory.getDefaultFaultEnvelope(); soapFaultDocument.addChild(faultEnvelope); // create the fault element if it is need SOAPFault fault = faultEnvelope.getBody().getFault(); if (fault == null) { fault = factory.createSOAPFault(); } // populate it setFaultCode(synCtx, factory, fault, soapVersion); setFaultReason(synCtx, factory, fault, soapVersion); setFaultNode(factory, fault); setFaultRole(factory, fault); setFaultDetail(synCtx, factory, fault); // set the all headers of original SOAP Envelope to the Fault Envelope if (synCtx.getEnvelope() != null) { SOAPHeader soapHeader = synCtx.getEnvelope().getHeader(); if (soapHeader != null) { for (Iterator iter = soapHeader.examineAllHeaderBlocks(); iter.hasNext(); ) { Object o = iter.next(); if (o instanceof SOAPHeaderBlock) { SOAPHeaderBlock header = (SOAPHeaderBlock) o; faultEnvelope.getHeader().addChild(header); } else if (o instanceof OMElement) { faultEnvelope.getHeader().addChild((OMElement) o); } } } } if (synLog.isTraceOrDebugEnabled()) { String msg = "Original SOAP Message : " + synCtx.getEnvelope().toString() + "Fault Message created : " + faultEnvelope.toString(); if (synLog.isTraceTraceEnabled()) { synLog.traceTrace(msg); } if (log.isTraceEnabled()) { log.trace(msg); } } // overwrite current message envelope with new fault envelope try { synCtx.setEnvelope(faultEnvelope); } catch (AxisFault af) { handleException( "Error replacing current SOAP envelope " + "with the fault envelope", af, synCtx); } if (synCtx.getFaultTo() != null) { synCtx.setTo(synCtx.getFaultTo()); } else if (synCtx.getReplyTo() != null) { synCtx.setTo(synCtx.getReplyTo()); } else { synCtx.setTo(null); } // set original messageID as relatesTo if (synCtx.getMessageID() != null) { RelatesTo relatesTo = new RelatesTo(synCtx.getMessageID()); synCtx.setRelatesTo(new RelatesTo[] {relatesTo}); } synLog.traceOrDebug("End : Fault mediator"); }