Skip to content

Commit

Permalink
add cors to version mismatch (#3504)
Browse files Browse the repository at this point in the history
* add cors to version mismatch

* use library for headers
  • Loading branch information
jrhizor committed May 21, 2021
1 parent 21ba32c commit 86d6640
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
19 changes: 11 additions & 8 deletions airbyte-server/src/main/java/io/airbyte/server/CorsFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@

package io.airbyte.server;

import com.google.common.collect.ImmutableMap;
import com.google.common.net.HttpHeaders;
import java.util.Map;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;

// https://medium.com/@Leejjon_net/how-to-allow-cross-origin-requests-in-a-jax-rs-microservice-d2a6aa2df484
public class CorsFilter implements ContainerResponseFilter {

private static final String ALLOW_ORIGIN = "Access-Control-Allow-Origin";
private static final String ALLOW_HEADERS = "Access-Control-Allow-Headers";
private static final String ALLOW_METHODS = "Access-Control-Allow-Methods";
public static final ImmutableMap<String, String> MAP = ImmutableMap.of(
HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*",
HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "Origin, Content-Type, Accept, Content-Encoding",
HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS, HEAD");

@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) {
responseContext.getHeaders().add(ALLOW_ORIGIN, "*");
responseContext.getHeaders().add(ALLOW_HEADERS, "Origin, Content-Type, Accept, Content-Encoding");
responseContext.getHeaders().add(ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS, HEAD");
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
for (Map.Entry<String, String> entry : MAP.entrySet()) {
responseContext.getHeaders().add(entry.getKey(), entry.getValue());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import com.google.common.collect.ImmutableMap;
import io.airbyte.commons.json.Jsons;
import io.airbyte.commons.version.AirbyteVersion;
import io.airbyte.server.CorsFilter;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -81,6 +83,10 @@ public static class VersionMismatchServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
var outputMap = ImmutableMap.of("error", ERROR_MESSAGE);

for (Map.Entry<String, String> entry : CorsFilter.MAP.entrySet()) {
response.setHeader(entry.getKey(), entry.getValue());
}

response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().println(Jsons.serialize(outputMap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.google.common.net.HttpHeaders;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.URI;
Expand Down Expand Up @@ -76,6 +77,10 @@ public void testIt(String relativePath) throws Exception {
http.connect();

assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, http.getResponseCode());

assertEquals(http.getHeaderField(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN), "*");
assertEquals(http.getHeaderField(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS), "Origin, Content-Type, Accept, Content-Encoding");
assertEquals(http.getHeaderField(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS), "GET, POST, PUT, DELETE, OPTIONS, HEAD");
}

}

0 comments on commit 86d6640

Please sign in to comment.