PGP general flow (X send to Y): At X side sending, sign with X private key, Encrypt with Y public key. At Y side receiving, verify with X public key, Decrypt with Y private key. Below show an example usage
Seeburger SFTP Receiver Adapter with Dynamic Filename
Below show example usage of SFTP receiver adapter: Host name is target SFTP host want to connect. Port is default 22. Known hosts store is the key store view that contain SSH cert of the host. For the first time
SAP PI Custom Module PayloadAddBOMBean Development
Custom module to add byte order mark (BOM) characters to output file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.test; import java.io.ByteArrayOutputStream; import java.io.InputStream; import javax.ejb.Stateless; import com.sap.aii.af.lib.mp.module.ModuleContext; import com.sap.aii.af.lib.mp.module.ModuleData; import com.sap.aii.af.lib.mp.module.ModuleException; import com.sap.engine.interfaces.messaging.api.Message; import com.sap.engine.interfaces.messaging.api.Payload; @Stateless public class PayloadAddBOMBean implements PayloadAddBOMBeanRemote, PayloadAddBOMBeanLocal { public PayloadAddBOMBean() { } public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)throws ModuleException{ try { Object obj = inputModuleData.getPrincipalData(); Message msg = (Message)obj; Payload payload = msg.getDocument(); InputStream inputStream = (InputStream)payload.getInputStream(); byte[] inputBytes = new byte[inputStream.available()]; inputStream.read(inputBytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(0xEF); outputStream.write(0xBB); outputStream.write(0xBF); outputStream.write(inputBytes); byte[] outputBytes = outputStream.toByteArray(); payload.setContent(outputBytes); inputModuleData.setPrincipalData(msg); return inputModuleData; } catch (Exception excep) { ModuleException modulExcep = new ModuleException( excep.getMessage()); throw modulExcep; } } } |
SAP PI Custom Module DynamicFileNameBean Development
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
package com.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.io.File; import java.text.SimpleDateFormat; import javax.ejb.Stateless; import javax.xml.namespace.NamespaceContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import com.sap.aii.af.lib.mp.module.ModuleContext; import com.sap.aii.af.lib.mp.module.ModuleData; import com.sap.aii.af.lib.mp.module.ModuleException; import com.sap.engine.interfaces.messaging.api.Message; import com.sap.engine.interfaces.messaging.api.MessageKey; import com.sap.engine.interfaces.messaging.api.MessagePropertyKey; import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory; import com.sap.engine.interfaces.messaging.api.XMLPayload; import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess; import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus; import com.sap.engine.interfaces.messaging.api.exception.MessagingException; import org.apache.commons.lang3.StringUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @Stateless public class DynamicFileNameBean implements DynamicFileNameBeanRemote, DynamicFileNameBeanLocal { private AuditAccess audit; private MessageKey msgkey; private Map<String, String> prefixNSmap; private Document document; public DynamicFileNameBean() { try { this.audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess(); } catch (MessagingException e) { this.audit = null; } } @SuppressWarnings("unchecked") public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)throws ModuleException{ try { Message msg = (Message)inputModuleData.getPrincipalData(); this.prefixNSmap = new HashMap<String, String>(); this.msgkey = new MessageKey(msg.getMessageId(), msg.getMessageDirection()); addLog(AuditLogStatus.SUCCESS, "Begin DynamicFileNameBean processing..."); Enumeration oContextDataKeys; //Process setASMA & setVariable oContextDataKeys = moduleContext.getContextDataKeys(); while(oContextDataKeys.hasMoreElements()){ String cdk = (String)oContextDataKeys.nextElement(); if(StringUtils.startsWithIgnoreCase(cdk, "setASMA")){ addLog(AuditLogStatus.SUCCESS, "Process " + cdk); String sAttribute = StringUtils.split(cdk, "@")[1]; String sNamespace = StringUtils.split(cdk, "@")[2]; String sPattern = moduleContext.getContextData(cdk); addLog(AuditLogStatus.SUCCESS, "INPUT: Namespace=" + sNamespace + "; " + "Attribute=" + sAttribute + "; Pattern=" + sPattern); String sPatternOutput = processPattern(sPattern, msg, msgkey, inputModuleData); MessagePropertyKey mpk = new MessagePropertyKey(sAttribute, sNamespace); msg.removeMessageProperty(mpk); msg.setMessageProperty(mpk, sPatternOutput); addLog(AuditLogStatus.SUCCESS, "OUTPUT: Namespace=" + sNamespace + "; " + "Attribute=" + sAttribute + "; Value=" + sPatternOutput); } } inputModuleData.setPrincipalData(msg); addLog(AuditLogStatus.SUCCESS, "End DynamicFileNameBean processing..."); return inputModuleData; } catch (Exception e){ ModuleException me = new ModuleException( e.toString()); throw me; } } public String processPattern(String sPattern, Message msg, MessageKey msgkey, ModuleData inputModuleData) { String sOutput = ""; try{ ArrayList<String> sTokenList = new ArrayList<String>(Arrays.asList(sPattern.split(" *& *"))); for(int i=0; i<sTokenList.size(); i++){ String sToken = sTokenList.get(i); String sExpResult = ""; ArrayList<String> sExpression = new ArrayList<String>(Arrays.asList(sToken.split(" *\\Q|\\E *"))); for(int j=0; j<sExpression.size(); j++){ String sExp = sExpression.get(j); String sExpResultBefore = sExpResult; if(StringUtils.startsWithAny(sExp, new String[]{"\"", "\'"}) && StringUtils.endsWithAny(sExp, new String[]{"\"", "\'"})){ sExpResult = StringUtils.strip(sExp, "\"\'"); }else if(!sExp.contains("(")){ sExpResult = sExp; }else{ String sFunction = StringUtils.substringBefore(sExp, "("); String sArgument = StringUtils.chop(StringUtils.substringAfter(sExp, "(")); String sArg[] = sArgument.split("(?x) " + ", " + // Split on comma "(?= " + // Followed by " (?: " + // Start a non-capture group " [^\"]* " + // 0 or more non-quote characters " \" " + // 1 quote " [^\"]* " + // 0 or more non-quote characters " \" " + // 1 quote " )* " + // 0 or more repetition of non-capture group (multiple of 2 quotes will be even) " [^\"]* " + // Finally 0 or more non-quotes " $ " + // Till the end (This is necessary, else every comma will satisfy the condition) ") " // End look-ahead ); for(int k=0; k<sArg.length; k++){ sArg[k] = StringUtils.strip(sArg[k], "\"\'"); } sExpResult = executeFunction(sExpResult, inputModuleData, msg, sFunction, sArg); } if(!sExpResultBefore.isEmpty()){ sExpResultBefore = sExpResultBefore + "|"; } addLog(AuditLogStatus.SUCCESS, "Input=" + sExpResultBefore + sExp + "; Output=" + sExpResult); } sOutput = sOutput + sExpResult; } }catch(Exception ex) { addLog(AuditLogStatus.ERROR, "Failed at processPattern=" + sPattern); } return sOutput; } public void addLog(AuditLogStatus status, String message) { if (this.audit != null) { this.audit.addAuditLogEntry(this.msgkey, status, message); }else { System.out.println( "Audit Log: " + message); } } public Document getDocument(Message msg){ if(this.document == null){ XMLPayload xmlpayload = msg.getDocument(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); this.document = builder.parse(xmlpayload.getInputStream()); } catch (Exception ex) { addLog(AuditLogStatus.SUCCESS, "Failed at getDocument parse xmlpayload."); } return this.document; }else{ return this.document; } } public void addPrefixNamespaceMap(Node root){ NamedNodeMap attributes = root.getAttributes(); if (attributes != null){ for (int i = 0; i < attributes.getLength(); i++){ Node node = attributes.item(i); if (node.getNodeType() == Node.ATTRIBUTE_NODE){ String name = node.getNodeName(); if(StringUtils.startsWithIgnoreCase(name, "xmlns")){ String sPrefix = StringUtils.substringAfter(name, ":"); String sNamespace = node.getNodeValue(); this.prefixNSmap.put(sPrefix, sNamespace); addLog(AuditLogStatus.SUCCESS, "Added prefix namespace map; Prefix=" + sPrefix + "; " + "Namespace=" + sNamespace); } } } } } public void addPrefixNamespaceMapRecursive(Node root) { addPrefixNamespaceMap(root); NodeList childNodes = root.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++){ addPrefixNamespaceMapRecursive(childNodes.item(i)); } } public String executeFunction(String sIn, ModuleData inputModuleData, Message msg, String sFunction, String[] sArg){ String sOut = sIn; if(sFunction.equalsIgnoreCase("getASMA")){ MessagePropertyKey mpk = new MessagePropertyKey(sArg[0],sArg[1]); sOut = msg.getMessageProperty(mpk); } else if(sFunction.equalsIgnoreCase("XPath")){ Element root = getDocument(msg).getDocumentElement(); if(prefixNSmap.size() == 0){ addPrefixNamespaceMapRecursive((Node) root); } try { XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(new NamespaceContext() { public String getNamespaceURI(String prefix) { return prefixNSmap.get(prefix); } public Iterator<String> getPrefixes(String val) { return null; } public String getPrefix(String uri) { return null; } }); XPathExpression expr; expr = xpath.compile(sArg[0]); Object result = expr.evaluate(root, XPathConstants.NODESET); NodeList nodes = (NodeList)result; if ((nodes != null) && (nodes.getLength() > 0)){ sOut = nodes.item(0).getChildNodes().item(0).getNodeValue(); } } catch (XPathExpressionException e) { addLog(AuditLogStatus.SUCCESS, "Failed at XPath function."); } } else if(sFunction.equalsIgnoreCase("getSAPSYSTEMNAME")){ sOut = System.getProperty("SAPSYSTEMNAME", ""); } else if(sFunction.equalsIgnoreCase("getSAPSYSTEM")){ sOut = System.getProperty("SAPSYSTEM", ""); } else if(sFunction.equalsIgnoreCase("getCorrelationId")){ sOut = msg.getCorrelationId(); } else if(sFunction.equalsIgnoreCase("getMessageId")){ sOut = msg.getMessageId(); } else if(sFunction.equalsIgnoreCase("getProtocol")){ sOut = msg.getProtocol(); } else if(sFunction.equalsIgnoreCase("getRefToMessageId")){ sOut = msg.getRefToMessageId(); } else if(sFunction.equalsIgnoreCase("getDeliverySemantics")){ sOut = msg.getDeliverySemantics().toString(); } else if(sFunction.equalsIgnoreCase("getFromParty")){ sOut = msg.getFromParty().getName(); } else if(sFunction.equalsIgnoreCase("getSequenceId")){ sOut = msg.getSequenceId(); } else if(sFunction.equalsIgnoreCase("getFromService")){ sOut = msg.getFromService().getName(); } else if(sFunction.equalsIgnoreCase("getMessageClass")){ sOut = msg.getMessageClass().toString(); } else if(sFunction.equalsIgnoreCase("getMessageDirection")){ sOut = msg.getMessageDirection().toString(); } else if(sFunction.equalsIgnoreCase("getToParty")){ sOut = msg.getToParty().getName(); } else if(sFunction.equalsIgnoreCase("getToService")){ sOut = msg.getToService().getName(); } else if(sFunction.equalsIgnoreCase("appendIfMissing")){ sOut = StringUtils.appendIfMissing(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("appendIfMissingIgnoreCase")){ sOut = StringUtils.appendIfMissingIgnoreCase(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("capitalize")){ sOut = StringUtils.capitalize(sIn); } else if(sFunction.equalsIgnoreCase("center")){ if(sArg.length == 1){ sOut = StringUtils.center(sIn, Integer.parseInt(sArg[0])); } else if(sArg.length == 2){ sOut = StringUtils.center(sIn, Integer.parseInt(sArg[0]), sArg[1]); } } else if(sFunction.equalsIgnoreCase("chop")){ sOut = StringUtils.chop(sIn); } else if(sFunction.equalsIgnoreCase("concat")){ sOut = sIn.concat(sArg[0]); } else if(sFunction.equalsIgnoreCase("left")){ sOut = StringUtils.left(sIn, Integer.parseInt(sArg[0])); } else if(sFunction.equalsIgnoreCase("leftPad")){ sOut = StringUtils.leftPad(sIn, Integer.parseInt(sArg[0]), sArg[1]); } else if(sFunction.equalsIgnoreCase("normalizeSpace")){ sOut = StringUtils.normalizeSpace(sIn); } else if(sFunction.equalsIgnoreCase("prependIfMissing")){ sOut = StringUtils.prependIfMissing(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("prependIfMissingIgnoreCase")){ sOut = StringUtils.prependIfMissingIgnoreCase(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("remove")){ sOut = StringUtils.remove(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("removeEnd")){ sOut = StringUtils.removeEnd(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("removeEndIgnoreCase")){ sOut = StringUtils.removeEndIgnoreCase(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("removePattern")){ sOut = StringUtils.removePattern(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("removeStart")){ sOut = StringUtils.removeStart(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("removeStartIgnoreCase")){ sOut = StringUtils.removeStartIgnoreCase(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("repeat")){ if(sArg.length == 1){ sOut = sOut + StringUtils.repeat(sIn, Integer.parseInt(sArg[0])); }if(sArg.length == 2){ sOut = sOut + StringUtils.repeat(sIn, sArg[0], Integer.parseInt(sArg[1])); } } else if(sFunction.equalsIgnoreCase("replace")){ if(sArg.length == 2){ sOut = StringUtils.replace(sIn, sArg[0], sArg[1]); }else if(sArg.length == 3){ sOut = StringUtils.replace(sIn, sArg[0], sArg[1], Integer.parseInt(sArg[2])); } } else if(sFunction.equalsIgnoreCase("replaceOnce")){ sOut = StringUtils.replace(sIn, sArg[0], sArg[1]); } else if(sFunction.equalsIgnoreCase("replacePattern")){ sOut = StringUtils.replacePattern(sIn, sArg[0], sArg[1]); } else if(sFunction.equalsIgnoreCase("reverse")){ sOut = StringUtils.reverse(sIn); } else if(sFunction.equalsIgnoreCase("reverseDelimited")){ sOut = StringUtils.reverseDelimited(sIn, sArg[0].toCharArray()[0]); } else if(sFunction.equalsIgnoreCase("right")){ sOut = StringUtils.right(sIn, Integer.parseInt(sArg[0])); } else if(sFunction.equalsIgnoreCase("rightPad")){ if(sArg.length == 2){ sOut = StringUtils.rightPad(sIn, Integer.parseInt(sArg[0])); }else if(sArg.length == 3){ sOut = StringUtils.rightPad(sIn, Integer.parseInt(sArg[0]), sArg[1]); } } else if(sFunction.equalsIgnoreCase("splitRegEx")){ try{ ArrayList<String> foundList = new ArrayList<String>(Arrays.asList(sIn.split(sArg[0]))); sOut = foundList.get(Integer.parseInt(sArg[1])); }catch(Exception ex){ sOut = ""; } } else if(sFunction.equalsIgnoreCase("substring")){ if(sArg.length == 1){ sOut = sOut + StringUtils.substring(sIn, Integer.parseInt(sArg[0])); }else if(sArg.length == 2){ sOut = sOut + StringUtils.substring(sIn, Integer.parseInt(sArg[0]), Integer.parseInt(sArg[1])); } } else if(sFunction.equalsIgnoreCase("substringAfter")){ sOut = sOut + StringUtils.substringAfter(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("substringAfterLast")){ sOut = sOut + StringUtils.substringAfterLast(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("substringBefore")){ sOut = sOut + StringUtils.substringBefore(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("substringBeforeLast")){ sOut = sOut + StringUtils.substringBeforeLast(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("substringBetween")){ if(sArg.length == 1){ sOut = sOut + StringUtils.substringBetween(sIn, sArg[0]); }else if(sArg.length == 2){ sOut = sOut + StringUtils.substringBetween(sIn, sArg[0], sArg[1]); } } else if(sFunction.equalsIgnoreCase("substringRegEx")){ try{ Pattern p = Pattern.compile(sArg[0]); Matcher m = p.matcher(sIn); ArrayList<String> foundList = new ArrayList<String>(); while(m.find()){ foundList.add(m.group(1)); } if(sArg.length == 1){ sOut = foundList.get(0); }else if(sArg.length == 2){ sOut = foundList.get(Integer.parseInt(sArg[1])-1); } }catch(Exception ex){ sOut = ""; } } else if(sFunction.equalsIgnoreCase("toLowerCase")){ sOut = sIn.toLowerCase(); } else if(sFunction.equalsIgnoreCase("toUpperCase")){ sOut = sIn.toUpperCase(); } else if(sFunction.equalsIgnoreCase("trim")){ sOut = sIn.trim(); } else if(sFunction.equalsIgnoreCase("uncapitalize")){ sOut = StringUtils.uncapitalize(sIn); } else if(sFunction.equalsIgnoreCase("wrap")){ sOut = StringUtils.wrap(sIn, sArg[0]); } else if(sFunction.equalsIgnoreCase("getTimestamp")){ if(sArg[0].isEmpty()){ sArg[0] = "yyyyMMDD_HHmmss_SSS"; } Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(sArg[0]); sOut = sdf.format(date); } return sOut; } } |
File Adapter – Sender – Convert Multiple Fixed Length Structure to XML Using Key Field Name
This example convert 2 different recordsets inside single file to xml. First recordset is “SINGER”, second recordset is “SONG”. First column is 1 character length. It is a key column to identify different recordset. “A” from first half mean SINGER,
File Adapter – Sender – Convert Single Fixed Length Structure to XML
Previous example tested the CSV file with delimiter, now look at fixed-length file. The objective is to convert fixed-length file to xml representation. ABB, FULLNAME, GENDER and COUNTRY, each is with length 5,20,7,10 respectively. Example: The target message type
File Adapter – Sender – Content Conversion to Convert CSV file to XML
The objective is to convert csv file to xml representation. Example: The target message type is 2 columns that repeatable at node row. Document Name is the target message type. Document Namespace should be the namespace of the message type.