<n0:BlogAwsS3HTTPSAdapter xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style">
<Assets>
<Documents>
<Type>
<Id>REF</Id>
<Name>Cad 3D</Name>
</Type>
<Id>794290</Id>
<Name>Cad 3D_1189.102</Name>
<Version>AA</Version>
<Part>000"</Part>
<Path>/Bola/Produtos/1189.BL102.MT.T06</Path>
<Atributes>
<item>
<Id>asset.filename</Id>
<Name>asset.filename</Name>
<Values>
<Valueid/>
<Value>Cad 3D_1189.102.dwg</Value>
<Uom/>
</Values>
</item>
<item>
<Id>asset.mime-type</Id>
<Name>name": "asset.mime-type</Name>
<Values>
<Valueid/>
<Value>application/octet-stream</Value>
<Uom/>
</Values>
</item>
</Atributes>
</Documents>
</Assets>
<Attributes>
<item>
<Id>TP_MEC</Id>
<Name>Tipo de Mecanismo utilizado</Name>
<Group/>
<Values>
<item>
<Valueid>LOV_TP_MEC_SOLEN</Valueid>
<Value>Solenóide</Value>
<Uom/>
</item>
</Values>
</item>
<item>
<Id>ESTILO_DESIGN_EVIDENCIA</Id>
<Name>Estilo e design em evidência</Name>
<Group/>
<Values>
<item>
<Valueid>LOV_ESTILO_DESIGN_EVIDENCIA_OUSADO</Valueid>
<Value>Formas e acabamentos ousados</Value>
<Uom/>
</item>
<item>
<Valueid>LOV_ESTILO_DESIGN_EVIDENCIA_NOBRE</Valueid>
<Value>Cores nobres e sofisticadas</Value>
<Uom/>
</item>
</Values>
</item>
</Attributes>
<EvBrand>Ceusa</EvBrand>
<EvName>1985.C.CT-CHUV TUBO PAR CUBO - CR</EvName>
<EvType>children</EvType>
<Keys>
<item>
<Id>COD_BARRAS</Id>
<Name>Código de barras do produto</Name>
<Value>7894203020995</Value>
<Group/>
</item>
<item>
<Id>COD_MATERIAL</Id>
<Name>Código do material</Name>
<Value>1189.BL102.MT.T06</Value>
<Group/>
</item>
</Keys>
</n0:BlogAwsS3HTTPSAdapter>
/**
* Creator: Ricardo Viana
* Date: 20/02/2020
**/
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.*
import groovy.xml.*
import java.nio.charset.StandardCharsets;
def Message processData(Message message) {
java.io.InputStream reader = message.getBody(java.io.InputStream)
def JsonFile = new XmlParser().parse(reader)
def builder = new JsonBuilder()
builder {
'id'
'name' JsonFile.EvName.text()
'type' JsonFile.EvType.text()
'brand' JsonFile.EvBrand.text()
def arrayKeys = JsonFile.Keys.item
'Keys' arrayKeys.collect { item->
[
'id': item.Id.text(),
'name': item.Name.text(),
'value': item.Value.text(),
]
}
assets{
documents{
type{
'id' JsonFile.Assets.Documents.Type.Id.text()
'name' JsonFile.Assets.Documents.Type.Name.text()
}
'id' JsonFile.Assets.Documents.Id.text()
'name' JsonFile.Assets.Documents.Name.text()
'version' JsonFile.Assets.Documents.Version.text()
'part' JsonFile.Assets.Documents.Part.text()
'path' JsonFile.Assets.Documents.Path.text()
}
attributes{
def arrayItemAssets = JsonFile.Assets.Documents.Atributes.item
'item' arrayItemAssets.collect { item ->
[
'id': item.Id.text(),
'name': item.Name.text(),
'values': item.Values.collect { item2 ->
[
'valueId': item2.Valueid.text(),
'value': item2.Value.text(),
'uom': item2.Uom.text(),
]
}
]
}
}
}
def arrayAttributes = JsonFile.Attributes.item
'attributes' arrayAttributes.collect { item3 ->
[
'id': item3.Id.text(),
'name': item3.Name.text(),
'group': item3.Group.text(),
'values': item3.Values.item.collect { item4 ->
[
'valueId': item4.Valueid.text(),
'value': item4.Value.text(),
'uom': item4.Uom.text(),
]
}
]
}
}
def jsonString = JsonOutput.prettyPrint(builder.toString())
jsonString = unescapeUnicode(jsonString)
message.setProperty("NomeArquivo", "JsonFileBlog_SAPCPI_HttpsAdapter.json")
message.setBody(jsonString)
return message
}
def unescapeUnicode(def inp){
(inp =~ /\\u([0-9a-f]{2})([0-9a-f]{2})/).each { m ->
def uniAsString = new String([
Integer.parseInt(m[1], 16),
Integer.parseInt(m[2], 16)
] as byte[], StandardCharsets.UTF_16)
inp = inp.replace(m[0], uniAsString)
}
return inp
}
byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
byte[] kDate = HmacSHA256(dateStamp, kSecret);
byte[] kRegion = HmacSHA256(regionName, kDate);
byte[] kService = HmacSHA256(serviceName, kRegion);
byte[] kSigning = HmacSHA256("aws4_request", kService);
return kSigning;
}
import com.sap.gateway.ip.core.customdev.util.Message;
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat
import java.lang.Object
import java.util.List;
import java.util.TimeZone;
import org.apache.commons.codec.digest.DigestUtils;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String
//************* Hash do Body using apache commons DigestUtils sha256Hex *************
def hashBody = DigestUtils.sha256Hex(body)
//************* Mapping the properties - The filename was set in previous groovyScript *************
def map = message.getProperties()
//************* Iniciating variables *************
String method = "PUT";
String host = "<yourbuket>.s3.us-east-2.amazonaws.com";
String region = "us-east-2";
String service = "s3";
String endpoint = "s3.us-east-2.amazonaws.com";
// Read AWS access key from security artifacts. Best practice is NOT to embed credentials in code.
def access_key = "ID_GeneratedInAWS"
def secret_key = "Key_GeneratedInAWS"
// Create a date for headers and the credential string
def now = new Date()
def amzFormat = new SimpleDateFormat( "yyyyMMdd'T'HHmmss'Z'" )
def formattedDate = new SimpleDateFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'")
def stampFormat = new SimpleDateFormat( "yyyyMMdd" )
def amzDate = amzFormat.format(now)
def date_stamp = stampFormat.format(now)
//************* Canonical Request variables *************
String canonical_uri = "/<yourFolder>/"+map.get('NomeArquivo');
String canonical_querystring = "";
String canonical_headers = "host:" + host + "\n"+ "x-amz-content-sha256:" + hashBody + "\n" + "x-amz-date:" + amzDate + "\n";
String signed_headers = "host;x-amz-content-sha256;x-amz-date";
String canonical_request = method + "\n" + canonical_uri + "\n" + canonical_querystring + "\n" + canonical_headers + "\n" + signed_headers + "\n" + hashBody;
//************* Sing to Sing variables *************
String algorithm = "AWS4-HMAC-SHA256";
String credential_scope = date_stamp + "/" + region + "/" + service + "/" + "aws4_request";
String string_to_sign = algorithm + "\n" + amzDate + "\n" + credential_scope + "\n" + DigestUtils.sha256Hex(canonical_request);
//************* Generating the Singning Key *************
byte[] signing_key = getSignatureKey(secret_key, date_stamp, region, service);
//************* Generating the HmacSHA256 - Amazon *************
byte[] signature = HmacSHA256(string_to_sign,signing_key);
//************* Generating the Hex of the Signature *************
String strHexSignature = bytesToHex(signature);
//************* Generating the authorization header signed - Amazon V4 S3 Bucket *************
String authorization_header = algorithm + " " + "Credential=" + access_key + "/" + credential_scope + ", " + "SignedHeaders=" + signed_headers + ", " + "Signature=" + strHexSignature;
//************* Seting the headers of HTTP call *************
message.setHeader("x-amz-date",amzDate);
message.setHeader("x-amz-content-sha256", hashBody)
message.setHeader("Authorization", authorization_header);
message.setHeader("Host", "<yourBucket>.s3.us-east-2.amazonaws.com");
message.setHeader("content-type", "application/json");
//************* Setting the body to be store in Amazon *************
message.setBody(body)
return message
}
//************* Function bytes to Hex *************
String bytesToHex(byte[] bytes) {
char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars).toLowerCase();
}
//************* Function HmacSHA256 *************
byte[] HmacSHA256(String data, byte[] key) throws Exception {
String algorithm="HmacSHA256";
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes("UTF8"));
}
//************* Function getSignature *************
byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
byte[] kDate = HmacSHA256(dateStamp, kSecret);
byte[] kRegion = HmacSHA256(regionName, kDate);
byte[] kService = HmacSHA256(serviceName, kRegion);
byte[] kSigning = HmacSHA256("aws4_request", kService);
return kSigning;
}
def generateHex(String data) {
MessageDigest mac = MessageDigest.getInstance("SHA-256");
byte[] signatureBytes = mac.digest(data.getBytes(StandardCharsets.UTF_8));
StringBuffer hexString = new StringBuffer();
for (int j=0; j<signatureBytes.length; j++) {
String hex=Integer.toHexString(0xff & signatureBytes[j]);
if(hex.length()==1) hexString.append('0');
hexString.append(hex);
}
String encryptedSignature = hexString.toString();
String encryptHash = encryptedSignature.replace("-","");
encryptHash = encryptHash.toLowerCase();
return encryptHash;
}
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap
import org.w3c.dom.Node
import groovy.xml.*
def Message processData(Message message) {
java.io.InputStream reader = message.getBody(java.io.InputStream)
def messageLog = messageLogFactory.getMessageLog(message)
def xHeaders = message.getHeaders()
def map = message.getProperties()
String logVar = map.get("enableLog")
String sResponseCode = xHeaders.get("CamelHttpResponseCode")
String sHtttpError = xHeaders.get ("CamelHttpResponseText")
def ex = map.get("CamelExceptionCaught");
if (ex!=null) {
if (ex.getClass().getCanonicalName().equals("org.apache.camel.component.ahc.AhcOperationFailedException")) {
messageLog.addAttachmentAsString("HTTP Response body: ", ex.getResponseBody(), "text/plain");
String exceptionBody = "Error in the routing details in the Iflow "
throw new Exception(exceptionBody);
return message;
}
}
else{
String exceptionBody = "Error in the routing details in the Iflow "
throw new Exception(exceptionBody);
return message;
}
return message;
}
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>WX63PW5GYS61NRYV</RequestId>
<HostId>+pyQoa9brW6NGw0j9rMDJiz84Nhucp58we4WPbtEVRI/yzSgXhMQE9TUro97Fr5wPYDDw2QP5RQ=</HostId>
</Error>
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<AWSAccessKeyId>AKIAWXC7MCXWRGBV5QVD</AWSAccessKeyId>
</Error>
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>MethodNotAllowed</Code>
<Message>The specified method is not allowed against this resource.</Message>
<Method>POST</Method>
<ResourceType>OBJECT</ResourceType>
<RequestId>NEAJEF9PATJ8ADS4</RequestId>
<HostId>Zb9ljNLrZtApEWu+Sr6fkKxFP7zy3jU3ILpr+EcQHKZfzP9nxyOdAeVr6mywTRVCls/zjMm62CA=</HostId>
</Error>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |