Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gateway issue with image upload. #3343

Open
manojsinghsaun opened this issue Apr 9, 2024 · 4 comments
Open

Gateway issue with image upload. #3343

manojsinghsaun opened this issue Apr 9, 2024 · 4 comments

Comments

@manojsinghsaun
Copy link

manojsinghsaun commented Apr 9, 2024

I have two services gateway and myfunction services.
I am uploading image using angular js.
But when request routing to myfunction services the Image file get removed from request.
I have used customer filter to in both gateway and myfunction sevrices to check if the request content image file or not.



import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;

@Component
public class CustomFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
        Object parameterValue = request.getParameter("file");
        		
        		if(contentType!=null && request.getContentType().startsWith(MediaType.MULTIPART_FORM_DATA_VALUE)) {
                    Collection<Part> parts = request.getParts();
                    for (Part part : parts) {
                        // Check if the part is a file or a form field
                        if (part.getContentType() != null) {
                            // File part
                            System.out.println("File part name: " + part.getName());
                            System.out.println("File name: " + part.getSubmittedFileName());
                            // You can process the file here
                        } else {
                            // Form field part
                            System.out.println("Form field name: " + part.getName());
                            // You can process the form field here
                        }
                    }
        		}
                    
                

        

        // Check if the request contains a file
        boolean hasFile = request.getContentType() != null && request.getContentType().startsWith(MediaType.MULTIPART_FORM_DATA_VALUE);
        

      Map<String, String[]> parameterMap = request.getParameterMap();
        for (String paramName : parameterMap.keySet()) {
            Object paramValues = parameterMap.get(paramName);
            System.out.println("manoj" +paramName);
           // for (String paramValue : paramValues) {
           //     System.out.println("Parameter: " + paramName + ", Value: " + paramValue);
            }


        // Perform your validations based on file presence, content type, and parameter value
       // if (hasFile && contentType != null && contentType.equals(MediaType.APPLICATION_JSON_VALUE) && parameterValue != null) {
            // Proceed with the request
            filterChain.doFilter(request, response);
       // } else {
            // Reject the request with a 400 Bad Request response
        //    response.setStatus(HttpStatus.BAD_REQUEST.value());
          //  response.getWriter().write("Invalid request parameters");
          //  return;
       // }
    }
}

I can see the file when request is in gateway but it get missed when it come to myfunction services.

I am using oauth2 client in gateway and ouath2 resources server in myfunction services.

please can you tell me what is worng with the gateway.

one more point to hightlight When I use postman It works fine the file did lost.

@manojsinghsaun
Copy link
Author

I have also check this link
but here tere haven't touch the part file which is coming with my request.
which filter do that.

@spencergibb
Copy link
Member

reading the parts reads the stream and it is, therefor, not available to send anymore.

@manojsinghsaun
Copy link
Author

Can you explain in detail..I did not get what you are explaing

@joel16
Copy link

joel16 commented May 10, 2024

Can you explain in detail..I did not get what you are explaing

I might be misunderstanding but it sounds like what Spencer is saying is that you're already reading the stream in your filter. Once the stream has been read it's not being sent to your controller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants