proxy 삭제

This commit is contained in:
현성필
2025-11-21 17:41:48 +09:00
parent 82d1bb1eb4
commit eddbefd698
2 changed files with 0 additions and 169 deletions
@@ -1,146 +0,0 @@
package com.eactive.apim.portal.apps.proxy;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
@RestController
@RequestMapping("/_proxy") // /proxy 대신 /_proxy 사용
@Slf4j
public class ApimProxyController {
private final RestTemplate restTemplate;
private final ProxyProperties proxyProperties;
private final ObjectMapper objectMapper;
private final MediaType JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8);
public ApimProxyController(
RestTemplateBuilder restTemplateBuilder,
ProxyProperties proxyProperties,
ObjectMapper objectMapper) {
this.proxyProperties = proxyProperties;
this.objectMapper = objectMapper;
this.restTemplate = restTemplateBuilder
.setConnectTimeout(Duration.ofMillis(proxyProperties.getTimeout().getConnect()))
.setReadTimeout(Duration.ofMillis(proxyProperties.getTimeout().getRead()))
.build();
}
private ResponseEntity<String> createJsonResponse(HttpStatus status, String message) {
ObjectNode jsonResponse = objectMapper.createObjectNode()
.put("status", status.value())
.put("message", message);
return ResponseEntity
.status(status)
.contentType(JSON_UTF8)
.body(jsonResponse.toString());
}
@RequestMapping(value = "/**")
public ResponseEntity<String> proxyRequest(
HttpServletRequest request,
@RequestBody(required = false) String body,
@RequestHeader HttpHeaders headers) {
String targetKey = headers.getFirst("target_host");
if (targetKey == null || targetKey.trim().isEmpty()) {
log.error("Missing or empty target_host header");
return createJsonResponse(HttpStatus.BAD_REQUEST, "Error: target_host header is required");
}
String targetUrl = proxyProperties.getTargets().get(targetKey.trim().toLowerCase());
if (targetUrl == null) {
log.error("Invalid target_host: {}. Available targets: {}",
targetKey, proxyProperties.getTargets().keySet());
return createJsonResponse(HttpStatus.BAD_REQUEST,
"Error: Invalid target_host. Must be one of: " +
String.join(", ", proxyProperties.getTargets().keySet()));
}
try {
String requestUrl = request.getRequestURI().replace("/_proxy", "");
URI uri = UriComponentsBuilder
.fromHttpUrl(targetUrl + requestUrl)
.query(request.getQueryString())
.build()
.toUri();
HttpMethod method = HttpMethod.valueOf(request.getMethod());
HttpHeaders proxyHeaders = new HttpHeaders();
headers.forEach((key, value) -> {
if (!key.equalsIgnoreCase("host") &&
!key.equalsIgnoreCase("target_host")) {
proxyHeaders.addAll(key, value);
}
});
HttpEntity<String> httpEntity = new HttpEntity<>(body, proxyHeaders);
log.debug("Forwarding request to: {} ({})", targetKey, uri);
ResponseEntity<String> response = restTemplate.exchange(
uri,
method,
httpEntity,
String.class
);
log.debug("Received response status: {}", response.getStatusCode());
// Create JSON response with the original response data
ObjectNode jsonResponse = objectMapper.createObjectNode()
.put("status", response.getStatusCode().value());
// Try to parse the response body as JSON, if it fails, include it as a string
try {
jsonResponse.set("data", objectMapper.readTree(response.getBody()));
} catch (Exception e) {
jsonResponse.put("data", response.getBody());
}
return ResponseEntity
.status(response.getStatusCode())
.contentType(JSON_UTF8)
.body(jsonResponse.toString());
} catch (HttpStatusCodeException e) {
log.error("Target server returned error: {} for host: {}", e.getStatusCode(), targetKey);
ObjectNode jsonResponse = objectMapper.createObjectNode()
.put("status", e.getStatusCode().value());
// Try to parse the error response body as JSON, if it fails, include it as a string
try {
jsonResponse.set("data", objectMapper.readTree(e.getResponseBodyAsString()));
} catch (Exception ex) {
jsonResponse.put("data", e.getResponseBodyAsString());
}
return ResponseEntity
.status(e.getStatusCode())
.contentType(JSON_UTF8)
.body(jsonResponse.toString());
} catch (Exception e) {
log.error("Error during proxy request to: {}", targetKey, e);
return createJsonResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
}
@@ -1,23 +0,0 @@
package com.eactive.apim.portal.apps.proxy;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "proxy")
@Getter
@Setter
public class ProxyProperties {
private Map<String, String> targets;
private Timeout timeout = new Timeout();
@Getter
@Setter
public static class Timeout {
private int connect = 5000;
private int read = 5000;
}
}