conn.setRequestProperty(header.getKey(), header.getValue());
}
}
// authenticate
AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(session);
if (authProvider != null) {
Map<String, List<String>> httpHeaders = authProvider.getHTTPHeaders(url.toString());
if (httpHeaders != null) {
for (Map.Entry<String, List<String>> header : httpHeaders.entrySet()) {
if (header.getValue() != null) {
for (String value : header.getValue()) {
conn.setRequestProperty(header.getKey(), value);
}
}
}
}
}
// range
if ((offset != null) || (length != null)) {
StringBuilder sb = new StringBuilder("bytes=");
if ((offset == null) || (offset.signum() == -1)) {
offset = BigInteger.ZERO;
}
sb.append(offset.toString());
sb.append("-");
if ((length != null) && (length.signum() == 1)) {
sb.append(offset.add(length.subtract(BigInteger.ONE)).toString());
}
conn.setRequestProperty("Range", sb.toString());
}
// compression
Object compression = session.get(SessionParameter.COMPRESSION);
if ((compression != null) && Boolean.parseBoolean(compression.toString())) {
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
}
// locale
if (session.get(CmisBindingsHelper.ACCEPT_LANGUAGE) instanceof String) {
conn.setRequestProperty("Accept-Language", session.get(CmisBindingsHelper.ACCEPT_LANGUAGE).toString());
}
// send data
if (writer != null) {
conn.setChunkedStreamingMode(64 * 1024);
OutputStream connOut = null;
Object clientCompression = session.get(SessionParameter.CLIENT_COMPRESSION);
if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
conn.setRequestProperty("Content-Encoding", "gzip");
connOut = new GZIPOutputStream(conn.getOutputStream(), 4096);
} else {
connOut = conn.getOutputStream();
}
OutputStream out = new BufferedOutputStream(connOut, BUFFER_SIZE);
writer.write(out);
out.flush();
}
// connect
conn.connect();
// get stream, if present
int respCode = conn.getResponseCode();
InputStream inputStream = null;
if ((respCode == 200) || (respCode == 201) || (respCode == 203) || (respCode == 206)) {
inputStream = conn.getInputStream();
}
// forward response HTTP headers
if (authProvider != null) {
authProvider.putResponseHeaders(url.toString(), respCode, conn.getHeaderFields());
}
// get the response
return new Response(respCode, conn.getResponseMessage(), conn.getHeaderFields(), inputStream,
conn.getErrorStream());