Page 1 of 1

reformat component issue in java

Posted: Sat Apr 28, 2012 7:54 am
by pOctavian
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.

Re: reformat component issue in java

Posted: Mon Apr 30, 2012 9:45 am
by jurban
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

Re: reformat component issue in java

Posted: Tue May 01, 2012 2:46 am
by pOctavian
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.

Re: reformat component issue in java

Posted: Tue May 01, 2012 3:11 am
by pOctavian
Hi,

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

Thanks.

Re: reformat component issue in java

Posted: Wed May 02, 2012 6:03 am
by pOctavian
Hi Jaro,

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

Re: reformat component issue in java

Posted: Mon May 07, 2012 4:25 am
by rafaelwinston23
thanks for sharing....

Re: reformat component issue in java

Posted: Thu Aug 11, 2016 7:46 pm
by rovarellasbt
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

Re: reformat component issue in java

Posted: Mon Aug 15, 2016 9:58 am
by imriskal
The most frequently used classes available are described in CustomJavaComponent documentation.

Re: reformat component issue in java

Posted: Mon Aug 22, 2016 5:41 pm
by rovarellasbt
Thank your your support!