2

I wrote a servlet (with .groovy extension) which should return xml in Base64 encoding

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String fileName=(String) request.getParameter("fileName")

    if (fileName == null || fileName.equals(""))throw new ServletException("Invalid or non-existent file parameter in SendXml servlet.")
    if (fileName.indexOf(".xml") == -1)fileName = fileName + ".xml"

    System.out.println(fileName)

    try {

      String relativeWebPath = "/WEB-INF/classes/com/abc/csm/xml/"+fileName
      String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath)

      String fileContents=new File(absoluteDiskPath).text
      response.setContentType("text/xml")
      response.addHeader("Content-Disposition", "attachment filename="+ fileName)

      XmlHandler xm=new XmlHandler()

      PrintWriter out = response.getWriter()
      String enxml=xm.encodeBase64(fileContents)
      response.setContentLength((int) enxml.length)

      out.println(enxml)
      out.close()
      out.flush()
    } catch (Exception e) {       println e     } 
}

XmlHandler encodeBase64 method

def encodeBase64(String text) {
    return new String(Base64.encodeBase64(text.getBytes()))
}

But I guess something is missing. Please help

Update

Also please comment is it proper way to access files from package?

AabinGunz
  • 12,109
  • 54
  • 146
  • 218

1 Answers1

4

You can replace:

  String enxml=xm.encodeBase64(fileContents)

with

  String enxml = fileContents.bytes.encodeBase64()

Or, better to pass an encoding to the call to getBytes:

  String enxml = fileContents.getBytes( 'UTF-8' ).encodeBase64()

Also, I believe you're missing a semicolon after attachment in your header... The line should read:

  response.addHeader( "Content-Disposition", "attachment; filename=$fileName" )

Edit, and encoding and decoding example:

Encode:

String encoded = "tim_yates".getBytes( 'UTF-8' ).encodeBase64()

Decode:

String original = new String( encoded.decodeBase64(), 'UTF-8' )
assert original == 'tim_yates'
Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • I get back the response, in fiddler I can see it but when I assign it to a variable and send it again to another redirection it has some other data appended to it. Is `response.setContentType("text/xml")` correct for this encoding? – AabinGunz May 03 '12 at 09:10
  • @AbhishekSimon Updated with decode example... If you are sending Base64 encoded data, then I believe you should do `response.addHeader( 'Content-Transfer-Encoding', 'base64' )`. Why not just send the plain XML without Base64 encoding though? – tim_yates May 03 '12 at 09:14
  • the original project sends it through encoding it, so I have to mimic the same thing with some extra features – AabinGunz May 03 '12 at 09:19
  • I used your edited code, now when I receive and resend it to another servlet it gives me `java.lang.RuntimeException: bad character in base64 value` sorry for so much trouble – AabinGunz May 03 '12 at 09:31
  • 1
    @AbhishekSimon no worries ;-) If you've set the content transfer encoding, the other end might be decoding it to xml automatically... Print it out before you decode it to check – tim_yates May 03 '12 at 09:36
  • Ok it prints the encoded xml (some garbage) I used [this](http://coderstoolbox.net/string/) to check the xml and it looks good. I am using `println xml root=new XmlParser().parseText(new String( xml.decodeBase64(), 'UTF-8' ))` – AabinGunz May 03 '12 at 09:42
  • No it still gives `java.lang.RuntimeException: bad character in base64 value` – AabinGunz May 03 '12 at 10:33
  • Ya in the online tool it decodes fine. The xml is more than 3 MB size if that helps – AabinGunz May 03 '12 at 10:35
  • In my test groovy program it says `Groovy:String too long. The given string is 174996 Unicode code units long, but only a maximum of 65535 is allowed.` – AabinGunz May 03 '12 at 10:40
  • Try using a different decoding method http://stackoverflow.com/questions/469695/decode-base64-data-in-java – tim_yates May 03 '12 at 10:48
  • @AbhishekSimon I suspect the problem you're getting with your test script is that by dumping the huge xml into the script, you're hitting JVM class file size limitations... – tim_yates May 03 '12 at 13:12
  • I used commons codec and it worked. thanks for all the help :) – AabinGunz May 03 '12 at 13:29