use of org.apache.commons.httpclient.UsernamePasswordCredentials in project intellij-community by JetBrains.
the class YouTrackRepository method login.
private HttpClient login(PostMethod method) throws Exception {
HttpClient client = getHttpClient();
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(getUsername(), getPassword()));
configureHttpMethod(method);
method.addParameter("login", getUsername());
method.addParameter("password", getPassword());
client.getParams().setContentCharset("UTF-8");
client.executeMethod(method);
String response;
try {
if (method.getStatusCode() != 200) {
throw new HttpRequests.HttpStatusException("Cannot login", method.getStatusCode(), method.getPath());
}
response = method.getResponseBodyAsString(1000);
} finally {
method.releaseConnection();
}
if (response == null) {
throw new NullPointerException();
}
if (!response.contains("<login>ok</login>")) {
int pos = response.indexOf("</error>");
int length = "<error>".length();
if (pos > length) {
response = response.substring(length, pos);
}
throw new Exception("Cannot login: " + response);
}
return client;
}
use of org.apache.commons.httpclient.UsernamePasswordCredentials in project oxCore by GluuFederation.
the class HTTPFileDownloader method createHttpClientWithBasicAuth.
private static HttpClient createHttpClientWithBasicAuth(String userid, String password) {
Credentials credentials = new UsernamePasswordCredentials(userid, password);
HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY, credentials);
httpClient.getParams().setAuthenticationPreemptive(true);
return httpClient;
}
use of org.apache.commons.httpclient.UsernamePasswordCredentials in project tdi-studio-se by Talend.
the class MDMTransaction method rollback.
public void rollback() throws IOException {
HttpClient client = new HttpClient();
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpMethod method = new DeleteMethod(url + "/" + id);
method.setDoAuthentication(true);
try {
//$NON-NLS-1$ //$NON-NLS-2$
method.setRequestHeader("Cookie", getStickySession() + "=" + sessionId);
client.executeMethod(method);
} catch (HttpException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
method.releaseConnection();
}
int statuscode = method.getStatusCode();
if (statuscode >= 400) {
throw new MDMTransactionException("Rollback failed. The rollback operation has returned the code " + statuscode + ".");
}
}
use of org.apache.commons.httpclient.UsernamePasswordCredentials in project zm-mailbox by Zimbra.
the class WebDavClient method setCredential.
public void setCredential(String user, String pass) {
mUsername = user;
mPassword = pass;
HttpState state = new HttpState();
Credentials cred = new UsernamePasswordCredentials(mUsername, mPassword);
state.setCredentials(AuthScope.ANY, cred);
mClient.setState(state);
ArrayList<String> authPrefs = new ArrayList<String>();
authPrefs.add(AuthPolicy.BASIC);
mClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
mClient.getParams().setAuthenticationPreemptive(true);
}
use of org.apache.commons.httpclient.UsernamePasswordCredentials 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