use of org.apache.http.StatusLine in project wildfly by wildfly.
the class UndertowHandlersConfigTestCase method testRewrite.
@Test
public void testRewrite() throws Exception {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpget = new HttpGet(url.toExternalForm() + "rewritea");
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
assertEquals(200, statusLine.getStatusCode());
String result = EntityUtils.toString(entity);
Assert.assertEquals("A file", result);
Header[] headers = response.getHeaders("MyHeader");
Assert.assertEquals(1, headers.length);
Assert.assertEquals("MyValue", headers[0].getValue());
}
}
use of org.apache.http.StatusLine in project wildfly by wildfly.
the class Utils method makeCall.
/**
* Makes HTTP call with FORM authentication.
*
* @param URL
* @param user
* @param pass
* @param expectedStatusCode
* @throws Exception
*/
public static void makeCall(String URL, String user, String pass, int expectedStatusCode) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
EntityUtils.consume(entity);
}
// We should get the Login Page
StatusLine statusLine = response.getStatusLine();
assertEquals(200, statusLine.getStatusCode());
// We should now login with the user name and password
HttpPost httpost = new HttpPost(URL + "/j_security_check");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("j_username", user));
nvps.add(new BasicNameValuePair("j_password", pass));
httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
response = httpclient.execute(httpost);
entity = response.getEntity();
if (entity != null) {
EntityUtils.consume(entity);
}
statusLine = response.getStatusLine();
// Post authentication - we have a 302
assertEquals(302, statusLine.getStatusCode());
Header locationHeader = response.getFirstHeader("Location");
String location = locationHeader.getValue();
HttpGet httpGet = new HttpGet(location);
response = httpclient.execute(httpGet);
entity = response.getEntity();
if (entity != null) {
EntityUtils.consume(entity);
}
// Either the authentication passed or failed based on the expected status code
statusLine = response.getStatusLine();
assertEquals(expectedStatusCode, statusLine.getStatusCode());
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
use of org.apache.http.StatusLine in project ThinkAndroid by white-cat.
the class BinaryHttpResponseHandler method sendResponseMessage.
// Interface to AsyncHttpRequest
@Override
protected void sendResponseMessage(HttpResponse response) {
StatusLine status = response.getStatusLine();
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
byte[] responseBody = null;
if (contentTypeHeaders.length != 1) {
//malformed/ambiguous HTTP Header, ABORT!
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"), responseBody);
return;
}
Header contentTypeHeader = contentTypeHeaders[0];
boolean foundAllowedContentType = false;
for (String anAllowedContentType : mAllowedContentTypes) {
if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
foundAllowedContentType = true;
}
}
if (!foundAllowedContentType) {
//Content-Type not in allowed list, ABORT!
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"), responseBody);
return;
}
try {
HttpEntity entity = null;
HttpEntity temp = response.getEntity();
if (temp != null) {
entity = new BufferedHttpEntity(temp);
}
responseBody = EntityUtils.toByteArray(entity);
} catch (IOException e) {
sendFailureMessage(e, (byte[]) null);
}
if (status.getStatusCode() >= 300) {
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
} else {
sendSuccessMessage(status.getStatusCode(), responseBody);
}
}
use of org.apache.http.StatusLine in project Diaspora-Webclient by voidcode.
the class getPodlistTask method doInBackground.
@Override
protected String[] doInBackground(Void... params) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
List<String> list = null;
try {
HttpGet httpGet = new HttpGet("http://podupti.me/api.php?key=4r45tg&format=json");
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
//TODO Notify User about failure
//Log.e("Diaspora-WebClient", "Failed to download file");
}
} catch (ClientProtocolException e) {
//TODO handle network unreachable exception here
e.printStackTrace();
} catch (IOException e) {
//TODO handle json buggy feed
e.printStackTrace();
}
//Parse the JSON Data
try {
JSONObject j = new JSONObject(builder.toString());
JSONArray jr = j.getJSONArray("pods");
//Log.i("Diaspora-WebClient","Number of entries " + jr.length());
list = new ArrayList<String>();
for (int i = 0; i < jr.length(); i++) {
JSONObject jo = jr.getJSONObject(i);
//Log.i("Diaspora-WebClient", jo.getString("domain"));
String secure = jo.getString("secure");
if (secure.equals("true"))
list.add(jo.getString("domain"));
}
} catch (Exception e) {
//TODO Handle Parsing errors here
e.printStackTrace();
}
return list.toArray(new String[list.size()]);
}
use of org.apache.http.StatusLine in project platform_external_apache-http by android.
the class DefaultResponseParser method parseHead.
@Override
protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
// clear the buffer
this.lineBuf.clear();
//read out the HTTP status string
int count = 0;
ParserCursor cursor = null;
do {
int i = sessionBuffer.readLine(this.lineBuf);
if (i == -1 && count == 0) {
// The server just dropped connection on us
throw new NoHttpResponseException("The target server failed to respond");
}
cursor = new ParserCursor(0, this.lineBuf.length());
if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
// Got one
break;
} else if (i == -1 || count >= this.maxGarbageLines) {
// Giving up
throw new ProtocolException("The server failed to respond with a " + "valid HTTP response");
}
count++;
} while (true);
//create the status line from the status string
StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
return this.responseFactory.newHttpResponse(statusline, null);
}
Aggregations