Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #7060 direct download causes RedirectException #7085

Merged
merged 3 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import java.io.OutputStream;
import java.io.IOException;

import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

Expand Down Expand Up @@ -196,10 +196,10 @@ public void writeTo(BundleDownloadInstance di, Class<?> clazz, Type type, Annota
}
}
} catch (IOException ioex) {
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
throw new InternalServerErrorException();
}

throw new WebApplicationException(Response.Status.NOT_FOUND);
throw new NotFoundException();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void writeTo(DownloadInstance di, Class<?> clazz, Type type, Annotation[]
}

if (redirect_url_str == null) {
throw new WebApplicationException(new ServiceUnavailableException());
throw new ServiceUnavailableException();
}

logger.fine("Data Access API: direct S3 url: "+redirect_url_str);
Expand Down Expand Up @@ -274,7 +274,7 @@ public void writeTo(DownloadInstance di, Class<?> clazz, Type type, Annotation[]
logger.fine("Issuing redirect to the file location on S3.");
throw new RedirectionException(response);
}
throw new WebApplicationException(new ServiceUnavailableException());
throw new ServiceUnavailableException();
}
}

Expand Down Expand Up @@ -368,7 +368,7 @@ public void writeTo(DownloadInstance di, Class<?> clazz, Type type, Annotation[]
}
}

throw new WebApplicationException(Response.Status.NOT_FOUND);
throw new NotFoundException();

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package edu.harvard.iq.dataverse.api.errorhandlers;

import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.json.Json;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/**
* Produces custom 500 messages for the API.
* @author qqmyers
*/
@Provider
public class InternalServerErrorExceptionHandler implements ExceptionMapper<InternalServerErrorException>{

private static final Logger logger = Logger.getLogger(InternalServerErrorExceptionHandler.class.getName());

@Context
HttpServletRequest request;

@Override
public Response toResponse(InternalServerErrorException ex){
String incidentId = UUID.randomUUID().toString();
logger.log(Level.SEVERE, "API internal error " + incidentId +": " + ex.getMessage(), ex);
return Response.status(500)
.entity( Json.createObjectBuilder()
.add("status", "ERROR")
.add("code", 500)
.add("message", "Internal server error. More details available at the server logs.")
.add("incidentId", incidentId)
.build())
.type("application/json").build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.harvard.iq.dataverse.api.errorhandlers;

import edu.harvard.iq.dataverse.util.BundleUtil;
import javax.json.Json;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.RedirectionException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/**
*
* @author qqmyers
*/
@Provider
public class RedirectionExceptionHandler implements ExceptionMapper<RedirectionException> {

@Context
HttpServletRequest request;

@Override
public Response toResponse(RedirectionException ex) {
return ex.getResponse();
}

}