Example usage for org.apache.commons.httpclient HttpMethod getResponseBodyAsString

List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod getResponseBodyAsString.

Prototype

public abstract String getResponseBodyAsString() throws IOException;

Source Link

Usage

From source file:org.onebusaway.enterprise.webapp.actions.api.GtfsrtProxyAction.java

@Override
public String execute() throws Exception {

    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    client.executeMethod(method);//from w  w  w. ja  va2 s .  com
    _results = method.getResponseBodyAsString();

    return SUCCESS;
}

From source file:org.openo.nfvo.vnfmadapter.common.ResultRequestUtil.java

/**
 * common method/*ww w. j av  a2 s .  c o  m*/
 * <br/>
 *
 * @param vnfmObject
 * @param path
 *            url defined
 * @param methodName
 *            [get, put, delete, post]
 * @param paramsJson
 *            raw data with json format, if <code>methodName</code> is get
 *            or delete, fill it with null
 * @return
 * @since NFVO 0.5
 */
public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson) {
    JSONObject resultJson = new JSONObject();

    ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();

    if (Constant.HTTP_OK != mgrVcmm.connect(vnfmObject)) {
        resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultJson.put("data", "connect fail.");
        return resultJson;
    }

    HttpMethod httpMethod = null;
    try {

        String result = null;
        String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
        LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
                mgrVcmm.getAccessSession());
        HttpRequests.Builder builder = new HttpRequests.Builder(Constant.ANONYMOUS)
                .addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
                .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
        MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
        MethodHandle mt = MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType)
                .bindTo(builder);

        builder = (HttpRequests.Builder) mt.invoke();
        httpMethod = builder.execute();
        result = httpMethod.getResponseBodyAsString();
        LOG.warn("function=call, msg=response status is {}. result is {}", httpMethod.getStatusCode(), result);
        resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
        resultJson.put("data", result);
    } catch (IOException e) {
        LOG.info("function=call, msg=IOException, e is {}", e);
    } catch (ReflectiveOperationException e) {
        LOG.info("function=call, msg=ReflectiveOperationException, e is {}", e);
    } catch (Throwable e) {
        LOG.info("function=call, msg=Throwable, e is {}", e);
    } finally {
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }

    if (httpMethod == null) {
        resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultJson.put("data", "get connection error");
    }

    return resultJson;
}

From source file:org.openo.nfvo.vnfmadapter.common.ResultRequestUtil.java

/**
 * common method//from  ww w  . j a  v  a 2  s .  c  o  m
 * <br/>
 *
 * @param vnfmObject
 * @param path
 *            url defined
 * @param methodName
 *            [get, put, delete, post]
 * @param paramsJson
 *            raw data with json format, if <code>methodName</code> is get
 *            or delete, fill it with null
 * @return
 * @since NFVO 0.5
 */
public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson,
        String authModel) {
    LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
            + vnfmObject);
    JSONObject resultJson = new JSONObject();

    ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();

    if (Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, authModel)) {
        resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultJson.put("data", "connect fail.");
        return resultJson;
    }

    HttpMethod httpMethod = null;
    try {

        String result = null;
        String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
        LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
                mgrVcmm.getAccessSession());
        HttpRequests.Builder builder = new HttpRequests.Builder(authModel)
                .addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
                .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
        MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
        MethodHandle mt = MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType)
                .bindTo(builder);

        builder = (HttpRequests.Builder) mt.invoke();
        httpMethod = builder.execute();
        result = httpMethod.getResponseBodyAsString();
        LOG.warn("function=call, msg=response status is {}. result is {}", httpMethod.getStatusCode(), result);
        resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
        resultJson.put("data", result);

        // logout delete tokens
        String token = mgrVcmm.getAccessSession();
        String roaRand = mgrVcmm.getRoaRand();
        String vnfmUrl = vnfmObject.getString("url");
        removeTokens(vnfmUrl, token, roaRand);
    } catch (IOException e) {
        LOG.info("function=call, msg=IOException, e is {}", e);
    } catch (ReflectiveOperationException e) {
        LOG.info("function=call, msg=ReflectiveOperationException, e is {}", e);
    } catch (Throwable e) {
        LOG.info("function=call, msg=Throwable, e is {}", e);
    } finally {
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }

    if (httpMethod == null) {
        resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultJson.put("data", "get connection error");
    }

    return resultJson;
}

From source file:org.openo.nfvo.vnfmadapter.common.ResultRequestUtil.java

/**
 * <br>//from www .  j av a 2s . co  m
 *
 * @since NFVO 0.5
 */
private static void removeTokens(String vnfmUrl, String token, String roaRand) {
    HttpMethod httpMethodToken = null;
    String tokenUrl = String.format(ParamConstants.CSM_AUTH_DISCONNECT, "manoadmin", roaRand);
    LOG.info("removeTokens tokenUrl=" + tokenUrl);
    try {
        httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
                .setParams("").addHeader("X-Auth-Token", token).delete().execute();
        int statusCode = httpMethodToken.getStatusCode();
        String result = httpMethodToken.getResponseBodyAsString();
        LOG.info("removeTokens int=" + statusCode + ", result=" + result);
    } catch (IOException e) {
        LOG.info("function=call, msg=IOException, e is {}", e);
    } catch (Throwable e) {
        LOG.info("function=call, msg=Throwable, e is {}", e);
    } finally {
        if (httpMethodToken != null) {
            httpMethodToken.releaseConnection();
        }
    }
}

From source file:org.openo.nfvo.vnfmadapter.service.adapter.impl.AdapterResourceManager.java

@Override
public JSONObject getAllCloud(String url, String conntoken) {
    JSONObject resultObj = new JSONObject();
    JSONArray resArray = new JSONArray();

    if (url == null || url.equals("")) {
        url = "http://127.0.0.1:31943";
    }//from  w ww.  ja v a  2  s . c o m

    // get vim_id
    HttpMethod httpMethodCloud = null;
    try {
        httpMethodCloud = new HttpRequests.Builder(Constant.CERTIFICATE)
                .setUrl(url.trim(), UrlConstant.URL_ALLCLOUD_NEW_GET)
                .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).setParams("").get().execute();

        int statusCode = httpMethodCloud.getStatusCode();

        String result = httpMethodCloud.getResponseBodyAsString();
        LOG.info(result);
        if (statusCode == HttpStatus.SC_OK) {
            JSONObject vimInfo = JSONObject.fromObject(result);
            resArray = vimInfo.getJSONArray("vim_info");
            resultObj = resArray.getJSONObject(0);
            resultObj.put(Constant.RETCODE, statusCode);
        } else {
            LOG.error("uploadVNFPackage get allcloud failed, code:" + statusCode + " re:" + result);
            resultObj.put(Constant.RETCODE, statusCode);
            resultObj.put("reason", "get allcloud failed. code:" + statusCode + " re:" + result);
            return resultObj;
        }
    } catch (JSONException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud JSONException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get allcloud failed and JSONException." + e.getMessage());
        return resultObj;
    } catch (VnfmException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud VnfmException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get allcloud failed and VnfmException." + e.getMessage());
        return resultObj;
    } catch (IOException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud IOException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get allcloud failed and IOException." + e.getMessage());
        return resultObj;
    }
    return resultObj;
}

From source file:org.openo.nfvo.vnfmadapter.service.adapter.impl.AdapterResourceManager.java

/**
 * Upload vnfpackage<br>//  w  ww.j  a  v  a2s .  co  m
 *
 * @param vnfpackage
 * @param vnfmurl
 * @param conntoken
 * @return
 * @since NFVO 0.5
 */
public JSONObject upload(JSONObject vnfpackage, String vnfmurl, String conntoken) {
    JSONObject resultObj = new JSONObject();
    HttpMethod httpMethodVnf = null;

    try {
        httpMethodVnf = new HttpRequests.Builder(Constant.CERTIFICATE)
                .setUrl(vnfmurl.trim(), UrlConstant.URL_VNFPACKAGE_POST).setParams(vnfpackage.toString())
                .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).post().execute();

        int statusCodeUp = httpMethodVnf.getStatusCode();

        String resultUp = httpMethodVnf.getResponseBodyAsString();

        if (statusCodeUp == HttpStatus.SC_CREATED || statusCodeUp == HttpStatus.SC_OK) {
            LOG.info(
                    "uploadVNFPackage upload VNF package successful, code:" + statusCodeUp + " re:" + resultUp);
            resultObj = JSONObject.fromObject(resultUp);
            resultObj.put(Constant.RETCODE, statusCodeUp);
        } else {
            LOG.error("uploadVNFPackage upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
            resultObj.put(Constant.RETCODE, statusCodeUp);
            resultObj.put("data", "upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
            return resultObj;
        }
    } catch (JSONException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package JSONException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "upload VNF package failed and JSONException." + e.getMessage());
        return resultObj;
    } catch (VnfmException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package VnfmException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "upload VNF package failed and VnfmException." + e.getMessage());
        return resultObj;
    } catch (IOException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package IOException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "upload VNF package failed and IOException." + e.getMessage());
        return resultObj;
    }
    return resultObj;
}

From source file:org.openo.nfvo.vnfmadapter.service.adapter.impl.AdapterResourceManager.java

/**
 * Find vnfd version.<br>/*w  ww. j  av  a  2 s  .  c  o m*/
 *
 * @param prefixUrl
 * @param serviceUrl
 * @return
 * @since NFVO 0.5
 */
public JSONObject getVnfdVersion(String prefixUrl, String serviceUrl, String conntoken) {
    JSONObject resultObj = new JSONObject();
    HttpMethod httpMethodVnfd = null;
    try {
        httpMethodVnfd = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(prefixUrl.trim(), serviceUrl)
                .setParams("").addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();

        int statusCodeVnfd = httpMethodVnfd.getStatusCode();

        String resultVnfd = httpMethodVnfd.getResponseBodyAsString();
        LOG.info("getVnfdVersion result:" + resultVnfd);
        if (statusCodeVnfd == HttpStatus.SC_OK) {
            resultObj = JSONObject.fromObject(resultVnfd);
            resultObj.put(Constant.RETCODE, statusCodeVnfd);
        } else {
            LOG.error("uploadVNFPackage vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
            resultObj.put(Constant.RETCODE, statusCodeVnfd);
            resultObj.put("data", "get vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
            return resultObj;
        }
    } catch (JSONException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version JSONException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get vnfd version failed and JSONException." + e.getMessage());
        return resultObj;
    } catch (VnfmException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version VnfmException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get vnfd version failed and VnfmException." + e.getMessage());
        return resultObj;
    } catch (IOException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version IOException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get vnfd version failed and IOException." + e.getMessage());
        return resultObj;
    }
    return resultObj;
}

From source file:org.openo.nfvo.vnfmadapter.service.adapter.impl.AdapterResourceManager.java

@Override
public JSONObject getVNFDPlanInfo(String url, String vnfdid, String conntoken) {
    JSONObject resultObj = new JSONObject();

    HttpMethod httpMethodPlan = null;
    try {/* w ww  .  j a  v  a2  s.com*/
        httpMethodPlan = new HttpRequests.Builder(Constant.CERTIFICATE)
                .setUrl(url.trim(), String.format(UrlConstant.URL_VNFDPLANINFO_GET, vnfdid)).setParams("")
                .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();

        int statusCode = httpMethodPlan.getStatusCode();

        String result = httpMethodPlan.getResponseBodyAsString();
        LOG.info("getVNFDPlanInfo result=" + result);
        if (statusCode == HttpStatus.SC_OK) {
            resultObj = JSONObject.fromObject(result);
            resultObj.put(Constant.RETCODE, statusCode);
        } else {
            LOG.error("uploadVNFPackage get VNFDPlanInfo failed, code:" + statusCode + " re:" + result);
            resultObj.put(Constant.RETCODE, statusCode);
            resultObj.put("reason", "get VNFDPlanInfo failed. code:" + statusCode + " re:" + result);
            return resultObj;
        }
    } catch (JSONException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo JSONException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get VNFDPlanInfo failed and JSONException." + e.getMessage());
        return resultObj;
    } catch (VnfmException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo VnfmException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get VNFDPlanInfo failed and VnfmException." + e.getMessage());
        return resultObj;
    } catch (IOException e) {
        LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo IOException e={}.", e);
        resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
        resultObj.put("reason", "get VNFDPlanInfo failed and IOException." + e.getMessage());
        return resultObj;
    }
    return resultObj;
}

From source file:org.openo.nfvo.vnfmadapter.service.csm.connect.ConnectMgrVnfm.java

/**
 * Make connection//  w ww. j av a 2  s  . c  om
 * <br>
 *
 * @param vnfmObj
 * @return
 * @since  NFVO 0.5
 */
public int connect(JSONObject vnfmObj, String authModel) {
    LOG.info("function=connect, msg=enter connect function.");

    ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString("userName"),
            vnfmObj.getString("password"), authModel);
    HttpMethod httpMethod = null;
    int statusCode = Constant.INTERNAL_EXCEPTION;

    try {
        httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
                .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
                .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
                .post().execute();
        statusCode = httpMethod.getStatusCode();

        String result = httpMethod.getResponseBodyAsString();
        LOG.info("connect result:" + result);
        if (statusCode == HttpStatus.SC_CREATED) {
            JSONObject accessObj = JSONObject.fromObject(result);
            JSONObject tokenObj = accessObj.getJSONObject("token");
            Header header = httpMethod.getResponseHeader("accessSession");
            setAccessSession(header.getValue());
            setRoaRand(tokenObj.getString("roa_rand"));
            statusCode = HttpStatus.SC_OK;
        } else {
            LOG.error("connect fail, code:" + statusCode + " re:" + result);
        }

    } catch (JSONException e) {
        LOG.error("function=connect, msg=connect JSONException e={}.", e);
    } catch (VnfmException e) {
        LOG.error("function=connect, msg=connect VnfmException e={}.", e);
    } catch (IOException e) {
        LOG.error("function=connect, msg=connect IOException e={}.", e);
    } finally {
        clearCSMPwd(info);
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
    return statusCode;

}

From source file:org.openo.nfvo.vnfmadapter.service.csm.connect.ConnectMgrVnfm.java

/**
 * Make connection/*from w w  w  .j av a  2s .  c  o m*/
 * <br>
 *
 * @param vnfmObj
 * @return
 * @since  NFVO 0.5
 */
public int connect(JSONObject vnfmObj) {
    LOG.info("function=connect, msg=enter connect function.");

    ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString("userName"),
            vnfmObj.getString("password"), Constant.ANONYMOUS);
    HttpMethod httpMethod = null;
    int statusCode = Constant.INTERNAL_EXCEPTION;

    try {
        httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
                .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
                .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
                .post().execute();
        statusCode = httpMethod.getStatusCode();

        String result = httpMethod.getResponseBodyAsString();

        if (statusCode == HttpStatus.SC_CREATED) {
            JSONObject accessObj = JSONObject.fromObject(result);
            JSONObject tokenObj = accessObj.getJSONObject("token");
            Header header = httpMethod.getResponseHeader("accessSession");
            setAccessSession(header.getValue());
            setRoaRand(tokenObj.getString("roa_rand"));
            statusCode = HttpStatus.SC_OK;
        } else {
            LOG.error("connect fail, code:" + statusCode + " re:" + result);
        }

    } catch (JSONException e) {
        LOG.error("function=connect, msg=connect JSONException e={}.", e);
    } catch (VnfmException e) {
        LOG.error("function=connect, msg=connect VnfmException e={}.", e);
    } catch (IOException e) {
        LOG.error("function=connect, msg=connect IOException e={}.", e);
    } finally {
        clearCSMPwd(info);
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
    return statusCode;

}