use of org.apache.commons.httpclient.HttpURL in project pinot by linkedin.
the class ChangeTableState method execute.
@Override
public boolean execute() throws Exception {
if (_controllerHost == null) {
_controllerHost = NetUtil.getHostAddress();
}
String stateValue = _state.toLowerCase();
if (!stateValue.equals("enable") && !stateValue.equals("disable") && !stateValue.equals("drop")) {
throw new IllegalArgumentException("Invalid value for state: " + _state + "\n Value must be one of enable|disable|drop");
}
HttpClient httpClient = new HttpClient();
HttpURL url = new HttpURL(_controllerHost, Integer.parseInt(_controllerPort), URI_TABLES_PATH + _tableName);
url.setQuery("state", stateValue);
GetMethod httpGet = new GetMethod(url.getEscapedURI());
int status = httpClient.executeMethod(httpGet);
if (status != 200) {
throw new RuntimeException("Failed to change table state, error: " + httpGet.getResponseBodyAsString());
}
return true;
}
use of org.apache.commons.httpclient.HttpURL in project zm-mailbox by Zimbra.
the class FeedManager method retrieveRemoteData.
private static RemoteDataInfo retrieveRemoteData(String url, Folder.SyncData fsd) throws ServiceException, HttpException, IOException {
assert !Strings.isNullOrEmpty(url);
HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
HttpProxyUtil.configureProxy(client);
// cannot set connection timeout because it'll affect all HttpClients associated with the conn mgr.
// see comments in ZimbraHttpConnectionManager
// client.setConnectionTimeout(10000);
HttpMethodParams params = new HttpMethodParams();
params.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, MimeConstants.P_CHARSET_UTF8);
params.setSoTimeout(60000);
GetMethod get = null;
BufferedInputStream content = null;
long lastModified = 0;
String expectedCharset = MimeConstants.P_CHARSET_UTF8;
int redirects = 0;
int statusCode = HttpServletResponse.SC_NOT_FOUND;
try {
do {
String lcurl = url.toLowerCase();
if (lcurl.startsWith("webcal:")) {
url = "http:" + url.substring(7);
} else if (lcurl.startsWith("feed:")) {
url = "http:" + url.substring(5);
} else if (!lcurl.startsWith("http:") && !lcurl.startsWith("https:")) {
throw ServiceException.INVALID_REQUEST("url must begin with http: or https:", null);
}
// username and password are encoded in the URL as http://user:pass@host/...
if (url.indexOf('@') != -1) {
HttpURL httpurl = lcurl.startsWith("https:") ? new HttpsURL(url) : new HttpURL(url);
if (httpurl.getUser() != null) {
String user = httpurl.getUser();
if (user.indexOf('%') != -1) {
try {
user = URLDecoder.decode(user, "UTF-8");
} catch (OutOfMemoryError e) {
Zimbra.halt("out of memory", e);
} catch (Throwable t) {
}
}
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, httpurl.getPassword());
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, creds);
}
}
try {
get = new GetMethod(url);
} catch (OutOfMemoryError e) {
Zimbra.halt("out of memory", e);
return null;
} catch (Throwable t) {
throw ServiceException.INVALID_REQUEST("invalid url for feed: " + url, t);
}
get.setParams(params);
get.setFollowRedirects(true);
get.setDoAuthentication(true);
get.addRequestHeader("User-Agent", HTTP_USER_AGENT);
get.addRequestHeader("Accept", HTTP_ACCEPT);
if (fsd != null && fsd.getLastSyncDate() > 0) {
String lastSyncAt = org.apache.commons.httpclient.util.DateUtil.formatDate(new Date(fsd.getLastSyncDate()));
get.addRequestHeader("If-Modified-Since", lastSyncAt);
}
HttpClientUtil.executeMethod(client, get);
Header locationHeader = get.getResponseHeader("location");
if (locationHeader != null) {
// update our target URL and loop again to do another HTTP GET
url = locationHeader.getValue();
get.releaseConnection();
} else {
statusCode = get.getStatusCode();
if (statusCode == HttpServletResponse.SC_OK) {
Header contentEncoding = get.getResponseHeader("Content-Encoding");
InputStream respInputStream = get.getResponseBodyAsStream();
if (contentEncoding != null) {
if (contentEncoding.getValue().indexOf("gzip") != -1) {
respInputStream = new GZIPInputStream(respInputStream);
}
}
content = new BufferedInputStream(respInputStream);
expectedCharset = get.getResponseCharSet();
Header lastModHdr = get.getResponseHeader("Last-Modified");
if (lastModHdr == null) {
lastModHdr = get.getResponseHeader("Date");
}
if (lastModHdr != null) {
try {
Date d = org.apache.commons.httpclient.util.DateUtil.parseDate(lastModHdr.getValue());
lastModified = d.getTime();
} catch (DateParseException e) {
ZimbraLog.misc.warn("unable to parse Last-Modified/Date header: " + lastModHdr.getValue(), e);
lastModified = System.currentTimeMillis();
}
} else {
lastModified = System.currentTimeMillis();
}
} else if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
ZimbraLog.misc.debug("Remote data at " + url + " not modified since last sync");
return new RemoteDataInfo(statusCode, redirects, null, expectedCharset, lastModified);
} else {
throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusLine().toString(), null);
}
break;
}
} while (++redirects <= MAX_REDIRECTS);
} catch (ServiceException ex) {
if (get != null) {
get.releaseConnection();
}
throw ex;
} catch (HttpException ex) {
if (get != null) {
get.releaseConnection();
}
throw ex;
} catch (IOException ex) {
if (get != null) {
get.releaseConnection();
}
throw ex;
}
RemoteDataInfo rdi = new RemoteDataInfo(statusCode, redirects, content, expectedCharset, lastModified);
rdi.setGetMethod(get);
return rdi;
}
Aggregations