[ 
https://issues.apache.org/jira/browse/SQOOP-1795?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14227661#comment-14227661
 ] 

Veena Basavaraj commented on SQOOP-1795:
----------------------------------------

[~stanleyxu2005] I have not closely looked at the RB, would you mind to 
summarize what your final proposal will be here?


Also, 
Some good discussions here and example code! This is the most safest since we 
can some day even have filter high up in the chain that just does getReader. If 
we want to support multi reads.

http://stackoverflow.com/questions/1046721/accessing-the-raw-body-of-a-put-or-post-request
{code}
ackage grails.util.http;

import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import java.io.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class MultiReadHttpServletRequest extends HttpServletRequestWrapper {

    private byte[] body;

    public MultiReadHttpServletRequest(HttpServletRequest httpServletRequest) {
        super(httpServletRequest);
        // Read the request body and save it as a byte array
        InputStream is = super.getInputStream();
        body = IOUtils.toByteArray(is);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        return new ServletInputStreamImpl(new ByteArrayInputStream(body));
    }

    @Override
    public BufferedReader getReader() throws IOException {
        String enc = getCharacterEncoding();
        if(enc == null) enc = "UTF-8";
        return new BufferedReader(new InputStreamReader(getInputStream(), enc));
    }

    private class ServletInputStreamImpl extends ServletInputStream {

        private InputStream is;

        public ServletInputStreamImpl(InputStream is) {
            this.is = is;
        }

        public int read() throws IOException {
            return is.read();
        }

        public boolean markSupported() {
            return false;
        }

        public synchronized void mark(int i) {
            throw new RuntimeException(new IOException("mark/reset not 
supported"));
        }

        public synchronized void reset() throws IOException {
            throw new IOException("mark/reset not supported");
        }
    }

}
{code}


> Sqoop2: Retrieve Http post data in plausible manner
> ---------------------------------------------------
>
>                 Key: SQOOP-1795
>                 URL: https://issues.apache.org/jira/browse/SQOOP-1795
>             Project: Sqoop
>          Issue Type: Sub-task
>            Reporter: Qian Xu
>            Assignee: Qian Xu
>             Fix For: 1.99.5
>
>
> Sqoop client posts parameters as JSON to server. As it is not query string 
> based pattern, {{HttpServletRequest}} is not able to return the JSON using 
> {{getParameterValue(...)}}. The current solution is to call {{getReader()}} 
> to get raw post data. There is a danger, if the method is NOT called at the 
> first place. You do not know, whether the reading position is at the 
> beginning. You might get unexpected result without notice. SQOOP-1784 is a 
> unfortunate case.
> {code}
> // 2 line unlucky code
> String username = ctx.getUserName(); // The method will change reading 
> position of reader internally
> JSONObject postData = (JSONObject) 
> JSONValue.parse(ctx.getRequest().getReader()); // No contents read
> // 2 line lucky code
> JSONObject postData = (JSONObject) 
> JSONValue.parse(ctx.getRequest().getReader()); // Good
> String username = ctx.getUserName(); // Good
> {code}
> In practice, it'd suggest to use query string base pattern, i.e. 
> jsonObject=JSON. The server can call {{ctx.getParameterValue("jsonObject")}} 
> to get the value without any problem. But we need to change the api.
> Now it is perfectly clear:
> 1. For query string based pattern, caller should always use 
> {{getParameterValue(...)}}.
> 2. For raw post data usage, the post data is parsed as the first parameter's 
> key. So I'd suggest to add {{RequestContext.getFirstParameterName()}}. so 
> that we can keep fingers away from {{getReader()}}. 



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to