/*
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.0-beta1/module-client/src/examples/org/apache/http/examples/client/ClientAuthentication.java $
* $Revision: 558158 $
* $Date: 2007-07-20 23:33:17 +0200 (Fri, 20 Jul 2007) $
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package com.simoncat.net;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.IOException;
/**
* A simple example that uses HttpClient to execute an HTTP request against
* a target site that requires user authentication.
*/
public class HttpClient {
private String host;
private int port;
private String user;
private String pwd;
private String url;
private String statusLine;
private String answer;
private InputStream content;
InputStreamReader isr;
LineNumberReader lnr;
public HttpClient(String host,int port, String user,String pwd,String url){
this.host=host;
this.port=port;
this.user=user;
this.pwd=pwd;
this.url=url;
}
public boolean execute(){
boolean r=false;
try{
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(host, port),
new UsernamePasswordCredentials(user,pwd));
HttpGet httpget = new HttpGet(url);
System.out.println("executing request:" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
statusLine = response.getStatusLine().toString();
System.out.println("statusLine:"+statusLine);
if(entity!= null){
//System.out.println("s1");
content = entity.getContent();
//System.out.println("s2");
//if(content.available()>0){
//System.out.println("s3");
isr = new InputStreamReader(content);
//System.out.println("s4");
lnr = new LineNumberReader(isr);
//System.out.println("s5");
answer = lnr.readLine();
//System.out.println("s6");
//System.out.println("answer:"+answer);
//System.out.println("s7");
if(answer.startsWith("OK") && statusLine.trim().endsWith("OK")){
r=true;
//System.out.println("s8");
}
//System.out.println("s9");
//}
//System.out.println("s10");
}
//System.out.println("s11");
}
catch(IOException io){
io.printStackTrace();
//System.out.println("s12");
}
//System.out.println("s13");
return r;
}
public String appState(String appName){
url="http://"+host+":"+port+"/manager/list";
boolean r = execute();
System.out.println("r:"+r);
String a="";
if(r){
System.out.println("r1");
try{
//InputStreamReader isr = new InputStreamReader(content);
//LineNumberReader lnr = new LineNumberReader(isr);
String ln = lnr.readLine();
//System.out.println("r2");
while(ln!=null){
//System.out.println("r3");
//System.out.println("linea:"+ln);
if(ln.indexOf(appName+":")>=0)
a=ln;
ln = lnr.readLine();
}
//System.out.println("r4");
}
catch(IOException io){
io.printStackTrace();
//System.out.println("r5");
}
}
//System.out.println("r6");
return a;
}
public boolean appIsRunning(String appName){
boolean a=false;
String st = appState(appName);
//System.out.println("st:"+st);
if(st.length()> 0 && st.indexOf(appName=":running")>=0)
a=true;
return a;
}
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("tomcat", "admin"));
HttpGet httpget = new HttpGet("http://localhost:8080/manager/undeploy?path=/mvx");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
entity.writeTo(System.out);
System.out.println("Chunked?: " + entity.isChunked());
}
if (entity != null) {
entity.consumeContent();
}
httpget = new HttpGet("http://localhost:8080/manager/list");
System.out.println("executing request" + httpget.getRequestLine());
response = httpclient.execute(httpget);
entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
entity.writeTo(System.out);
System.out.println("Chunked?: " + entity.isChunked());
}
if (entity != null) {
entity.consumeContent();
}
httpget = new HttpGet("http://localhost:8080/manager/deploy?path=/Binary&war=file:/tmp/Binary.war");
System.out.println("executing request" + httpget.getRequestLine());
response = httpclient.execute(httpget);
entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
entity.writeTo(System.out);
System.out.println("Chunked?: " + entity.isChunked());
}
if (entity != null) {
entity.consumeContent();
}
}
}