Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hudson.cli;
  
  import java.net.URL;
 import java.util.UUID;
 
Creates a capacity-unlimited bi-directional java.io.InputStream/java.io.OutputStream pair over HTTP, which is a request/response protocol.

Author(s):
Kohsuke Kawaguchi
 
 public class FullDuplexHttpStream {
     private final URL target;
 
     private final OutputStream output;
     private final InputStream input;
 
     public InputStream getInputStream() {
         return ;
     }
 
     public OutputStream getOutputStream() {
         return ;
     }
 
     public FullDuplexHttpStream(URL targetthrows IOException {
         this. = target;
 
         String authorization = null;
         if (target.getUserInfo() != null) {
         	authorization = new BASE64Encoder().encode(target.getUserInfo().getBytes());
         }
 
         CrumbData crumbData = new CrumbData();
 
         UUID uuid = UUID.randomUUID(); // so that the server can correlate those two connections
 
         // server->client
         HttpURLConnection con = (HttpURLConnectiontarget.openConnection();
         con.setDoOutput(true); // request POST to avoid caching
         con.setRequestMethod("POST");
         con.addRequestProperty("Session"uuid.toString());
         con.addRequestProperty("Side","download");
         if (authorization != null) {
             con.addRequestProperty("Authorization""Basic " + authorization);
         }
         if(crumbData.isValid) {
             con.addRequestProperty(crumbData.crumbNamecrumbData.crumb);
         }
         con.getOutputStream().close();
          = con.getInputStream();
         // make sure we hit the right URL
         if(con.getHeaderField("Hudson-Duplex")==null)
             throw new IOException(target+" doesn't look like Hudson");
 
         // client->server uses chunked encoded POST for unlimited capacity. 
         con = (HttpURLConnectiontarget.openConnection();
         con.setDoOutput(true); // request POST
         con.setRequestMethod("POST");
         con.setChunkedStreamingMode(0);
         con.setRequestProperty("Content-type","application/octet-stream");
         con.addRequestProperty("Session"uuid.toString());
         con.addRequestProperty("Side","upload");
         if (authorization != null) {
         	con.addRequestProperty ("Authorization""Basic " + authorization);
         }
 
         if(crumbData.isValid) {
             con.addRequestProperty(crumbData.crumbNamecrumbData.crumb);
         }
          = con.getOutputStream();
     }
 
     static final int BLOCK_SIZE = 1024;
     static final Logger LOGGER = Logger.getLogger(FullDuplexHttpStream.class.getName());
     
     private final class CrumbData {
     	String crumbName;
     	String crumb;
     	boolean isValid;
 
     	private CrumbData() {
             this. = "";
             this. = "";
             this. = false;
             getData();
     	}
 
     	private void getData() {
            try {
                String base = createCrumbUrlBase();
                 = readData(base+"?xpath=/*/crumbRequestField/text()");
                 = readData(base+"?xpath=/*/crumb/text()");
                 = true;
                .fine("Crumb data: "++"="+);
            } catch (IOException e) {
                // presumably this Hudson doesn't use crumb 
                .log(.,"Failed to get crumb data",e);
            }
    	}
    	private String createCrumbUrlBase() {
            String url = .toExternalForm();    		
            return new StringBuilder(url.substring(0, url.lastIndexOf("/cli"))).append("/crumbIssuer/api/xml/").toString();
    	}
    	private String readData(String destthrows IOException {
            HttpURLConnection con = (HttpURLConnectionnew URL(dest).openConnection();
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                return reader.readLine();
            }
            finally {
                con.disconnect();
            }
        }
    }
New to GrepCode? Check out our FAQ X