本文整理匯總了Java中org.apache.commons.httpclient.HttpState.setCredentials方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpState.setCredentials方法的具體用法?Java HttpState.setCredentials怎麽用?Java HttpState.setCredentials使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.httpclient.HttpState
的用法示例。
在下文中一共展示了HttpState.setCredentials方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testPreemptiveAuthorization
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
/**
* Make sure preemptive authorization works when the server requires NLM.
* @throws Exception
*/
public void testPreemptiveAuthorization() throws Exception {
NTCredentials creds =
new NTCredentials("testuser", "testpass", "host", "domain");
HttpState state = new HttpState();
state.setCredentials(AuthScope.ANY, creds);
this.client.setState(state);
this.client.getParams().setAuthenticationPreemptive(true);
this.server.setHttpService(new PreemptiveNTLMAuthService());
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
} finally {
httpget.releaseConnection();
}
assertNotNull(httpget.getStatusLine());
assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
}
示例2: setConnectionAuthorization
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
/**
* Extracts all the required authorization for that particular URL request
* and sets it in the <code>HttpMethod</code> passed in.
*
* @param client the HttpClient object
*
* @param u
* <code>URL</code> of the URL request
* @param authManager
* the <code>AuthManager</code> containing all the authorisations for
* this <code>UrlConfig</code>
*/
private void setConnectionAuthorization(HttpClient client, URL u, AuthManager authManager) {
HttpState state = client.getState();
if (authManager != null) {
HttpClientParams params = client.getParams();
Authorization auth = authManager.getAuthForURL(u);
if (auth != null) {
String username = auth.getUser();
String realm = auth.getRealm();
String domain = auth.getDomain();
if (log.isDebugEnabled()){
log.debug(username + " > D="+ username + " D="+domain+" R="+realm);
}
state.setCredentials(
new AuthScope(u.getHost(),u.getPort(),
realm.length()==0 ? null : realm //"" is not the same as no realm
,AuthScope.ANY_SCHEME),
// NT Includes other types of Credentials
new NTCredentials(
username,
auth.getPass(),
localHost,
domain
));
// We have credentials - should we set pre-emptive authentication?
if (canSetPreEmptive){
log.debug("Setting Pre-emptive authentication");
params.setAuthenticationPreemptive(true);
}
} else {
state.clearCredentials();
if (canSetPreEmptive){
params.setAuthenticationPreemptive(false);
}
}
} else {
state.clearCredentials();
}
}
示例3: testBasicAuthenticationWithDefaultCreds
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testBasicAuthenticationWithDefaultCreds() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
HttpState state = new HttpState();
state.setCredentials(AuthScope.ANY, creds);
this.client.setState(state);
this.server.setRequestHandler(handlerchain);
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
} finally {
httpget.releaseConnection();
}
assertNotNull(httpget.getStatusLine());
assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
Header auth = httpget.getRequestHeader("Authorization");
assertNotNull(auth);
String expected = "Basic " + EncodingUtil.getAsciiString(
Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
assertEquals(expected, auth.getValue());
AuthState authstate = httpget.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
}
示例4: testBasicAuthentication
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testBasicAuthentication() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
HttpState state = new HttpState();
AuthScope authscope = new AuthScope(
this.server.getLocalAddress(),
this.server.getLocalPort(),
"test");
state.setCredentials(authscope, creds);
this.client.setState(state);
this.server.setRequestHandler(handlerchain);
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
} finally {
httpget.releaseConnection();
}
assertNotNull(httpget.getStatusLine());
assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
Header auth = httpget.getRequestHeader("Authorization");
assertNotNull(auth);
String expected = "Basic " + EncodingUtil.getAsciiString(
Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
assertEquals(expected, auth.getValue());
AuthState authstate = httpget.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
}
示例5: testBasicAuthenticationWithInvalidCredentials
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testBasicAuthenticationWithInvalidCredentials() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
HttpState state = new HttpState();
AuthScope authscope = new AuthScope(
this.server.getLocalAddress(),
this.server.getLocalPort(),
"test");
state.setCredentials(authscope, new UsernamePasswordCredentials("test", "stuff"));
this.client.setState(state);
this.server.setRequestHandler(handlerchain);
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
} finally {
httpget.releaseConnection();
}
assertNotNull(httpget.getStatusLine());
assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode());
AuthState authstate = httpget.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
}
示例6: testPreemptiveAuthorizationTrueWithCreds
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testPreemptiveAuthorizationTrueWithCreds() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
HttpState state = new HttpState();
state.setCredentials(AuthScope.ANY, creds);
this.client.setState(state);
this.client.getParams().setAuthenticationPreemptive(true);
this.server.setRequestHandler(handlerchain);
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
} finally {
httpget.releaseConnection();
}
assertNotNull(httpget.getStatusLine());
assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
Header auth = httpget.getRequestHeader("Authorization");
assertNotNull(auth);
String expected = "Basic " + EncodingUtil.getAsciiString(
Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
assertEquals(expected, auth.getValue());
AuthState authstate = httpget.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertNull(authstate.getRealm());
assertTrue(authstate.isPreemptive());
}
示例7: testHeadBasicAuthentication
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testHeadBasicAuthentication() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
HttpState state = new HttpState();
AuthScope authscope = new AuthScope(
this.server.getLocalAddress(),
this.server.getLocalPort(),
"test");
state.setCredentials(authscope, creds);
this.client.setState(state);
this.server.setRequestHandler(handlerchain);
HeadMethod head = new HeadMethod("/test/");
try {
this.client.executeMethod(head);
} finally {
head.releaseConnection();
}
assertNotNull(head.getStatusLine());
assertEquals(HttpStatus.SC_OK, head.getStatusLine().getStatusCode());
Header auth = head.getRequestHeader("Authorization");
assertNotNull(auth);
String expected = "Basic " + EncodingUtil.getAsciiString(
Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
assertEquals(expected, auth.getValue());
AuthState authstate = head.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
}
示例8: testPostBasicAuthentication
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testPostBasicAuthentication() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
HttpState state = new HttpState();
AuthScope authscope = new AuthScope(
this.server.getLocalAddress(),
this.server.getLocalPort(),
"test");
state.setCredentials(authscope, creds);
this.client.setState(state);
this.server.setRequestHandler(handlerchain);
PostMethod post = new PostMethod("/test/");
post.setRequestEntity(new StringRequestEntity("Test body", null, null));
try {
this.client.executeMethod(post);
assertEquals("Test body", post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
assertNotNull(post.getStatusLine());
assertEquals(HttpStatus.SC_OK, post.getStatusLine().getStatusCode());
Header auth = post.getRequestHeader("Authorization");
assertNotNull(auth);
String expected = "Basic " + EncodingUtil.getAsciiString(
Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
assertEquals(expected, auth.getValue());
AuthState authstate = post.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
}
示例9: testPutBasicAuthentication
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testPutBasicAuthentication() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
HttpState state = new HttpState();
AuthScope authscope = new AuthScope(
this.server.getLocalAddress(),
this.server.getLocalPort(),
"test");
state.setCredentials(authscope, creds);
this.client.setState(state);
this.server.setRequestHandler(handlerchain);
PutMethod put = new PutMethod("/test/");
put.setRequestEntity(new StringRequestEntity("Test body", null, null));
try {
this.client.executeMethod(put);
assertEquals("Test body", put.getResponseBodyAsString());
} finally {
put.releaseConnection();
}
assertNotNull(put.getStatusLine());
assertEquals(HttpStatus.SC_OK, put.getStatusLine().getStatusCode());
Header auth = put.getRequestHeader("Authorization");
assertNotNull(auth);
String expected = "Basic " + EncodingUtil.getAsciiString(
Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
assertEquals(expected, auth.getValue());
AuthState authstate = put.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
}
示例10: testPreemptiveAuthorizationFailure
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testPreemptiveAuthorizationFailure() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
UsernamePasswordCredentials wrongcreds =
new UsernamePasswordCredentials("testuser", "garbage");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
HttpState state = new HttpState();
state.setCredentials(AuthScope.ANY, wrongcreds);
this.client.setState(state);
this.client.getParams().setAuthenticationPreemptive(true);
this.server.setRequestHandler(handlerchain);
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
} finally {
httpget.releaseConnection();
}
assertNotNull(httpget.getStatusLine());
assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode());
AuthState authstate = httpget.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
assertTrue(authstate.isPreemptive());
}
示例11: getHttpState
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
/**
* Get the HTTPState for a transfer target
* @param target TransferTarget
* @return HttpState
*/
protected HttpState getHttpState(TransferTarget target)
{
HttpState httpState = new HttpState();
httpState.setCredentials(new AuthScope(target.getEndpointHost(), target.getEndpointPort(),
AuthScope.ANY_REALM),
new UsernamePasswordCredentials(target.getUsername(), new String(target.getPassword())));
return httpState;
}
示例12: testPostBasicAuthentication
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testPostBasicAuthentication() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
HttpState state = new HttpState();
AuthScope authscope = new AuthScope(
this.server.getLocalAddress(),
this.server.getLocalPort(),
"test");
state.setCredentials(authscope, creds);
this.client.setState(state);
this.server.setRequestHandler(handlerchain);
PostMethod post = new PostMethod("/test/");
post.setRequestEntity(new StringRequestEntity("Test body"));
try {
this.client.executeMethod(post);
assertEquals("Test body", post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
assertNotNull(post.getStatusLine());
assertEquals(HttpStatus.SC_OK, post.getStatusLine().getStatusCode());
Header auth = post.getRequestHeader("Authorization");
assertNotNull(auth);
String expected = "Basic " + EncodingUtil.getAsciiString(
Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
assertEquals(expected, auth.getValue());
AuthState authstate = post.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
}
示例13: testPutBasicAuthentication
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public void testPutBasicAuthentication() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
HttpState state = new HttpState();
AuthScope authscope = new AuthScope(
this.server.getLocalAddress(),
this.server.getLocalPort(),
"test");
state.setCredentials(authscope, creds);
this.client.setState(state);
this.server.setRequestHandler(handlerchain);
PutMethod put = new PutMethod("/test/");
put.setRequestEntity(new StringRequestEntity("Test body"));
try {
this.client.executeMethod(put);
assertEquals("Test body", put.getResponseBodyAsString());
} finally {
put.releaseConnection();
}
assertNotNull(put.getStatusLine());
assertEquals(HttpStatus.SC_OK, put.getStatusLine().getStatusCode());
Header auth = put.getRequestHeader("Authorization");
assertNotNull(auth);
String expected = "Basic " + EncodingUtil.getAsciiString(
Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
assertEquals(expected, auth.getValue());
AuthState authstate = put.getHostAuthState();
assertNotNull(authstate.getAuthScheme());
assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
assertEquals("test", authstate.getRealm());
}
示例14: unsubscribe
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
public boolean unsubscribe(String uri, Subscriber listener, Credentials credentials) {
UnsubscribeMethod unsubscribeMethod = new UnsubscribeMethod(repositoryDomain+uri);
synchronized ( subscribers ) {
for ( Iterator i = subscribers.iterator(); i.hasNext(); ) {
Subscription subscription = (Subscription)i.next();
if ( subscription.getSubscriber().equals(listener) ) {
String id = String.valueOf(subscription.getId());
unsubscribeMethod.addRequestHeader(UnsubscribeMethod.H_SUBSCRIPTION_ID, id);
try {
unsubscribeMethod.setDoAuthentication(true);
HttpState httpState = new HttpState();
httpState.setCredentials(null, repositoryHost, credentials);
HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort, protocol);
httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
int state = unsubscribeMethod.execute(httpState, httpConnection);
if ( state == HttpStatus.SC_OK ) {
i.remove();
return true;
} else {
logger.log(Level.SEVERE, "Unsubscription failed. State: "+state);
}
} catch (IOException e) {
logger.log(Level.SEVERE, "Unsubscription of listener '"+listener+"' failed!", e);
}
}
}
}
logger.log(Level.SEVERE, "Listener not unsubscribed!");
return false;
}
示例15: fireEvent
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
protected void fireEvent(EventMethod eventMethod, Credentials credentials) throws IOException {
eventMethod.setDoAuthentication(true);
HttpState httpState = new HttpState();
httpState.setCredentials(null, repositoryHost, credentials);
int state = eventMethod.execute(httpState, new HttpConnection(repositoryHost, repositoryPort, protocol));
if ( state == HttpStatus.SC_OK ) {
} else {
logger.log(Level.SEVERE, "Event failed. State: "+state);
}
}