1 40 package org.dspace.app.webui.servlet; 41 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.net.URLDecoder ; 45 import java.sql.SQLException ; 46 47 import javax.servlet.ServletException ; 48 import javax.servlet.http.HttpServletRequest ; 49 import javax.servlet.http.HttpServletResponse ; 50 51 import org.apache.log4j.Logger; 52 import org.dspace.app.webui.util.JSPManager; 53 import org.dspace.authorize.AuthorizeException; 54 import org.dspace.content.Bitstream; 55 import org.dspace.content.Bundle; 56 import org.dspace.content.Item; 57 import org.dspace.core.ConfigurationManager; 58 import org.dspace.core.Constants; 59 import org.dspace.core.Context; 60 import org.dspace.core.LogManager; 61 import org.dspace.core.Utils; 62 import org.dspace.handle.HandleManager; 63 64 84 public class HTMLServlet extends DSpaceServlet 85 { 86 87 private static Logger log = Logger.getLogger(HTMLServlet.class); 88 89 94 private int maxDepthGuess; 95 96 99 public HTMLServlet() 100 { 101 super(); 102 103 if (ConfigurationManager.getProperty("webui.html.max-depth-guess") != null) 104 { 105 maxDepthGuess = ConfigurationManager 106 .getIntProperty("webui.html.max-depth-guess"); 107 } 108 else 109 { 110 maxDepthGuess = 3; 111 } 112 } 113 114 private static Bitstream getItemBitstreamByName(Item item, String bsName) 118 throws SQLException 119 { 120 Bundle[] bundles = item.getBundles(); 121 122 for (int i = 0; i < bundles.length; i++) 123 { 124 Bitstream[] bitstreams = bundles[i].getBitstreams(); 125 126 for (int k = 0; k < bitstreams.length; k++) 127 { 128 if (bsName.equals(bitstreams[k].getName())) 129 return bitstreams[k]; 130 } 131 } 132 return null; 133 } 134 135 protected void doDSPost(Context context, HttpServletRequest request, 139 HttpServletResponse response) 140 throws ServletException , IOException , SQLException , AuthorizeException 141 { 142 doDSGet(context, request, response); 143 } 144 145 protected void doDSGet(Context context, HttpServletRequest request, 146 HttpServletResponse response) throws ServletException , IOException , 147 SQLException , AuthorizeException 148 { 149 Item item = null; 150 Bitstream bitstream = null; 151 152 String idString = request.getPathInfo(); 153 String filenameNoPath = null; 154 String fullpath = null; 155 String handle = null; 156 157 if (idString != null) 159 { 160 if (idString.startsWith("/")) 162 { 163 idString = idString.substring(1); 164 } 165 166 int slashIndex = idString.indexOf('/'); 168 if (slashIndex != -1) 169 { 170 slashIndex = idString.indexOf('/', slashIndex + 1); 171 if (slashIndex != -1) 172 { 173 handle = idString.substring(0, slashIndex); 174 fullpath = URLDecoder.decode(idString 175 .substring(slashIndex + 1), 176 Constants.DEFAULT_ENCODING); 177 178 slashIndex = fullpath.indexOf('/'); 180 if (slashIndex != -1) 181 { 182 String [] pathComponents = fullpath.split("/"); 183 if (pathComponents.length <= maxDepthGuess + 1) 184 { 185 filenameNoPath = pathComponents[pathComponents.length - 1]; 186 } 187 } 188 } 189 } 190 } 191 192 if (handle != null && fullpath != null) 193 { 194 try 196 { 197 203 if (handle.startsWith("db-id")) 204 { 205 String dbIDString = handle 206 .substring(handle.indexOf('/') + 1); 207 int dbID = Integer.parseInt(dbIDString); 208 item = Item.find(context, dbID); 209 } 210 else 211 { 212 item = (Item) HandleManager 213 .resolveToObject(context, handle); 214 } 215 } 216 catch (NumberFormatException nfe) 217 { 218 } 220 } 221 222 if (item != null) 223 { 224 bitstream = getItemBitstreamByName(item, fullpath); 226 227 if (bitstream == null && filenameNoPath != null) 228 { 229 bitstream = getItemBitstreamByName(item, filenameNoPath); 232 } 233 } 234 235 if (bitstream != null) 237 { 238 log.info(LogManager.getHeader(context, "view_html", "handle=" 239 + handle + ",bitstream_id=" + bitstream.getID())); 240 241 response.setContentType(bitstream.getFormat().getMIMEType()); 243 244 response.setHeader("Content-Length", String.valueOf(bitstream 246 .getSize())); 247 248 InputStream is = bitstream.retrieve(); 250 251 Utils.bufferedCopy(is, response.getOutputStream()); 252 is.close(); 253 response.getOutputStream().flush(); 254 } 255 else 256 { 257 log.info(LogManager.getHeader(context, "view_html", 259 "invalid_bitstream_id=" + idString)); 260 261 JSPManager.showInvalidIDError(request, response, idString, 262 Constants.BITSTREAM); 263 } 264 } 265 } 266 | Popular Tags |