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;
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”.
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.
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.
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
Hi,
your issue can be broken down into 2 parts:
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”.
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.