reformat component issue in java

Support/help with CloverETL (4.9) and CloverDX (5.0 or newer) implementation problems

pOctavian
Posts: 19
Joined: Thu Mar 22, 2012 5:48 am

reformat component issue in java

Postby pOctavian » Sat Apr 28, 2012 7:54 am

Hi,

I converted my reformat component to java since my task involves encrypting a string to HmacSHA256 and I'm having difficulties with it. What I did was I first created a java file in JCreator then tested it and it succesfully encrypts my string then encode it to base64. What I did then was I copy pasted the code from JCreator to clover in the reformat component. However, when I run the graph I get an error.

unreported exception java.security.SignatureException; must be caught or declared to be thrown
hashed = signedReq.sign(stringToSign,key);

Here's what I have in my reformat component:

public String sign(String stringToSign, String key) throws SignatureException
{
byte[] signature;
try
{
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key.getBytes(), "HmacSHA256"));
signature = Base64.encodeBase64(mac.doFinal(stringToSign.getBytes("UTF-8")));
}
catch (Exception e)
{
throw new SignatureException("Failed to generate signature: " + e.getMessage(), e);
}
}

protected final int transformDelegate() throws org.jetel.exception.ComponentNotReadyException, org.jetel.exception.TransformException
{
String stringToSign;
String key = "Key";
String hashed;
String ts;
String urlEncoded;

CTL_REFORMAT0_26042012_14_36_40_0 signedReq = new CTL_REFORMAT0_26042012_14_36_40_0();
ts = signedReq.getFormattedTimestamp();
stringToSign = signedReq.calculatedString() + ts.replace(":","%3A").replace(".","%2E");
hashed = signedReq.sign(stringToSign,key);
urlEncoded = hashed.replace("+","%2B").replace("*", "%2A").replace("~", "%7E").replace("/","%2F").replace("=","%3D");
getOutputRecord(0).getField(0).setValue(org.jetel.ctl.extensions.StringLib.concat(this.__ctl_function_call_context[0],"Signature:",urlEncoded));

return 0;
}

Is protected final int transformDelegate() in the reformat component equivalent to my main() method? Thanks.

jurban
Posts: 163
Joined: Fri Jul 20, 2007 9:25 am

Re: reformat component issue in java

Postby jurban » Mon Apr 30, 2012 9:45 am

Hi,

your issue can be broken down into 2 parts:

1) I believe you're extending the CTLRecordTransform class as the base for your transformation. That class is internally used by our CTL to Java compiler, it's not intended to be used elsewhere. You should extend the DataRecordTransform class and implement the transform() method - this will be your "main".

2) The specific cause of the error you're seeing is that the sign() method throws SignatureException, and you don't catch the exception at the location where you're calling the method (I'm guessing that your original main had a "throws Exception" clause). You should catch the SignatureException and either re-throw it by wrapping it in a TransformException, or handle the error in some other way (e.g. send some record to a different output port for error logging).

Combining the 2 hints above should help - extend DataRecordTransform and catch the SignatureException.

Best regards,
Jaro
Jaroslav Urban
CloverCARE Support
CloverETL | Rapid Data Integration

Visit us online at http://www.cloveretl.com

pOctavian
Posts: 19
Joined: Thu Mar 22, 2012 5:48 am

Re: reformat component issue in java

Postby pOctavian » Tue May 01, 2012 2:46 am

Hi Jaro,

Thanks for the response. I'm new to all this and I really appreciate your help.

I did the changes you asked me to do and I still have a few more questions about how to make my code work in the reformat component since I'm still getting some errors that I can't figure out.

First, I used to have public final class CTL_REFORMAT0_01052012_09_15_57_0 extends org.jetel.component.CTLRecordTransform {} then I changed it to public final class CTL_REFORMAT0_01052012_09_15_57_0 extends org.jetel.component.DataRecordTransform {}
but I'm getting an error that says "CTL_REFORMAT0_01052012_09_15_57_0 is not abstract and does not override abstract method transform(org.jetel.data.DataRecord[],org.jetel.data.DataRecord[]) in org.jetel.component.DataRecordTransform". So I changed it to public abstract class CTL_REFORMAT0_01052012_09_15_57_0 extends org.jetel.component.DataRecordTransform {}, I'm not even sure if I should be doing that. Also, you mentioned that I should implement the transform() method and that would act as my main method. Does that mean that I should change protected final int transformDelegate() throws org.jetel.exception.ComponentNotReadyException, org.jetel.exception.TransformException to public void transform() throws org.jetel.exception.ComponentNotReadyException, org.jetel.exception.TransformException? One more thing, should my class be something like "public final class CTL_REFORMAT0_01052012_09_15_57_0 extends org.jetel.component.DataRecordTransform implements transform{}"?

I don't know if it'll help but I can send you a copy of the java file that I'm trying to implement in the reformat component.

Thanks.

pOctavian
Posts: 19
Joined: Thu Mar 22, 2012 5:48 am

Re: reformat component issue in java

Postby pOctavian » Tue May 01, 2012 3:11 am

Hi,

Attached is the copy of the java codes that I'm trying to implement in the reformat component.

Thanks.
Attachments
CalculateSignature.java
(2.78 KiB) Downloaded 275 times

pOctavian
Posts: 19
Joined: Thu Mar 22, 2012 5:48 am

Re: reformat component issue in java

Postby pOctavian » Wed May 02, 2012 6:03 am

Hi Jaro,

I went through your instructions again and I got it working. Thanks.

rafaelwinston23
Posts: 1
Joined: Mon May 07, 2012 3:41 am

Re: reformat component issue in java

Postby rafaelwinston23 » Mon May 07, 2012 4:25 am

thanks for sharing....

rovarellasbt
Posts: 2
Joined: Thu Aug 11, 2016 7:34 pm

Re: reformat component issue in java

Postby rovarellasbt » Thu Aug 11, 2016 7:46 pm

Hello, I had a similar problem but I can't find any information regarding which Clover classes should be used and not.

Where I can find this information (internal classes vs API classes)?

Thank you for your attention

jurban wrote:Hi,

your issue can be broken down into 2 parts:

1) I believe you're extending the CTLRecordTransform class as the base for your transformation. That class is internally used by our CTL to Java compiler, it's not intended to be used elsewhere. You should extend the DataRecordTransform class and implement the transform() method - this will be your "main".

2) The specific cause of the error you're seeing is that the sign() method throws SignatureException, and you don't catch the exception at the location where you're calling the method (I'm guessing that your original main had a "throws Exception" clause). You should catch the SignatureException and either re-throw it by wrapping it in a TransformException, or handle the error in some other way (e.g. send some record to a different output port for error logging).

Combining the 2 hints above should help - extend DataRecordTransform and catch the SignatureException.

Best regards,
Jaro

imriskal
Posts: 397
Joined: Wed Aug 15, 2012 8:18 am

Re: reformat component issue in java

Postby imriskal » Mon Aug 15, 2016 9:58 am

The most frequently used classes available are described in CustomJavaComponent documentation.
---
Lubos Imriska
CloverCARE Support
CloverDX

Visit us online at http://www.cloverdx.com

rovarellasbt
Posts: 2
Joined: Thu Aug 11, 2016 7:34 pm

Re: reformat component issue in java

Postby rovarellasbt » Mon Aug 22, 2016 5:41 pm

Thank your your support!