Pages

Sunday, September 6, 2009

Struts - DownloadAction

Every struts starter is facing the problem while writing the code for uploading a file. Especially to reduce the burden of writing long code for uploading struts introduced DownloadAction as an added feature from 1.2.6 version onwards. Using this action very easy like eating banana.

i am giving small example on how to use DownloadAction

=======================================================================

public class downloadFileAction extends org.apache.struts.actions.DownloadAction {

protected StreamInfo getStreamInfo(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

// File Name
String fileName = "c:\\WriteSelectedRows.pdf";

/*
* To open up in the browser: "inline; filename=myFile.pdf"
* To download: "attachment; filename=myFile.pdf"
* URL for reference: http://wiki.apache.org/struts/StrutsFileDownload
*/

// Set the content disposition
response.setHeader("Content-disposition",
"attachment; filename=" + fileName);

// Download a "pdf" file - gets the file name from the
// Action Mapping's parameter
String contentType = "application/pdf";
File file = new File(fileName);

return new FileStreamInfo(contentType, file);

}
}

=======================================================================

if you want to know more about the Download Action Please go through the URL specified in the above example.

Tuesday, September 1, 2009

Encrypt URL

We have a major problem while calling some hyperlinks which will carry sensitive data, to avoid showing the parameters data while calling hyperlinks we have one beautiful concept in java is encryption we can do the encryption of total data and can decrypt the data while using.
But how to write the encryption code and decryption code, dont worry we no need to write the long lines code, we can use open source code for encryption and decryption given by Query Crypt.

Just follow the sample to encrypt the data.


                        <%@ page import="com.guhesan.querycrypt.QueryCrypt" %>
<%
String plainString = "BankID=1234&AccountID=J5678&TrasactionID=25";
String encryptedS = QueryCrypt.getInstance().encrypt(request, plainString);
%>
A HREF="<%=request.getContextPath()%>/URL_U_WANT_TO_GO.jsp?<%=encryptedS%>">Click here for the secure transaction /A
To do the decryption just go through the below code.

Suppose you had a encrypted string such as the following:
someURL.jsp?_tq=c95a29f62075cf63028be5ec6ba48be0
To decrypt in JSP:
  <%@ page import="com.guhesan.querycrypt.QueryCrypt" %>
<%@ page import="com.guhesan.querycrypt.beans.RequestParameterObject" %>
<%
RequestParameterObject rpo = QueryCrypt.getInstance().decrypt(request);
%>
You can now use the methods available in the RequestParameterObject to get the name value pairs.
The following methods are available for RequestParameterObject:
    java.util.Map RequestParameterObject.getParameterMap();
java.lang.String RequestParameterObject.getParameter(java.lang.String name);
java.util.Enumeration RequestParameterObject.getParameterNames();
java.lang.String[] RequestParameterObject.getParameterValues(java.lang.String name);
String RequestParameterObject.toString();


Source URL: http://www.avedatech.com/Products/QueryCrypt/index.jsp
you can download source files, jar files related to query crypt.