-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backward compatible SWORD upload on Payara 6 #8305
As of Payara 6, you can no longer send "Content-Disposition: filename=example.zip" Instead you must send "Content-Disposition: attachment; filename=example.zip" For backward compatibility, we are adding "attachment; " if it's missing. An alternative would be to force this backward incompatibility on our users. Originally we did this in 89182c1 but we reverted it in 83c55a9
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/edu/harvard/iq/dataverse/api/datadeposit/SwordFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package edu.harvard.iq.dataverse.api.datadeposit; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import java.io.IOException; | ||
|
||
public class SwordFilter implements jakarta.servlet.Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
HttpServletRequest req = (HttpServletRequest) request; | ||
MutateHeaders requestWrapper = new MutateHeaders(req); | ||
chain.doFilter(requestWrapper, response); | ||
} | ||
|
||
/** | ||
* We are mutating headers because Payara 6 is more strict than Paraya 5 and | ||
* wants "attachment; filename=" instead of just "filename=". In order to | ||
* not break backward compatibility, we add "attachment; " for our (SWORD) | ||
* API users. (This only seem to affect our SWORD API.) That is, the can | ||
* continue to send '-H "Content-Disposition: filename=example.zip"' as | ||
* we've documented for years. | ||
*/ | ||
public class MutateHeaders extends HttpServletRequestWrapper { | ||
|
||
public MutateHeaders(HttpServletRequest request) { | ||
super(request); | ||
} | ||
|
||
// inspired by https://stackoverflow.com/questions/2811769/adding-an-http-header-to-the-request-in-a-servlet-filter/2811841#2811841 | ||
@Override | ||
public String getHeader(String name) { | ||
String header = super.getHeader(name); | ||
if ("Content-Disposition".equalsIgnoreCase(name)) { | ||
if (header.startsWith("filename=")) { | ||
header = header.replaceFirst("filename=", "attachment; filename="); | ||
} | ||
} | ||
return (header != null) ? header : super.getParameter(name); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters