problem with jsp - Base64 decode POST Parameter

Discussion in 'Java' started by inetquestion, Sep 22, 2010.

  1. inetquestion

    inetquestion Guest

    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";
    ?>
    inetquestion, Sep 22, 2010
    #1
    1. Advertising

  2. inetquestion

    D M Guest

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

    Maybe you should add <%@ page
    import="org.apache.commons.codec.binary.Base64" %> and add Apache
    Commons JAR to classpath?
    D M, Sep 22, 2010
    #2
    1. Advertising

  3. inetquestion

    Arne Vajhøj Guest

    On 21-09-2010 23:43, inetquestion wrote:
    > 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
    Arne Vajhøj, Sep 23, 2010
    #3
  4. inetquestion

    Arne Vajhøj Guest

    On 22-09-2010 04:59, D M wrote:
    >> <%@ page import="java.util.*" %>

    >
    > 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
    Arne Vajhøj, Sep 23, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guoqi Zheng

    decode base64 string

    Guoqi Zheng, Sep 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,819
    Rick Strahl [MVP]
    Sep 27, 2004
  2. =?Utf-8?B?QmVnaW5uZXI=?=

    Decode a PNG image from Base64

    =?Utf-8?B?QmVnaW5uZXI=?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    794
    =?Utf-8?B?QmVnaW5uZXI=?=
    Mar 9, 2006
  3. Damir Hakimov

    base64.encode and decode not correct

    Damir Hakimov, Aug 16, 2005, in forum: Python
    Replies:
    1
    Views:
    341
  4. Replies:
    0
    Views:
    543
  5. Mr. T

    decode base64 encoded login info

    Mr. T, Mar 5, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    506
    Patrice
    Mar 5, 2007
Loading...

Share This Page