Java实现OpenLayers跨域代理程序(4)

//step 3: return output
                                //can output be the same for GET/POST? or different return headers?
                                //servlet may return 3 things: status code, response headers, response body
                                // status code and headers have to be set before response body
                                response.setContentType(connection.getContentType());
                                ostream = response.getOutputStream();
                                copy(connectionIstream, ostream);
                        }
                        // if not targetURL send page that targetURL is required param
                } catch (Exception e){
                        response.setStatus(500); //what will user get? default page for response code
                        //WMS/WFS have specific responses to errors
                        //response.getWriter();//will writing custom result help
                        e.printStackTrace();
                } finally {
                        if(istream != null) { istream.close(); }
                        if(ostream != null) { ostream.close(); }
                        if(connectionIstream != null) { connectionIstream.close(); }
                        if(connectionOstream != null) { connectionOstream.close(); }
                }

}

private void copy(InputStream istream, OutputStream ostream) throws Exception {
                int bufferSize = 4*4*1024;//same buffer size as in Jetty utils (2*8192)
                byte[] buffer = new byte[bufferSize];
                int read;
                while ((read = istream.read(buffer)) != -1) {
                        ostream.write(buffer, 0, read);
                }
        }

private void transferHTTPRequestHeaders(HttpURLConnection connection, HttpServletRequest request){
                //TODO make sure all headers are copied to target, see for HTTP headers
                //Do request.getProperties to get request properties
                if(request.getHeader("Accept") != null){
                        connection.setRequestProperty("Accept", request.getHeader("Accept"));
                }
                if(request.getHeader("Accept-Charset") != null){
                        connection.setRequestProperty("Accept-Charset", request.getHeader("Accept-Charset"));
                }
                if(request.getHeader("Accept-Encoding") != null){
                        //TODO browsers accept gzipped, should proxy accept gzip and how to handle it?
                        //connection.setRequestProperty("Accept-Encoding", request.getHeader("Accept-Encoding"));
                }
                if(request.getHeader("Authorization") != null){
                        connection.setRequestProperty("Authorization", request.getHeader("Authorization"));
                }
                if(request.getHeader("Connection") != null){
                        //TODO HTTP/1.1 proxies MUST parse the Connection header field before a message is forwarded and, for each connection-token in this field, remove any header field(s) from the message with the same name as the connection-token.
                        //connection.setRequestProperty("Connection", request.getHeader("Connection"));
                }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c5c9f3449647f0e7e9590e9bffae1383.html