String name = par.getParameter("name", null);
this.isSpider = par.getParameter("userAgent", "").equals("spider");
// Resolve the bitstream
Bitstream bitstream = null;
DSpaceObject dso = null;
if (bitstreamID > -1)
{
// Direct reference to the individual bitstream ID.
bitstream = Bitstream.find(context, bitstreamID);
}
else if (itemID > -1)
{
// Referenced by internal itemID
item = Item.find(context, itemID);
if (sequence > -1)
{
bitstream = findBitstreamBySequence(item, sequence);
}
else if (name != null)
{
bitstream = findBitstreamByName(item, name);
}
}
else if (handle != null)
{
// Reference by an item's handle.
dso = HandleManager.resolveToObject(context,handle);
if (dso instanceof Item)
{
item = (Item)dso;
if (sequence > -1)
{
bitstream = findBitstreamBySequence(item,sequence);
}
else if (name != null)
{
bitstream = findBitstreamByName(item,name);
}
}
}
// if initial search was by sequence number and found nothing,
// then try to find bitstream by name (assuming we have a file name)
if((sequence > -1 && bitstream==null) && name!=null)
{
bitstream = findBitstreamByName(item,name);
// if we found bitstream by name, send a redirect to its new sequence number location
if(bitstream!=null)
{
String redirectURL = "";
// build redirect URL based on whether item has a handle assigned yet
if(item.getHandle()!=null && item.getHandle().length()>0)
{
redirectURL = request.getContextPath() + "/bitstream/handle/" + item.getHandle();
}
else
{
redirectURL = request.getContextPath() + "/bitstream/item/" + item.getID();
}
redirectURL += "/" + name + "?sequence=" + bitstream.getSequenceID();
HttpServletResponse httpResponse = (HttpServletResponse)
objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
httpResponse.sendRedirect(redirectURL);
return;
}
}
// Was a bitstream found?
if (bitstream == null)
{
throw new ResourceNotFoundException("Unable to locate bitstream");
}
// Is there a User logged in and does the user have access to read it?
boolean isAuthorized = AuthorizeManager.authorizeActionBoolean(context, bitstream, Constants.READ);
if (item != null && item.isWithdrawn() && !AuthorizeManager.isAdmin(context))
{
isAuthorized = false;
log.info(LogManager.getHeader(context, "view_bitstream", "handle=" + item.getHandle() + ",withdrawn=true"));
}
// It item-request is enabled to all request we redirect to restricted-resource immediately without login request
String requestItemType = ConfigurationManager.getProperty("request.item.type");
if (!isAuthorized)
{
if(context.getCurrentUser() != null || StringUtils.equalsIgnoreCase("all", requestItemType)){
// A user is logged in, but they are not authorized to read this bitstream,
// instead of asking them to login again we'll point them to a friendly error
// message that tells them the bitstream is restricted.
String redictURL = request.getContextPath() + "/handle/";
if (item!=null){
redictURL += item.getHandle();
}
else if(dso!=null){
redictURL += dso.getHandle();
}
redictURL += "/restricted-resource?bitstreamId=" + bitstream.getID();
HttpServletResponse httpResponse = (HttpServletResponse)
objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
httpResponse.sendRedirect(redictURL);
return;
}
else{
if(ConfigurationManager.getProperty("request.item.type")==null||
ConfigurationManager.getProperty("request.item.type").equalsIgnoreCase("logged")){
// The user does not have read access to this bitstream. Interrupt this current request
// and then forward them to the login page so that they can be authenticated. Once that is
// successful, their request will be resumed.
AuthenticationUtil.interruptRequest(objectModel, AUTH_REQUIRED_HEADER, AUTH_REQUIRED_MESSAGE, null);
// Redirect
String redictURL = request.getContextPath() + "/login";
HttpServletResponse httpResponse = (HttpServletResponse)
objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
httpResponse.sendRedirect(redictURL);
return;
}
}
}
// Success, bitstream found and the user has access to read it.
// Store these for later retrieval:
// Intercepting views to the original bitstream to instead show a citation altered version of the object
// We need to check if this resource falls under the "show watermarked alternative" umbrella.
// At which time we will not return the "bitstream", but will instead on-the-fly generate the citation rendition.
// What will trigger a redirect/intercept?
// 1) Intercepting Enabled
// 2) This User is not an admin
// 3) This object is citation-able
if (CitationDocument.isCitationEnabledForBitstream(bitstream, context)) {
// on-the-fly citation generator
log.info(item.getHandle() + " - " + bitstream.getName() + " is citable.");
FileInputStream fileInputStream = null;
CitationDocument citationDocument = new CitationDocument();
try {
//Create the cited document
tempFile = citationDocument.makeCitedDocument(bitstream);
if(tempFile == null) {
log.error("CitedDocument was null");
} else {
log.info("CitedDocument was ok," + tempFile.getAbsolutePath());
}
fileInputStream = new FileInputStream(tempFile);
if(fileInputStream == null) {
log.error("Error opening fileInputStream: ");
}
this.bitstreamInputStream = fileInputStream;
this.bitstreamSize = tempFile.length();
} catch (Exception e) {
log.error("Caught an error with intercepting the citation document:" + e.getMessage());
}
//End of CitationDocument
} else {
this.bitstreamInputStream = bitstream.retrieve();
this.bitstreamSize = bitstream.getSize();
}
this.bitstreamMimeType = bitstream.getFormat().getMIMEType();
this.bitstreamName = bitstream.getName();
if (context.getCurrentUser() == null)
{
this.isAnonymouslyReadable = true;
}
else