problem with jsp - Base64 decode POST Parameter

I

inetquestion

I am trying to piece together a jsp based on the original PHP code
below to base64 decode a post parameter called: SAMLResponse. The
decoded param should then be printed to the screen with a content type
of xml.

I am having problems getting this to work as I'm not very proficient
at java... The method "Base64.decode" does not appear to be a
standard library...

-Inet


<%@ page import="java.util.*" %>
<%@ page contentType="application/xml" %>

<%
// Get POST param and decode into array
String base64Message = request.getParameter("SAMLResponse");
byte[] decodedBytes = Base64.decode(base64Message);
if (decodedBytes == null) {
String message = "Unable to Base64 decode SAML message";
log.error(message);
throw new MessageDecodingException(message);
}


// Convert the decoded byte[] to the original string and print the
result.
String decodedString = new String(decodedBytes);

System.out.println(decodedString);
%>


-------------------------------------------
PHP Version follows:
-------------------------------------------


<?php
header('Content-type: application/xml');
$decoded_data = base64_decode($_POST['SAMLResponse']);
echo "$decoded_data";
?>
 
D

D M

<%@ page import="java.util.*" %>

Maybe you should add <%@ page
import="org.apache.commons.codec.binary.Base64" %> and add Apache
Commons JAR to classpath?
 
A

Arne Vajhøj

I am trying to piece together a jsp based on the original PHP code
below to base64 decode a post parameter called: SAMLResponse. The
decoded param should then be printed to the screen with a content type
of xml.

I am having problems getting this to work as I'm not very proficient
at java... The method "Base64.decode" does not appear to be a
standard library...
<%@ page import="java.util.*" %>
<%@ page contentType="application/xml" %>

<%
// Get POST param and decode into array
String base64Message = request.getParameter("SAMLResponse");
byte[] decodedBytes = Base64.decode(base64Message);
if (decodedBytes == null) {
String message = "Unable to Base64 decode SAML message";
log.error(message);
throw new MessageDecodingException(message);
}


// Convert the decoded byte[] to the original string and print the
result.
String decodedString = new String(decodedBytes);

System.out.println(decodedString);
%>

There are Base64 support in Java EE.

Method using that:

public static byte[] b64decode(String s) throws
MessagingException, IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
InputStream b64is = MimeUtility.decode(bais, "Base64");
byte[] tmp = new byte[s.length()];
int n = b64is.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return res;
}

Arne
 
A

Arne Vajhøj

Maybe you should add<%@ page
import="org.apache.commons.codec.binary.Base64" %> and add Apache
Commons JAR to classpath?

One way.

But given that Base64 are in Java EE, then using an external
library should not be necessary in that context.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top