📄 samplemtomtests.java
字号:
* @return String - the input string or null * @throws Exception */ public String testMtomWithDispatch(String result) throws Exception { Service svc; System.out.println(">>---------------------------------------"); System.out.println(">>MTOM Dispatch Test"); init(); // Set the data inside of the appropriate object ImageDepot imageDepot = new ObjectFactory().createImageDepot(); imageDepot.setImageData(content); if (soap12) { svc = Service.create(serviceName12); svc.addPort(portNameDispatch, SOAPBinding.SOAP12HTTP_BINDING, uriString + urlSuffix); } else { svc = Service.create(serviceName11); svc.addPort(portNameDispatch, SOAPBinding.SOAP11HTTP_BINDING, uriString + urlSuffix); } // Setup the necessary JAX-WS artifacts JAXBContext jbc = JAXBContext .newInstance("org.apache.axis2.jaxws.samples.mtom"); Dispatch<Object> dispatch = svc.createDispatch(portNameDispatch, jbc, Service.Mode.PAYLOAD); BindingProvider bp = (BindingProvider) dispatch; bp.getRequestContext().put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE); bp.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, "sendImage"); // Set the actual flag to enable MTOM SOAPBinding binding = (SOAPBinding) dispatch.getBinding(); binding.setMTOMEnabled(true); // Create the request wrapper bean ObjectFactory factory = new ObjectFactory(); SendImage request = factory.createSendImage(); request.setInput(imageDepot); if (null != result) { result = result.concat("Invoking Dispatch<Object> with a binary payload\n"); } else { System.out .println(">>MTOM Invoking Dispatch<Object> with a binary payload"); } // Send the image and process the response image try { SendImageResponse response = (SendImageResponse) dispatch.invoke(request); if (null != result) { if (response != null) { result = result.concat("MTOM Dispatch Response received - " + response.getOutput().getImageData().getContentType()); } else { result = result.concat("ERROR: MTOM Dispatch NULL Response received"); } } else { if (response != null) { System.out.println(">>MTOM Response received"); System.out .println(">>MTOM Writing returned image to dispatch_response.gif"); ImageDepot responseContent = response.getOutput(); processImageDepot(responseContent, "dispatch_response.gif"); } else { System.out.println(">> [ERROR] - Response from the server was NULL"); } } } catch (Exception e) { if (null != result) { result = result.concat(">> [ERROR] - Exception making connection."); } System.out.println(">> [ERROR] - Exception making connection."); e.printStackTrace(); } System.out.println(">>MTOM Dispatch Done"); return (result); } /** * Tests sending an image with MTOM, using a JAX-WS Dynamic Proxy. * * @param result - String - used to pass back results to servlet. * @return String - the input string or null * @throws Exception */ public String testMtomWithProxy(String result, URL url) throws Exception { ImageDepot response; System.out.println(">>---------------------------------------"); System.out.println(">>MTOM Proxy Test"); init(); // Set the data inside of the appropriate object ImageDepot imageDepot = new ObjectFactory().createImageDepot(); imageDepot.setImageData(content); if (null != result) { result = result.concat("Invoking MTOM proxy with a binary payload\n"); } else { System.out.println(">>MTOM Invoking proxy with a binary payload"); } // Setup the necessary JAX-WS artifacts try { if (soap12) { // Use the generated proxy MtomSample12PortProxy proxy = new MtomSample12PortProxy(url); proxy._getDescriptor().setEndpoint(uriString + urlSuffix); // Enable MTOM BindingProvider bp = (BindingProvider) proxy._getDescriptor().getProxy(); SOAPBinding binding = (SOAPBinding) bp.getBinding(); binding.setMTOMEnabled(true); // Send the image and process the response image response = proxy.sendImage(imageDepot); } else { // SOAP 1.1 Create the service // Use the generated proxy MtomSamplePortProxy proxy = new MtomSamplePortProxy(url); proxy._getDescriptor().setEndpoint(uriString + urlSuffix); // Enable MTOM BindingProvider bp = (BindingProvider) proxy._getDescriptor().getProxy(); SOAPBinding binding = (SOAPBinding) bp.getBinding(); binding.setMTOMEnabled(true); // Send the image and process the response image response = proxy.sendImage(imageDepot); } if (null != result) { if (response != null) { result = result.concat("MTOM Proxy Response received - " + response.getImageData().getContentType()); } else { result = result.concat("ERROR: MTOM Proxy NULL Response received\n"); } } else { if (response != null) { System.out.println(">>MTOM Response received"); System.out .println(">>MTOM Writing returned image to proxy_response.gif"); processImageDepot(response, "proxy_response.gif"); } else { System.out .println(">> [ERROR] - Response from the server was NULL"); } } } catch (Exception e) { if (null != result) { result = result.concat(">> [ERROR] - Exception making connection."); } System.out.println(">> [ERROR] - Exception making connection."); e.printStackTrace(); } System.out.println(">>MTOM Proxy Done"); return (result); } /* * Get a DataHandler that contains the binary data we'd like to send. * @return DataHandler * @throws Exception */ private DataHandler getBinaryContent() throws Exception { DataHandler dh = null; if (imageFilename != null) { File file = new File(imageFilename); if (!file.exists()) { throw new FileNotFoundException(); } System.out.println(">>MTOM Loading data from: '" + file.toURI().toURL().toExternalForm() + "'"); FileDataSource fds = new FileDataSource(file); dh = new DataHandler(fds); } return dh; } /* * Takes the data from the ImageDepot and writes it out to a file named by * the provided String. * @param ImageDepot data * @param String fname * @throws Exception */ private void processImageDepot(ImageDepot data, String fname) throws Exception { DataHandler dh = data.getImageData(); if (dh != null) { File f = new File(fname); if (f.exists()) { f.delete(); } FileOutputStream fos = new FileOutputStream(f); dh.writeTo(fos); } else { System.out .println(">> [ERROR] - ImageDepot was not null, but did not contain binary data"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -