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 import java.text.DateFormat ; 46 import java.text.ParseException ; 47 import java.text.SimpleDateFormat ; 48 import java.util.Date ; 49 import java.util.Locale ; 50 import java.util.TimeZone ; 51 52 import javax.servlet.ServletException ; 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletResponse ; 55 56 import org.apache.log4j.Logger; 57 import org.dspace.app.webui.util.JSPManager; 58 import org.dspace.authorize.AuthorizeException; 59 import org.dspace.content.Bitstream; 60 import org.dspace.content.Bundle; 61 import org.dspace.content.DSpaceObject; 62 import org.dspace.content.Item; 63 import org.dspace.core.Constants; 64 import org.dspace.core.Context; 65 import org.dspace.core.LogManager; 66 import org.dspace.core.Utils; 67 import org.dspace.handle.HandleManager; 68 69 79 public class BitstreamServlet extends DSpaceServlet 80 { 81 82 private static Logger log = Logger.getLogger(BitstreamServlet.class); 83 84 protected void doDSGet(Context context, HttpServletRequest request, 85 HttpServletResponse response) throws ServletException , IOException , 86 SQLException , AuthorizeException 87 { 88 Item item = null; 89 Bitstream bitstream = null; 90 91 String idString = request.getPathInfo(); 93 String handle = ""; 94 String sequenceText = ""; 95 String filename = null; 96 int sequenceID; 97 98 105 if (idString.startsWith("/")) 107 { 108 idString = idString.substring(1); 109 } 110 111 int slashIndex = idString.indexOf('/'); 113 if (slashIndex != -1) 114 { 115 slashIndex = idString.indexOf('/', slashIndex + 1); 116 if (slashIndex != -1) 117 { 118 handle = idString.substring(0, slashIndex); 119 int slash2 = idString.indexOf('/', slashIndex + 1); 120 if (slash2 != -1) 121 { 122 sequenceText = idString.substring(slashIndex+1,slash2); 123 filename = idString.substring(slash2+1); 124 } 125 } 126 } 127 128 try 129 { 130 sequenceID = Integer.parseInt(sequenceText); 131 } 132 catch (NumberFormatException nfe) 133 { 134 sequenceID = -1; 135 } 136 137 DSpaceObject dso = HandleManager.resolveToObject(context, handle); 139 140 if (dso != null && dso.getType() == Constants.ITEM && sequenceID >= 0) 142 { 143 item = (Item) dso; 144 145 if (item.isWithdrawn()) 146 { 147 log.info(LogManager.getHeader(context, "view_bitstream", 148 "handle=" + handle + ",withdrawn=true")); 149 JSPManager.showJSP(request, response, "/tombstone.jsp"); 150 return; 151 } 152 153 boolean found = false; 154 155 Bundle[] bundles = item.getBundles(); 156 157 for (int i = 0; (i < bundles.length) && !found; i++) 158 { 159 Bitstream[] bitstreams = bundles[i].getBitstreams(); 160 161 for (int k = 0; (k < bitstreams.length) && !found; k++) 162 { 163 if (sequenceID == bitstreams[k].getSequenceID()) 164 { 165 bitstream = bitstreams[k]; 166 found = true; 167 } 168 } 169 } 170 } 171 172 if (bitstream == null || filename == null 173 || !filename.equals(bitstream.getName())) 174 { 175 log.info(LogManager.getHeader(context, "invalid_id", "path=" 177 + idString)); 178 JSPManager.showInvalidIDError(request, response, idString, 179 Constants.BITSTREAM); 180 181 return; 182 } 183 184 log.info(LogManager.getHeader(context, "view_bitstream", 185 "bitstream_id=" + bitstream.getID())); 186 187 response.setDateHeader("Last-Modified", item.getLastModified() 191 .getTime()); 192 193 long modSince = request.getDateHeader("If-Modified-Since"); 195 196 if (modSince != -1 && item.getLastModified().getTime() < modSince) 197 { 198 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 201 return; 202 } 203 204 InputStream is = bitstream.retrieve(); 206 207 response.setContentType(bitstream.getFormat().getMIMEType()); 209 210 response.setHeader("Content-Length", String 212 .valueOf(bitstream.getSize())); 213 214 Utils.bufferedCopy(is, response.getOutputStream()); 215 is.close(); 216 response.getOutputStream().flush(); 217 } 218 } 219 | Popular Tags |