org.cvasilak.jboss.mobile.admin.net.JBossResponseHandler.java Source code

Java tutorial

Introduction

Here is the source code for org.cvasilak.jboss.mobile.admin.net.JBossResponseHandler.java

Source

/*
 * JBoss Admin
 * Copyright 2012, Christos Vasilakis, and individual contributors.
 * See the copyright.txt file in the distribution for a full
 * listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.cvasilak.jboss.mobile.admin.net;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.annotation.Immutable;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/*
 * A response handler that doesn't throw Exception
 * (as the BasicResponseHandler does) upon a >500 error
 * retrieved from the jboss server. Instead it consumes
 * the error message.
 */
@Immutable
public class JBossResponseHandler implements ResponseHandler<String> {

    public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
        StatusLine statusLine = response.getStatusLine();

        if (statusLine.getStatusCode() >= 300 && statusLine.getStatusCode() < 500) {
            throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        HttpEntity entity = response.getEntity();
        return entity == null ? null : EntityUtils.toString(entity);
    }

}