Skip to content

Commit b70499d

Browse files
committed
Fix JSON responses
1 parent 84726f8 commit b70499d

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/api/ProxyRouteController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import eu.openanalytics.containerproxy.model.runtime.Proxy;
2424
import eu.openanalytics.containerproxy.service.ProxyService;
2525
import eu.openanalytics.containerproxy.service.UserService;
26+
import eu.openanalytics.containerproxy.util.ImmediateJsonResponse;
2627
import eu.openanalytics.containerproxy.util.ProxyMappingManager;
2728
import eu.openanalytics.containerproxy.util.ContextPathHelper;
2829
import org.springframework.stereotype.Controller;

src/main/java/eu/openanalytics/containerproxy/auth/impl/oidc/OpenIdReAuthorizeFilter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
package eu.openanalytics.containerproxy.auth.impl.oidc;
2222

23+
import eu.openanalytics.containerproxy.util.ImmediateJsonResponse;
2324
import org.springframework.core.env.Environment;
2425
import org.springframework.security.core.Authentication;
2526
import org.springframework.security.core.context.SecurityContextHolder;
@@ -132,8 +133,7 @@ protected void doFilterInternal(@Nonnull HttpServletRequest request, @Nonnull Ht
132133
}
133134
}
134135
if (REFRESH_OPENID_MATCHER.matches(request)) {
135-
response.getWriter().write("{\"status\":\"success\"}");
136-
response.setStatus(200);
136+
ImmediateJsonResponse.write(response, 200, "{\"status\":\"success\"}");
137137
return;
138138
}
139139
chain.doFilter(request, response);
@@ -156,8 +156,7 @@ private void invalidateSession(@Nonnull HttpServletRequest request, @Nonnull Htt
156156
session.invalidate();
157157
}
158158
if (REFRESH_OPENID_MATCHER.matches(request)) {
159-
response.getWriter().write("{\"status\":\"success\"}");
160-
response.setStatus(200);
159+
ImmediateJsonResponse.write(response, 200, "{\"status\":\"success\"}");
161160
} else {
162161
throw new ClientAuthorizationRequiredException(REG_ID);
163162
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2023 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.util;
22+
23+
import javax.servlet.http.HttpServletResponse;
24+
import java.io.IOException;
25+
26+
public class ImmediateJsonResponse {
27+
28+
/**
29+
* Helper that writes a JSON response to a HttpServletResponse without using Spring.
30+
* This can be used in places where no Spring logic can be used (e.g. Filters)
31+
* @param response the response to write into
32+
* @param status the status code of the response
33+
* @param json the jsons string to write
34+
* @throws IOException
35+
*/
36+
public static void write(HttpServletResponse response, int status, String json) throws IOException {
37+
response.setCharacterEncoding("UTF-8");
38+
response.setContentType("application/json");
39+
response.getWriter().write(json);
40+
response.setStatus(status);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)