Skip to content

Commit

Permalink
Merge pull request #7085 from GlobalDataverseCommunityConsortium/IQSS…
Browse files Browse the repository at this point in the history
…/7060

Fix for #7060 direct download causes RedirectException
  • Loading branch information
kcondon committed Jul 17, 2020
2 parents 875374f + 70ba37c commit c9ec158
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
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();
}

}

0 comments on commit c9ec158

Please sign in to comment.