File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\cli\src\main\java\hudson\cli\FullDuplexHttpStream.java.html

1 package hudson.cli;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.OutputStream;
8 import java.net.HttpURLConnection;
9 import java.net.URL;
10 import java.util.UUID;
11 import java.util.logging.Level;
12 import java.util.logging.Logger;
13
14 import org.apache.commons.codec.binary.Base64;
15
16 /**
17 * Creates a capacity-unlimited bi-directional {@link InputStream}/{@link OutputStream} pair over
18 * HTTP, which is a request/response protocol.
19 *
20 * @author Kohsuke Kawaguchi
21 */
22 public class FullDuplexHttpStream {
23 private final URL target;
24 /**
25 * Authorization header value needed to get through the HTTP layer.
26 */
27 private final String authorization;
28
29 private final OutputStream output;
30 private final InputStream input;
31
32 public InputStream getInputStream() {
33 return input;
34 }
35
36 public OutputStream getOutputStream() {
37 return output;
38 }
39
40 @Deprecated
41 public FullDuplexHttpStream(URL target) throws IOException {
42 this(target,basicAuth(target.getUserInfo()));
43 }
44
45 private static String basicAuth(String userInfo) {
46 if (userInfo != null)
47 return "Basic "+new String(new Base64().encodeBase64(userInfo.getBytes()));
48 return null;
49 }
50
51 /**
52 * @param target
53 * The endpoint that we are making requests to.
54 * @param authorization
55 * The value of the authorization header, if non-null.
56 */
57 public FullDuplexHttpStream(URL target, String authorization) throws IOException {
58 this.target = target;
59 this.authorization = authorization;
60
61 CrumbData crumbData = new CrumbData();
62
63 UUID uuid = UUID.randomUUID(); // so that the server can correlate those two connections
64
65 // server->client
66 HttpURLConnection con = (HttpURLConnection) target.openConnection();
67 con.setDoOutput(true); // request POST to avoid caching
68 con.setRequestMethod("POST");
69 con.addRequestProperty("Session", uuid.toString());
70 con.addRequestProperty("Side","download");
71 if (authorization != null) {
72 con.addRequestProperty("Authorization", authorization);
73 }
74 if(crumbData.isValid) {
75 con.addRequestProperty(crumbData.crumbName, crumbData.crumb);
76 }
77 con.getOutputStream().close();
78 input = con.getInputStream();
79 // make sure we hit the right URL
80 if(con.getHeaderField("Hudson-Duplex")==null)
81 throw new IOException(target+" doesn't look like Jenkins");
82
83 // client->server uses chunked encoded POST for unlimited capacity.
84 con = (HttpURLConnection) target.openConnection();
85 con.setDoOutput(true); // request POST
86 con.setRequestMethod("POST");
87 con.setChunkedStreamingMode(0);
88 con.setRequestProperty("Content-type","application/octet-stream");
89 con.addRequestProperty("Session", uuid.toString());
90 con.addRequestProperty("Side","upload");
91 if (authorization != null) {
92 con.addRequestProperty ("Authorization", authorization);
93 }
94
95 if(crumbData.isValid) {
96 con.addRequestProperty(crumbData.crumbName, crumbData.crumb);
97 }
98 output = con.getOutputStream();
99 }
100
101 static final int BLOCK_SIZE = 1024;
102 static final Logger LOGGER = Logger.getLogger(FullDuplexHttpStream.class.getName());
103
104 private final class CrumbData {
105 String crumbName;
106 String crumb;
107 boolean isValid;
108
109 private CrumbData() {
110 this.crumbName = "";
111 this.crumb = "";
112 this.isValid = false;
113 getData();
114 }
115
116 private void getData() {
117 try {
118 String base = createCrumbUrlBase();
119 String[] pair = readData(base + "?xpath=concat(//crumbRequestField,\":\",//crumb)").split(":", 2);
120 crumbName = pair[0];
121 crumb = pair[1];
122 isValid = true;
123 LOGGER.fine("Crumb data: "+crumbName+"="+crumb);
124 } catch (IOException e) {
125 // presumably this Hudson doesn't use crumb
126 LOGGER.log(Level.FINE,"Failed to get crumb data",e);
127 }
128 }
129
130 private String createCrumbUrlBase() {
131 String url = target.toExternalForm();
132 return new StringBuilder(url.substring(0, url.lastIndexOf("/cli"))).append("/crumbIssuer/api/xml/").toString();
133 }
134
135 private String readData(String dest) throws IOException {
136 HttpURLConnection con = (HttpURLConnection) new URL(dest).openConnection();
137 if (authorization != null) {
138 con.addRequestProperty("Authorization", authorization);
139 }
140 try {
141 BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
142 String line = reader.readLine();
143 String nextLine = reader.readLine();
144 if (nextLine != null) {
145 System.err.println("Warning: received junk from " + dest);
146 System.err.println(line);
147 System.err.println(nextLine);
148 while ((nextLine = reader.readLine()) != null) {
149 System.err.println(nextLine);
150 }
151 }
152 return line;
153 }
154 finally {
155 con.disconnect();
156 }
157 }
158 }
159 }
160