Skip to content

Commit

Permalink
Merge pull request #56 from pmpowers-usgs/cors-headers-29
Browse files Browse the repository at this point in the history
Cors headers 29
  • Loading branch information
pmpowers-usgs authored May 4, 2017
2 parents 9565bb6 + 5b062a4 commit 23188ab
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions htdocs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h3>Service: hazard</h3>
<li>longitude <code>(-360..360)</code> °</li>
<li>latitude <code>[-90..90]</code> °</li>
<li>imt (intensity measure type) <code>[PGA, SA0P2, SA1P0]</code></li>
<li>vs30 <code>[180, 259, 360, 537, 1150, 2000]</code> m/s</li>
<li>vs30 <code>[180, 259, 360, 537, 760, 1150, 2000]</code> m/s</li>
</ul>
<div class="serviceExample">
<div class="serviceLink">/hazard/E2008/WUS/-118.25/34.05/PGA/760</div>
Expand All @@ -65,7 +65,7 @@ <h3>Service: deagg</h3>
<li>longitude <code>(-360..360)</code> °</li>
<li>latitude <code>[-90..90]</code> °</li>
<li>imt (intensity measure type) <code>[PGA, SA0P2, SA1P0]</code></li>
<li>vs30 <code>[180, 259, 360, 537, 1150, 2000]</code> m/s</li>
<li>vs30 <code>[180, 259, 360, 537, 760, 1150, 2000]</code> m/s</li>
<li>returnperiod <code>[1..4000]</code> years</li>
</ul>
<div class="serviceExample">
Expand Down
2 changes: 1 addition & 1 deletion src/gov/usgs/earthquake/nshm/www/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AdminService extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("application/json; charset=UTF-8");
ServletUtil.setCorsHeadersAndContentType(response);
response.getWriter().print("{\"hello\" : \"admin\"}");
}
}
2 changes: 1 addition & 1 deletion src/gov/usgs/earthquake/nshm/www/DeaggService.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected void doGet(
HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("application/json; charset=UTF-8");
ServletUtil.setCorsHeadersAndContentType(response);

String query = request.getQueryString();
String pathInfo = request.getPathInfo();
Expand Down
18 changes: 3 additions & 15 deletions src/gov/usgs/earthquake/nshm/www/HazardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import gov.usgs.earthquake.nshm.www.meta.Status;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.EnumMap;
import java.util.List;
Expand Down Expand Up @@ -115,7 +116,7 @@ protected void doGet(
HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("application/json; charset=UTF-8");
ServletUtil.setCorsHeadersAndContentType(response);

String query = request.getQueryString();
String pathInfo = request.getPathInfo();
Expand Down Expand Up @@ -188,7 +189,7 @@ private RequestData buildRequest(Map<String, String[]> paramMap) {

/* Reduce slash-delimited request */
private RequestData buildRequest(List<String> params) {

Set<Imt> imts = (params.get(4).equalsIgnoreCase("any"))
? Metadata.HAZARD_IMTS : readValues(params.get(4), Imt.class);

Expand Down Expand Up @@ -437,19 +438,6 @@ Result build() {
}
return new Result(url, responseListBuilder.build());
}

// // TODO replace with XySequence.addToMap
// private static <E extends Enum<E>> void addOrPut(
// Map<E, XySequence> map,
// E key,
// XySequence sequence) {
//
// if (map.containsKey(key)) {
// map.get(key).add(sequence);
// } else {
// map.put(key, XySequence.copyOf(sequence));
// }
// }
}
}
}
2 changes: 1 addition & 1 deletion src/gov/usgs/earthquake/nshm/www/RateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected void doGet(
HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("application/json; charset=UTF-8");
ServletUtil.setCorsHeadersAndContentType(response);

String query = request.getQueryString();
String pathInfo = request.getPathInfo();
Expand Down
16 changes: 16 additions & 0 deletions src/gov/usgs/earthquake/nshm/www/ServletUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletResponse;

import org.opensha2.calc.ValueFormat;
import org.opensha2.calc.Vs30;
Expand Down Expand Up @@ -119,6 +120,21 @@ public HazardModel call() throws Exception {
}
}

/*
* Because nshmp-haz-ws services may be called by both the USGS website, other
* websites, and directly by 3rd party applications, reponses generated by
* direct requests will not have the necessary header information that would
* be required by security protocols for web requests. This means that any
* initial direct request will pollute intermediate caches with a response
* that a browser will deem invalid.
*/
static void setCorsHeadersAndContentType(HttpServletResponse response) {
response.setContentType("application/json; charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Allow-Headers", "accept,origin,authorization,content-type");
}

private static HazardModel loadModel(ServletContext context, Model model) {
Path path;
URL url;
Expand Down
2 changes: 1 addition & 1 deletion src/gov/usgs/earthquake/nshm/www/SpectraService.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class SpectraService extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("application/json; charset=UTF-8");
ServletUtil.setCorsHeadersAndContentType(response);

String query = request.getQueryString();
String pathInfo = request.getPathInfo();
Expand Down

0 comments on commit 23188ab

Please sign in to comment.