Skip to content

Commit

Permalink
backward compatible SWORD upload on Payara 6 #8305
Browse files Browse the repository at this point in the history
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
pdurbin committed Jul 28, 2023
1 parent 83c55a9 commit 0221006
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
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);
}

}

}
8 changes: 8 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,13 @@
<servlet-name>edu.harvard.iq.dataverse.api.datadeposit.SWORDv2ContainerServlet</servlet-name>
<url-pattern>/dvn/api/data-deposit/v1.1/swordv2/edit/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>edu.harvard.iq.dataverse.api.datadeposit.SwordFilter</filter-name>
<filter-class>edu.harvard.iq.dataverse.api.datadeposit.SwordFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>edu.harvard.iq.dataverse.api.datadeposit.SwordFilter</filter-name>
<url-pattern>/dvn/api/data-deposit/v1.1/swordv2/edit-media/*</url-pattern>
</filter-mapping>
<!-- END Data Deposit API (SWORD v2) -->
</web-app>

0 comments on commit 0221006

Please sign in to comment.