user385599
user385599

Reputation: 25

My ArrayList does not work in jsp

I am not really a java developer but I am currently working on a project that require a bit of java skills. I got sample code from Cybersource to implement their secure acceptance payment but I am having an issue with their code if I use as is. I think the ArrayList may be the problem. It would not compile but I don't see the error message. I am working with a framework which makes it different to see the error message. Not sure how to just test this section of the code only.

${
 import sun.misc.BASE64Encoder;
 import javax.crypto.Mac; 
 import javax.crypto.spec.SecretKeySpec;
 import java.security.InvalidKeyException; 
 import java.security.NoSuchAlgorithmException; 
 import java.io.UnsupportedEncodingException; 
 import java.util.ArrayList; 
 import java.util.Iterator;

    private String sign(HashMap params) throws InvalidKeyException, NoSuchAlgorithmException,    {
        return sign(buildDataToSign(params), "111111111111111");
    }

    private String sign(String data, String secretKey) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(secretKeySpec);
        byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encodeBuffer(rawHmac).replace("\n", "");
    }

    private String buildDataToSign(HashMap params) {
        String[] signedFieldNames = String.valueOf(params.get("signed_field_names")).split(",");
        ArrayList<String> dataToSign = new ArrayList<String>();
        for (String signedFieldName : signedFieldNames) {
            dataToSign.add(signedFieldName + "=" + String.valueOf(params.get(signedFieldName)));
        }
        return commaSeparate(dataToSign);
    }

    private String commaSeparate(ArrayList<String> dataToSign) {
        StringBuilder csv = new StringBuilder();
        for (Iterator<String> it = dataToSign.iterator(); it.hasNext(); ) {
            csv.append(it.next());
            if (it.hasNext()) {
                csv.append(",");
            }
        }
         return csv.toString();
    }
}$  

Upvotes: 0

Views: 200

Answers (1)

Schroed
Schroed

Reputation: 204

What you need to do is create a class in a regular java file and then import it in your JSP like this:

<%@ page import="com.yourcompany.package.ClassName" %>

and you would be able to access any method from your JSP file, print variables, etc. Using the <% code> tag. For example:

<p>The sign response is <%= sign("test", "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=") %></p>

Upvotes: 4

Related Questions