How to use addAttachmentPart method of org.cerberus.service.soap.impl.SoapService class

Best Cerberus-source code snippet using org.cerberus.service.soap.impl.SoapService.addAttachmentPart

Source:SoapService.java Github

copy

Full Screen

...110 MessageFactory messageFactory = MessageFactory.newInstance(is12SoapVersion ? SOAPConstants.SOAP_1_2_PROTOCOL : SOAPConstants.SOAP_1_1_PROTOCOL);111 return messageFactory.createMessage(headers, input);112 }113 @Override114 public void addAttachmentPart(SOAPMessage input, String path) throws CerberusException {115 URL url;116 try {117 LOG.debug("Adding Attachement to SOAP request : " + path);118 url = new URL(path);119 DataHandler handler = new DataHandler(url);120 //TODO: verify if this code is necessary121 /*String str = "";122 StringBuilder sb = new StringBuilder();123 BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));124 while (null != (str = br.readLine())) {125 sb.append(str);126 }*/127 AttachmentPart attachPart = input.createAttachmentPart(handler);128 input.addAttachmentPart(attachPart);129 } catch (MalformedURLException ex) {130 throw new CerberusException(new MessageGeneral(MessageGeneralEnum.SOAPLIB_MALFORMED_URL));131 } catch (Exception ex) {132 throw new CerberusException(new MessageGeneral(MessageGeneralEnum.SOAPLIB_MALFORMED_URL));133 }134 }135 private static SOAPMessage sendSOAPMessage(SOAPMessage message, String url, final Proxy p, final int timeoutms) throws SOAPException, MalformedURLException {136 SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();137 SOAPConnection connection = factory.createConnection();138 URL endpoint = new URL(null, url, new URLStreamHandler() {139 protected URLConnection openConnection(URL url) throws IOException {140 // The url is the parent of this stream handler, so must141 // create clone142 URL clone = new URL(url.toString());143 URLConnection connection = null;144 if (p == null) {145 connection = clone.openConnection();146 } else if (p.address().toString().equals("0.0.0.0/0.0.0.0:80")) {147 connection = clone.openConnection();148 } else {149 connection = clone.openConnection(p);150 }151 // Set Timeout152 connection.setConnectTimeout(timeoutms);153 connection.setReadTimeout(timeoutms);154 // Custom header155// connection.addRequestProperty("Developer-Mood", "Happy");156 return connection;157 }158 });159 try {160 SOAPMessage response = connection.call(message, endpoint);161 connection.close();162 return response;163 } catch (Exception e) {164 // Re-try if the connection failed165 SOAPMessage response = connection.call(message, endpoint);166 connection.close();167 return response;168 }169 }170 private void initializeProxyAuthenticator(final String proxyUser, final String proxyPassword) {171 if (proxyUser != null && proxyPassword != null) {172 Authenticator.setDefault(173 new Authenticator() {174 @Override175 public PasswordAuthentication getPasswordAuthentication() {176 return new PasswordAuthentication(177 proxyUser, proxyPassword.toCharArray()178 );179 }180 }181 );182 System.setProperty("http.proxyUser", proxyUser);183 System.setProperty("http.proxyPassword", proxyPassword);184 }185 }186 @Override187 public AnswerItem<AppService> callSOAP(String envelope, String servicePath, String soapOperation, String attachmentUrl, List<AppServiceHeader> header, String token, int timeOutMs, String system) {188 AnswerItem<AppService> result = new AnswerItem<>();189 String unescapedEnvelope = StringEscapeUtils.unescapeXml(envelope);190 boolean is12SoapVersion = SOAP_1_2_NAMESPACE_PATTERN.matcher(unescapedEnvelope).matches();191 AppService serviceSOAP = factoryAppService.create("", AppService.TYPE_SOAP, null, "", "", envelope, "", "", "", "", "", servicePath, "", soapOperation, "", null, "", null, null);192 serviceSOAP.setTimeoutms(timeOutMs);193 ByteArrayOutputStream out = null;194 MessageEvent message = null;195 if (StringUtil.isNullOrEmpty(servicePath)) {196 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_SERVICEPATHMISSING);197 result.setResultMessage(message);198 return result;199 }200 if (StringUtil.isNullOrEmpty(envelope)) {201 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_ENVELOPEMISSING);202 result.setResultMessage(message);203 return result;204 }205 // If header is null we create the list empty.206 if (header == null) {207 header = new ArrayList<>();208 }209 // We feed the header with token + Standard SOAP header.210 if (token != null) {211 header.add(factoryAppServiceHeader.create(null, "cerberus-token", token, "Y", 0, "", "", null, "", null));212 }213 if (StringUtil.isNullOrEmpty(soapOperation)) {214 header.add(factoryAppServiceHeader.create(null, "SOAPAction", "", "Y", 0, "", "", null, "", null));215 } else {216 header.add(factoryAppServiceHeader.create(null, "SOAPAction", "\"" + soapOperation + "\"", "Y", 0, "", "", null, "", null));217 }218 header.add(factoryAppServiceHeader.create(null, "Content-Type", is12SoapVersion ? SOAPConstants.SOAP_1_2_CONTENT_TYPE : SOAPConstants.SOAP_1_1_CONTENT_TYPE, "Y", 0, "", "", null, "", null));219 serviceSOAP.setHeaderList(header);220 SOAPConnectionFactory soapConnectionFactory;221 SOAPConnection soapConnection = null;222 try {223 //Initialize SOAP Connection224 soapConnectionFactory = SOAPConnectionFactory.newInstance();225 soapConnection = soapConnectionFactory.createConnection();226 LOG.debug("Connection opened");227 // Create SOAP Request228 LOG.debug("Create request");229 SOAPMessage input = createSoapRequest(envelope, soapOperation, header, token);230 //Add attachment File if specified231 if (!StringUtil.isNullOrEmpty(attachmentUrl)) {232 this.addAttachmentPart(input, attachmentUrl);233 // Store the SOAP Call234 out = new ByteArrayOutputStream();235 input.writeTo(out);236 LOG.debug("WS call with attachement : " + out.toString());237 serviceSOAP.setServiceRequest(out.toString());238 } else {239 // Store the SOAP Call240 out = new ByteArrayOutputStream();241 input.writeTo(out);242 LOG.debug("WS call : " + out.toString());243 }244 // We already set the item in order to keep the request message in case of failure of SOAP calls.245 serviceSOAP.setService(servicePath);246 result.setItem(serviceSOAP);...

Full Screen

Full Screen

addAttachmentPart

Using AI Code Generation

copy

Full Screen

1 public static void addAttachmentPart(SOAPMessage soapMessage, String attachmentPartName, String attachmentPartContent, String attachmentPartContentType) throws SOAPException, IOException {2 SOAPPart soapPart = soapMessage.getSOAPPart();3 SOAPEnvelope soapEnvelope = soapPart.getEnvelope();4 SOAPBody soapBody = soapEnvelope.getBody();5 SOAPElement soapBodyElement = soapBody.getFirstChild();6 MimeHeaders mimeHeaders = soapMessage.getMimeHeaders();7 mimeHeaders.addHeader("Content-Type", attachmentPartContentType);8 soapMessage.saveChanges();9 AttachmentPart attachmentPart = soapMessage.createAttachmentPart();10 attachmentPart.setContent(attachmentPartContent, attachmentPartContentType);11 attachmentPart.setContentId(attachmentPartName);12 soapMessage.addAttachmentPart(attachmentPart);13 }

Full Screen

Full Screen

addAttachmentPart

Using AI Code Generation

copy

Full Screen

1import org.cerberus.service.soap.impl.SoapService;2import org.cerberus.service.soap.impl.SoapServiceException;3import java.util.HashMap;4import java.util.Map;5public class SoapServiceTest {6 public static void main(String[] args) {7 try {8 SoapService soapService = new SoapService();9 Map<String, String> parameters = new HashMap<String, String>();10 parameters.put("operation", "sayHello");11 parameters.put("username", "admin");12 parameters.put("password", "admin");13 parameters.put("contentType", "text/xml");14 parameters.put("message", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"15 + "</soapenv:Envelope>");16 parameters.put("attachment", "C:\\Users\\user\\Desktop\\attachment.txt");17 parameters.put("attachmentName", "attachment.txt");18 parameters.put("attachmentContentType", "text/plain");19 String response = soapService.callSOAPWithAttachment(parameters);20 System.out.println(response);21 } catch (SoapServiceException e) {22 e.printStackTrace();23 }24 }25}

Full Screen

Full Screen

addAttachmentPart

Using AI Code Generation

copy

Full Screen

1import org.cerberus.service.soap.impl.SoapService;2SoapService soapService = new SoapService();3String soapRequest = "soap request";4String soapResponse = "soap response";5String attachmentPartName = "attachment part name";6byte[] attachmentPartContent = soapService.getAttachmentPartContent(soapRequest, soapResponse, attachmentPartName);7soapService.addAttachmentPart(soapResponse, attachmentPartName, attachmentPartContent);

Full Screen

Full Screen

addAttachmentPart

Using AI Code Generation

copy

Full Screen

1package com.mycompany.myproject;2import org.cerberus.service.soap.impl.SoapService;3public class AddFileToAttachment {4 public static void main(String[] args) {5 SoapService soapService = new SoapService();6 soapService.addAttachmentPart("attachment", "file.txt", "C:\\", "text/plain");7 }8}9package com.mycompany.myproject;10import org.cerberus.service.soap.impl.SoapService;11public class AddFileToAttachment {12 public static void main(String[] args) {13 SoapService soapService = new SoapService();14 soapService.addAttachmentPart("attachment", "file.txt", "C:\\", "text/plain");15 }16}17package com.mycompany.myproject;18import org.cerberus.service.soap.impl.SoapService;19public class AddFileToAttachment {20 public static void main(String[] args) {21 SoapService soapService = new SoapService();22 soapService.addAttachmentPart("attachment", "file.txt", "C:\\", "text/plain");23 }24}

Full Screen

Full Screen

addAttachmentPart

Using AI Code Generation

copy

Full Screen

1public void addAttachmentPart(String attachmentPartName, String attachmentPartValue, 2SOAPMessage soapMessage) {3 try {4 SOAPPart soapPart = soapMessage.getSOAPPart();5 SOAPEnvelope soapEnvelope = soapPart.getEnvelope();6 SOAPBody soapBody = soapEnvelope.getBody();7 AttachmentPart attachmentPart = soapMessage.createAttachmentPart();8 attachmentPart.setContent(attachmentPartValue, attachmentPartContentType);9 attachmentPart.setContentId(attachmentPartContentId);10 attachmentPart.setContentTransferEncoding(attachmentPartContentTransferEncoding);11 attachmentPart.setContentDisposition(attachmentPartContentDisposition);12 attachmentPart.setContentDescription(attachmentPartContentDescription);13 attachmentPart.setContentLocation(attachmentPartContentLocation);14 soapMessage.addAttachmentPart(attachmentPart);15 soapBody.addAttachmentPart(attachmentPart);16 } catch (SOAPException ex) {17 Logger.getLogger(SoapService.class.getName()).log(Level.SEVERE, null, ex);18 }19}20public void addAttachmentPart(String attachmentPartName, String attachmentPartValue,

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cerberus-source automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful