1+ package com .continiousdisappointment .apigw .config ;
2+
3+ import org .springframework .core .env .Environment ;
4+ import org .springframework .http .*;
5+ import org .springframework .util .LinkedMultiValueMap ;
6+ import org .springframework .util .MultiValueMap ;
7+ import org .springframework .web .bind .annotation .*;
8+ import org .springframework .web .client .RestTemplate ;
9+ import org .springframework .web .multipart .MultipartHttpServletRequest ;
10+
11+ import lombok .RequiredArgsConstructor ;
12+
13+ import jakarta .servlet .http .HttpServletRequest ;
14+ import java .util .Arrays ;
15+
16+ @ RestController
17+ @ RequiredArgsConstructor
18+ public class FileUploadProxyController {
19+
20+ private final Environment environment ;
21+ private final RestTemplate restTemplate ;
22+
23+ @ PostMapping ("/genai/**" )
24+ public ResponseEntity <?> proxyMultipartRequest (
25+ HttpServletRequest request ,
26+ @ RequestHeader HttpHeaders headers ) {
27+
28+ try {
29+ // Extract the remaining path after /genai
30+ String requestPath = request .getRequestURI ();
31+ String targetPath = requestPath ; // Keep the full path including /genai
32+
33+ // Build target URL
34+ String genaiServiceUrl = getGenAiServiceUrl ();
35+ String targetUrl = genaiServiceUrl + targetPath ;
36+
37+ // Prepare headers for forwarding (exclude host and content-length)
38+ HttpHeaders forwardHeaders = new HttpHeaders ();
39+ headers .forEach ((name , values ) -> {
40+ String lowerName = name .toLowerCase ();
41+ if (!lowerName .equals ("host" ) && !lowerName .equals ("content-length" )) {
42+ forwardHeaders .put (name , values );
43+ }
44+ });
45+
46+ // Check if this is a multipart request
47+ if (request instanceof MultipartHttpServletRequest multipartRequest ) {
48+ // Handle multipart request
49+ MultiValueMap <String , Object > body = new LinkedMultiValueMap <>();
50+
51+ // Add all multipart files
52+ multipartRequest .getFileMap ().forEach ((name , file ) -> {
53+ body .add (name , file .getResource ());
54+ });
55+
56+ // Add all form parameters
57+ multipartRequest .getParameterMap ().forEach ((name , values ) -> {
58+ for (String value : values ) {
59+ body .add (name , value );
60+ }
61+ });
62+
63+ forwardHeaders .setContentType (MediaType .MULTIPART_FORM_DATA );
64+
65+ HttpEntity <MultiValueMap <String , Object >> entity = new HttpEntity <>(body , forwardHeaders );
66+ return restTemplate .postForEntity (targetUrl , entity , Object .class );
67+ } else {
68+ // Handle non-multipart request
69+ HttpEntity <Object > entity = new HttpEntity <>(forwardHeaders );
70+ return restTemplate .postForEntity (targetUrl , entity , Object .class );
71+ }
72+
73+ } catch (Exception e ) {
74+ return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR )
75+ .body ("Proxy error: " + e .getMessage ());
76+ }
77+ }
78+
79+ private String getGenAiServiceUrl () {
80+ if (Arrays .asList (environment .getActiveProfiles ()).contains ("dev" )) {
81+ return "http://localhost:8000" ;
82+ }
83+ return "http://genai-service:8000" ;
84+ }
85+ }
0 commit comments