diff --git a/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java b/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java index af0e2e4..84cb81b 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java @@ -3,7 +3,6 @@ package org.gcube.datatransfer.resolver.http; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.SocketException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; @@ -44,8 +43,7 @@ public class HttpResolver extends HttpServlet { } - protected void doGet(HttpServletRequest request, - HttpServletResponse response) throws IOException { + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { logger.info("The http session id is: " + request.getSession().getId()); @@ -90,10 +88,9 @@ public class HttpResolver extends HttpServlet { logger.debug("URI = "+ uri); - + InputStream in = null; try { - OutputStream out = response.getOutputStream(); if (fileName != null) @@ -116,9 +113,7 @@ public class HttpResolver extends HttpServlet { }); URLConnection uc = null; - - InputStream in = null; - + try { uc = ( URLConnection ) url.openConnection ( ); in = uc.getInputStream(); @@ -138,37 +133,50 @@ public class HttpResolver extends HttpServlet { in.close(); */ + if(in!=null) + //CHANGED BY FRANCESCO M. try { IOUtils.copy(in, out); - } catch (SocketException e){ - if (!e.getClass().getSimpleName().equals("ClientAbortException")) - throw e; - else - logger.warn("Skipping ClientAbortException: "+e.getMessage()); + } catch (IOException e){ + logger.warn("IOException class name: "+e.getClass().getSimpleName()); + if (e.getClass().getSimpleName().equals("ClientAbortException")) + logger.warn("Skipping ClientAbortException: "+e.getMessage()); + else + throw e; //Sending Exceptions + + } catch (NullPointerException e) { + logger.warn("NullPointerException: input stream is null, skipping printStrackTrace"); + sendErrorQuietly(response, 404); + } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } catch (Exception e) { -// e.printStackTrace(); logger.error("Exception:", e); // response.sendError(404); - - //CHANGED BY FRANCESCO M. - if(!response.isCommitted()) - response.sendError(404); - else - logger.warn("Response already committed, skipped send Error 404"); - + IOUtils.closeQuietly(in); + sendErrorQuietly(response, 404); return; } } + protected void sendErrorQuietly(HttpServletResponse response, int code){ + + if(response!=null){ + try { + response.sendError(code); + } catch (IOException ioe) { + // ignore + } + } + } + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { doGet(request,response);