1 40 package org.dspace.app.webui.servlet; 41 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.sql.SQLException ; 45 46 import javax.servlet.ServletException ; 47 import javax.servlet.http.HttpServletRequest ; 48 import javax.servlet.http.HttpServletResponse ; 49 50 import org.apache.log4j.Logger; 51 import org.dspace.app.webui.util.JSPManager; 52 import org.dspace.authorize.AuthorizeException; 53 import org.dspace.content.Bitstream; 54 import org.dspace.core.Constants; 55 import org.dspace.core.Context; 56 import org.dspace.core.LogManager; 57 import org.dspace.core.Utils; 58 59 67 public class RetrieveServlet extends DSpaceServlet 68 { 69 70 private static Logger log = Logger.getLogger(RetrieveServlet.class); 71 72 protected void doDSGet(Context context, HttpServletRequest request, 73 HttpServletResponse response) throws ServletException , IOException , 74 SQLException , AuthorizeException 75 { 76 Bitstream bitstream = null; 77 78 String idString = request.getPathInfo(); 80 81 if (idString != null) 82 { 83 if (idString.startsWith("/")) 85 { 86 idString = idString.substring(1); 87 } 88 89 int slashIndex = idString.indexOf('/'); 92 93 if (slashIndex != -1) 94 { 95 idString = idString.substring(0, slashIndex); 96 } 97 98 try 100 { 101 int id = Integer.parseInt(idString); 102 bitstream = Bitstream.find(context, id); 103 } 104 catch (NumberFormatException nfe) 105 { 106 } 108 } 109 110 if (bitstream != null) 112 { 113 log.info(LogManager.getHeader(context, "view_bitstream", 114 "bitstream_id=" + bitstream.getID())); 115 116 response.setContentType(bitstream.getFormat().getMIMEType()); 118 119 response.setHeader("Content-Length", String.valueOf(bitstream 121 .getSize())); 122 123 InputStream is = bitstream.retrieve(); 125 126 Utils.bufferedCopy(is, response.getOutputStream()); 127 is.close(); 128 response.getOutputStream().flush(); 129 } 130 else 131 { 132 log.info(LogManager.getHeader(context, "view_bitstream", 134 "invalid_bitstream_id=" + idString)); 135 136 JSPManager.showInvalidIDError(request, response, idString, 137 Constants.BITSTREAM); 138 } 139 } 140 } 141 | Popular Tags |