List of usage examples for org.apache.commons.httpclient HttpMethod getStatusCode
public abstract int getStatusCode();
From source file:org.olat.user.propertyhandlers.MSNPropertyHandler.java
/** * @see org.olat.user.propertyhandlers.Generic127CharTextPropertyHandler#isValid(org.olat.core.gui.components.form.flexible.FormItem, java.util.Map) *///from w w w.java 2 s.c o m @SuppressWarnings({ "unchecked" }) @Override public boolean isValid(final FormItem formItem, final Map formContext) { boolean result; final TextElement textElement = (TextElement) formItem; OLog log = Tracing.createLoggerFor(this.getClass()); if (StringHelper.containsNonWhitespace(textElement.getValue())) { // Use an HttpClient to fetch a profile information page from MSN. final HttpClient httpClient = HttpClientFactory.getHttpClientInstance(); final HttpClientParams httpClientParams = httpClient.getParams(); httpClientParams.setConnectionManagerTimeout(2500); httpClient.setParams(httpClientParams); final HttpMethod httpMethod = new GetMethod(MSN_NAME_VALIDATION_URL); final NameValuePair idParam = new NameValuePair(MSN_NAME_URL_PARAMETER, textElement.getValue()); httpMethod.setQueryString(new NameValuePair[] { idParam }); // Don't allow redirects since otherwise, we won't be able to get the correct status httpMethod.setFollowRedirects(false); try { // Get the user profile page httpClient.executeMethod(httpMethod); final int httpStatusCode = httpMethod.getStatusCode(); // Looking at the HTTP status code tells us whether a user with the given MSN name exists. if (httpStatusCode == HttpStatus.SC_MOVED_PERMANENTLY) { // If the user exists, we get a 301... result = true; } else if (httpStatusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) { // ...and if the user doesn't exist, MSN sends a 500. textElement.setErrorKey("form.name.msn.error", null); result = false; } else { // For HTTP status codes other than 301 and 500 we will assume that the given MSN name is valid, but inform the user about this. textElement.setExampleKey("form.example.msnname.notvalidated", null); log.warn("MSN name validation: Expected HTTP status 301 or 500, but got " + httpStatusCode); result = true; } } catch (final Exception e) { // In case of any exception, assume that the given MSN name is valid (The opposite would block easily upon network problems), and inform the user about // this. textElement.setExampleKey("form.example.msnname.notvalidated", null); log.warn("MSN name validation: Exception: " + e.getMessage()); result = true; } } else { result = true; } log = null; return result; }
From source file:org.olat.user.propertyhandlers.XingPropertyHandler.java
/** * @see org.olat.user.propertyhandlers.Generic127CharTextPropertyHandler#isValid(org.olat.core.gui.components.form.flexible.FormItem, java.util.Map) *///from w w w . j ava2 s. co m @SuppressWarnings({ "unused", "unchecked" }) @Override public boolean isValid(final FormItem formItem, final Map formContext) { boolean result; final TextElement textElement = (TextElement) formItem; OLog log = Tracing.createLoggerFor(this.getClass()); if (StringHelper.containsNonWhitespace(textElement.getValue())) { final HttpClient httpClient = HttpClientFactory.getHttpClientInstance(); final HttpClientParams httpClientParams = httpClient.getParams(); httpClientParams.setConnectionManagerTimeout(2500); httpClient.setParams(httpClientParams); try { // Could throw IllegalArgumentException if argument is not a valid url // (e.g. contains whitespaces) final HttpMethod httpMethod = new GetMethod(XING_NAME_VALIDATION_URL + textElement.getValue()); // Don't allow redirects since otherwise, we won't be able to get the correct status httpMethod.setFollowRedirects(false); // Get the user profile page httpClient.executeMethod(httpMethod); final int httpStatusCode = httpMethod.getStatusCode(); // Looking at the HTTP status code tells us whether a user with the given Xing name exists. if (httpStatusCode == HttpStatus.SC_OK) { // If the user exists, we get a 200... result = true; } else if (httpStatusCode == HttpStatus.SC_MOVED_PERMANENTLY) { // ... and if he doesn't exist, we get a 301. textElement.setErrorKey("form.name.xing.error", null); result = false; } else { // In case of any exception, assume that the given MSN name is valid (The opposite would block easily upon network problems), and inform the user // about this. textElement.setExampleKey("form.example.xingname.notvalidated", null); log.warn("Xing name validation: Expected HTTP status 200 or 301, but got " + httpStatusCode); result = true; } } catch (final IllegalArgumentException e) { // The xing name is not url compatible (e.g. contains whitespaces) textElement.setErrorKey("form.xingname.notvalid", null); result = false; } catch (final Exception e) { // In case of any exception, assume that the given MSN name is valid (The opposite would block easily upon network problems), and inform the user about // this. textElement.setExampleKey("form.example.xingname.notvalidated", null); log.warn("Xing name validation: Exception: " + e.getMessage()); result = true; } } else { result = true; } log = null; return result; }
From source file:org.openmicroscopy.shoola.svc.proxy.Reply.java
/** * Checks the status of the response./*w ww . ja va 2 s .com*/ * * @param response The response to handle. * @return The message from server. * @throws TransportException If an error occurred while transferring data. */ protected static String checkStatusCode(HttpMethod response) throws TransportException { int status = response.getStatusCode(); if (status != -1) {//HttpStatus.SC_OK) { Reader reader = null; try { reader = new InputStreamReader(response.getResponseBodyAsStream()); char[] buf = new char[32678]; StringBuilder str = new StringBuilder(); for (int n; (n = reader.read(buf)) != -1;) str.append(buf, 0, n); try { if (reader != null) reader.close(); } catch (Exception ex) { } return str.toString(); } catch (Exception e) { try { if (reader != null) reader.close(); } catch (Exception ex) { } throw new TransportException("Couldn't handle request: " + HttpStatus.getStatusText(status) + "."); } } return null; }
From source file:org.openo.nfvo.vnfmadapter.common.ResultRequestUtil.java
/** * common method// w ww . 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) { 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// ww w . j ava 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 w w w . j av a2 s . c o 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 ww w. j ava 2 s. com*/ // 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 w w .j av a 2 s.c o 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>/*from ww w .java2 s.co 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 {//from w w w. java 2 s . co m 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; }