use of org.apache.commons.httpclient.HttpException 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;
}
use of org.apache.commons.httpclient.HttpException in project zm-mailbox by Zimbra.
the class TestAutoProvision method preauthServlet.
/*
* Note: need to restart server each time before re-run. Otherwise server would still
* have the account created in previous run cached.
*/
@Test
public void preauthServlet() throws Exception {
String testName = getTestName();
String externalPassword = "test456";
String extAcctLocalPart = testName;
String extAcctName = createExternalAcctEntry(extAcctLocalPart, externalPassword, null);
Map<String, Object> zimbraDomainAttrs = commonZimbraDomainAttrs();
// setup auto prov
// commonZimbraDomainAttrs added only LDAP, add preauth here
StringUtil.addToMultiMap(zimbraDomainAttrs, Provisioning.A_zimbraAutoProvAuthMech, AutoProvAuthMech.PREAUTH.name());
zimbraDomainAttrs.put(Provisioning.A_zimbraAutoProvLdapSearchBase, extDomainDn);
zimbraDomainAttrs.put(Provisioning.A_zimbraAutoProvLdapSearchFilter, "(uid=%u)");
// setup external LDAP auth
zimbraDomainAttrs.put(Provisioning.A_zimbraAuthMech, AuthMech.ldap.name());
zimbraDomainAttrs.put(Provisioning.A_zimbraAuthLdapURL, "ldap://localhost:389");
zimbraDomainAttrs.put(Provisioning.A_zimbraAuthLdapBindDn, "uid=%u,ou=people," + extDomainDn);
// setup preauth
String preAuthKey = PreAuthKey.generateRandomPreAuthKey();
zimbraDomainAttrs.put(Provisioning.A_zimbraPreAuthKey, preAuthKey);
Domain zimbraDomain = createZimbraDomain(testName, zimbraDomainAttrs);
String loginName = extAcctLocalPart + "@" + zimbraDomain.getName();
// preauth data
String preAuthUrl = TestPreAuthServlet.genPreAuthUrl(preAuthKey, loginName, false, false);
// do the preauth servlet request
String url = TestUtil.getBaseUrl() + preAuthUrl;
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
boolean ok = false;
try {
int respCode = HttpClientUtil.executeMethod(client, method);
int statusCode = method.getStatusCode();
String statusLine = method.getStatusLine().toString();
ok = (respCode == 200);
/*
System.out.println("respCode=" + respCode);
System.out.println("statusCode=" + statusCode);
System.out.println("statusLine=" + statusLine);
System.out.println("Headers");
Header[] respHeaders = method.getResponseHeaders();
for (int i=0; i < respHeaders.length; i++) {
String header = respHeaders[i].toString();
System.out.println(header);
}
*/
/*
String respBody = method.getResponseBodyAsString();
System.out.println(respBody);
*/
} catch (HttpException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
method.releaseConnection();
}
assertTrue(ok);
/*
String encodedAuthToken = response.getElement(AccountConstants.E_AUTH_TOKEN).getText();
assertNotNull(encodedAuthToken);
AuthToken authToken = AuthToken.getAuthToken(encodedAuthToken);
String acctId = authToken.getAccountId();
Account acct = prov.get(AccountBy.id, acctId);
verifyAcctAutoProvisioned(acct, loginName.toLowerCase());
*/
}
use of org.apache.commons.httpclient.HttpException in project zm-mailbox by Zimbra.
the class ElasticSearchIndex method refreshIndexIfNecessary.
/**
* By default, ElasticSearch refreshes every second (configurable?). Can force it using this.
* TODO: Perhaps only do this if we've written something in the last second?
*/
private boolean refreshIndexIfNecessary() {
String url = String.format("%s_refresh", indexUrl);
GetMethod method = new GetMethod(ElasticSearchConnector.actualUrl(url));
try {
ElasticSearchConnector connector = new ElasticSearchConnector();
int statusCode = connector.executeMethod(method);
if (statusCode == HttpStatus.SC_OK) {
return true;
} else if (statusCode == HttpStatus.SC_NOT_FOUND) {
ZimbraLog.index.debug("Index not present on %s %d", url, statusCode);
return false;
}
ZimbraLog.index.error("Problem refreshing index %s %d", url, statusCode);
} catch (HttpException e) {
ZimbraLog.index.error("Problem refreshing index %s", url, e);
} catch (IOException e) {
ZimbraLog.index.error("Problem refreshing index %s", url, e);
}
return false;
}
use of org.apache.commons.httpclient.HttpException in project zm-mailbox by Zimbra.
the class UserServlet method doHttpOp.
private static Pair<Header[], HttpMethod> doHttpOp(ZAuthToken authToken, HttpMethod method) throws ServiceException {
// create an HTTP client with the same cookies
String url = "";
String hostname = "";
try {
url = method.getURI().toString();
hostname = method.getURI().getHost();
} catch (IOException e) {
log.warn("can't parse target URI", e);
}
HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
Map<String, String> cookieMap = authToken.cookieMap(false);
if (cookieMap != null) {
HttpState state = new HttpState();
for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
state.addCookie(new org.apache.commons.httpclient.Cookie(hostname, ck.getKey(), ck.getValue(), "/", null, false));
}
client.setState(state);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
}
if (method instanceof PutMethod) {
long contentLength = ((PutMethod) method).getRequestEntity().getContentLength();
if (contentLength > 0) {
// 100kbps in millis
int timeEstimate = Math.max(10000, (int) (contentLength / 100));
// cannot set connection time using our ZimbrahttpConnectionManager,
// see comments in ZimbrahttpConnectionManager.
// actually, length of the content to Put should not be a factor for
// establishing a connection, only read time out matter, which we set
// client.getHttpConnectionManager().getParams().setConnectionTimeout(timeEstimate);
method.getParams().setSoTimeout(timeEstimate);
}
}
try {
int statusCode = HttpClientUtil.executeMethod(client, method);
if (statusCode == HttpStatus.SC_NOT_FOUND || statusCode == HttpStatus.SC_FORBIDDEN)
throw MailServiceException.NO_SUCH_ITEM(-1);
else if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_NO_CONTENT)
throw ServiceException.RESOURCE_UNREACHABLE(method.getStatusText(), null, new ServiceException.InternalArgument(HTTP_URL, url, ServiceException.Argument.Type.STR), new ServiceException.InternalArgument(HTTP_STATUS_CODE, statusCode, ServiceException.Argument.Type.NUM));
List<Header> headers = new ArrayList<Header>(Arrays.asList(method.getResponseHeaders()));
headers.add(new Header("X-Zimbra-Http-Status", "" + statusCode));
return new Pair<Header[], HttpMethod>(headers.toArray(new Header[0]), method);
} catch (HttpException e) {
throw ServiceException.RESOURCE_UNREACHABLE("HttpException while fetching " + url, e);
} catch (IOException e) {
throw ServiceException.RESOURCE_UNREACHABLE("IOException while fetching " + url, e);
}
}
use of org.apache.commons.httpclient.HttpException in project zm-mailbox by Zimbra.
the class HostedAuth method authenticate.
/**
* zmprov md test.com zimbraAuthMech 'custom:hosted http://auth.customer.com:80'
*
* This custom auth module takes arguments in the following form:
* {URL} [GET|POST - default is GET] [encryption method - defautl is plain] [auth protocol - default is imap]
* e.g.: http://auth.customer.com:80 GET
**/
public void authenticate(Account acct, String password, Map<String, Object> context, List<String> args) throws Exception {
HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
HttpMethod method = null;
String targetURL = args.get(0);
if (args.size() > 1) {
if (args.get(1).equalsIgnoreCase("GET"))
method = new GetMethod(targetURL);
else
method = new PostMethod(targetURL);
} else
method = new GetMethod(targetURL);
if (context.get(AuthContext.AC_ORIGINATING_CLIENT_IP) != null)
method.addRequestHeader(HEADER_CLIENT_IP, context.get(AuthContext.AC_ORIGINATING_CLIENT_IP).toString());
if (context.get(AuthContext.AC_REMOTE_IP) != null)
method.addRequestHeader(HEADER_X_ZIMBRA_REMOTE_ADDR, context.get(AuthContext.AC_REMOTE_IP).toString());
method.addRequestHeader(HEADER_AUTH_USER, acct.getName());
method.addRequestHeader(HEADER_AUTH_PASSWORD, password);
AuthContext.Protocol proto = (AuthContext.Protocol) context.get(AuthContext.AC_PROTOCOL);
if (proto != null)
method.addRequestHeader(HEADER_AUTH_PROTOCOL, proto.toString());
if (context.get(AuthContext.AC_USER_AGENT) != null)
method.addRequestHeader(HEADER_AUTH_USER_AGENT, context.get(AuthContext.AC_USER_AGENT).toString());
try {
HttpClientUtil.executeMethod(client, method);
} catch (HttpException ex) {
throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), acct.getName(), "HTTP request to remote authentication server failed", ex);
} catch (IOException ex) {
throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), acct.getName(), "HTTP request to remote authentication server failed", ex);
} finally {
if (method != null)
method.releaseConnection();
}
int status = method.getStatusCode();
if (status != HttpStatus.SC_OK) {
throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), "HTTP request to remote authentication server failed. Remote response code: " + Integer.toString(status));
}
String responseMessage;
if (method.getResponseHeader(HEADER_AUTH_STATUS) != null) {
responseMessage = method.getResponseHeader(HEADER_AUTH_STATUS).getValue();
} else {
throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), "Empty response from remote authentication server.");
}
if (responseMessage.equalsIgnoreCase(AUTH_STATUS_OK)) {
return;
} else {
throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), responseMessage);
}
}
Aggregations