added redirect functionality in download servlet

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@77288 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2013-06-25 16:26:25 +00:00
parent 8c0e5b1663
commit fce6d410d8
3 changed files with 78 additions and 44 deletions

View File

@ -783,7 +783,7 @@ public class AppControllerExplorer implements EventHandler, TreeAppControllerInt
if(fileDownloadEvent.getItemName()!= null){
try {
new RequestBuilderWorkspaceValidateItem(RequestBuilder.GET, ConstantsExplorer.DOWNLOAD_WORKSPACE_SERVICE, "id="+fileDownloadEvent.getItemIdentifier()+"&viewContent=true", "_self", downloadHandlerCallback);
new RequestBuilderWorkspaceValidateItem(RequestBuilder.GET, ConstantsExplorer.DOWNLOAD_WORKSPACE_SERVICE, "id="+fileDownloadEvent.getItemIdentifier()+"&viewContent=true", "_blank", downloadHandlerCallback);
} catch (Exception e) {
explorerPanel.getAsycTreePanel().unmask();

View File

@ -198,5 +198,5 @@ public class ConstantsExplorer {
//SERLVET ERROR
public static final String ERROR_ITEM_DOES_NOT_EXIST = "Item does not exist. It may have been deleted by another user";
public static final String VALIDATEITEM = "validateitem";
public static final String REDIRECTONERROR = "redirectonerror";
}

View File

@ -57,8 +57,6 @@ public class DownloadServlet extends HttpServlet{
protected static GCUBELog logger = new GCUBELog(DownloadServlet.class);
/**
* {@inheritDoc}
*/
@ -76,8 +74,10 @@ public class DownloadServlet extends HttpServlet{
String itemId = req.getParameter("id");
boolean viewContent = (req.getParameter("viewContent")==null)?false:req.getParameter("viewContent").equals("true");
boolean isValidItem = (req.getParameter(ConstantsExplorer.VALIDATEITEM)==null)?false:req.getParameter(ConstantsExplorer.VALIDATEITEM).equals("true");
boolean urlRedirectOnError = (req.getParameter(ConstantsExplorer.REDIRECTONERROR)==null)?false:req.getParameter(ConstantsExplorer.REDIRECTONERROR).equals("true");
logger.trace("Input Params [id: "+itemId + ", viewContent: "+viewContent+", "+ConstantsExplorer.VALIDATEITEM +":" +isValidItem+"]");
logger.trace("Input Params [id: "+itemId + ", viewContent: "+viewContent+", "+ConstantsExplorer.VALIDATEITEM +": " +isValidItem+", urlRedirectOnError:" +urlRedirectOnError+"]");
if(itemId==null || itemId.isEmpty()){
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Item id is null");
@ -90,12 +90,14 @@ public class DownloadServlet extends HttpServlet{
try {
wa = WsUtil.getWorkspace(req.getSession());
} catch (Exception e) {
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during workspace retrieving");
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during workspace retrieving");
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during workspace retrieving");
return;
}
if (wa == null) {
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no workspace in session");
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no workspace in session");
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no workspace in session");
return;
}
@ -106,7 +108,7 @@ public class DownloadServlet extends HttpServlet{
if(isValidItem){ //ADDED 25/06/2013
String message = HttpServletResponse.SC_ACCEPTED+ ": The resource is available";
sendMessageContinue(resp, message);
sendMessageResourceAvailable(resp, message);
logger.trace("response return: "+message);
return;
}
@ -122,8 +124,8 @@ public class DownloadServlet extends HttpServlet{
} catch (ItemNotFoundException e) {
logger.error("Requested item "+itemId+" not found",e);
// sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no items found");
sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": "+ConstantsExplorer.ERROR_ITEM_DOES_NOT_EXIST);
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": "+ConstantsExplorer.ERROR_ITEM_DOES_NOT_EXIST);
// sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": "+ConstantsExplorer.ERROR_ITEM_DOES_NOT_EXIST);
return;
}
@ -150,9 +152,10 @@ public class DownloadServlet extends HttpServlet{
tmpZip.delete();
return;
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during folder compression "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during folder compression: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during folder compression: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during folder compression: "+e.getMessage());
return;
}
}
@ -179,9 +182,10 @@ public class DownloadServlet extends HttpServlet{
is.close();
out.close();
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during external item sending "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
return;
@ -207,9 +211,10 @@ public class DownloadServlet extends HttpServlet{
is.close();
out.close();
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during external item sending "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
return;
@ -247,9 +252,10 @@ public class DownloadServlet extends HttpServlet{
is.close();
out.close();
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during external item sending "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
return;
@ -278,9 +284,10 @@ public class DownloadServlet extends HttpServlet{
out.close();
return;
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during item compression "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
@ -309,9 +316,10 @@ public class DownloadServlet extends HttpServlet{
sr.close();
out.close();
return;
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during item compression "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
@ -321,9 +329,10 @@ public class DownloadServlet extends HttpServlet{
resp.setContentType("text/plain");
try {
resp.setContentLength((int) query.getLength());
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error getting item lenght "+query,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
@ -357,9 +366,10 @@ public class DownloadServlet extends HttpServlet{
out.close();
tmpZip.delete();
return;
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during item compression "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
@ -383,9 +393,10 @@ public class DownloadServlet extends HttpServlet{
out.close();
tmpZip.delete();
return;
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during item compression "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
@ -429,9 +440,10 @@ public class DownloadServlet extends HttpServlet{
out.close();
}
return;
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during item compression "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
}
@ -452,9 +464,10 @@ public class DownloadServlet extends HttpServlet{
out.close();
return;
} catch (InternalErrorException e) {
} catch (Exception e) {
logger.error("Error during item compression "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
}
@ -498,24 +511,29 @@ public class DownloadServlet extends HttpServlet{
}
else{
logger.error("Input stream is null "+itemId);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during inpunt stream retrieving, it's null");
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during inpunt stream retrieving, it's null");
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during inpunt stream retrieving, it's null");
}
} catch (ExternalResourceBrokenLinkException e) {
logger.error("Error during link resource retrieving "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, link broken!: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, link broken!: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, link broken!: "+e.getMessage());
} catch (ExternalResourcePluginNotFoundException e) {
logger.error("Error during link resource retrieving "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, plugin not found!: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, plugin not found!: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during link retrieving, plugin not found!: "+e.getMessage());
}
out.close();
} catch (InternalErrorException e) {
logger.error("Error during external item sending "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving: "+e.getMessage());
return;
} catch (Exception e) {
logger.error("Error during resource retrieving "+itemId,e);
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during resource retrieving, plugin not found!: "+e.getMessage());
handleError(urlRedirectOnError, req, resp, itemId,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during resource retrieving! "+e.getMessage());
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during resource retrieving, plugin not found!: "+e.getMessage());
e.printStackTrace();
}
@ -529,14 +547,27 @@ public class DownloadServlet extends HttpServlet{
}
}
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving");
handleError(urlRedirectOnError, req, resp, itemId,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving");
// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving");
return;
}
protected void handleError(boolean urlRedirectOnError, HttpServletRequest req, HttpServletResponse resp, String itemId, String message) throws IOException{
if(urlRedirectOnError){
String path = req.getRequestURI().substring(req.getContextPath().length());
urlRedirect(resp, path, itemId);
}else
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during data retrieving");
}
protected void sendError(HttpServletResponse response, String message) throws IOException
{
{
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
HandlerResultMessage resultMessage = HandlerResultMessage.errorResult(message);
response.getWriter().write(resultMessage.toString());
@ -551,7 +582,7 @@ public class DownloadServlet extends HttpServlet{
response.flushBuffer();
}
protected void sendMessageContinue(HttpServletResponse response, String message) throws IOException
protected void sendMessageResourceAvailable(HttpServletResponse response, String message) throws IOException
{
response.setStatus(HttpServletResponse.SC_ACCEPTED);
HandlerResultMessage resultMessage = HandlerResultMessage.okResult(message);
@ -567,6 +598,12 @@ public class DownloadServlet extends HttpServlet{
response.flushBuffer();
}
protected void urlRedirect(HttpServletResponse response, String contextPath, String url) throws IOException {
response.sendRedirect(response.encodeRedirectURL(contextPath + "/" +url));
return;
}
public static void main(String[] args) {
InputStream is = null;
@ -614,8 +651,5 @@ public class DownloadServlet extends HttpServlet{
}catch (Exception e) {
e.printStackTrace();
}
}
}