Java Code Examples for org.apache.commons.httpclient.HttpStatus#SC_OK

The following examples show how to use org.apache.commons.httpclient.HttpStatus#SC_OK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientSecretAuthentication.java    From JavaChatterRESTApi with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * <p>Authenticate using a client Secret. This is usually gotten by either asking the user,
 * or by using a callback. This allows for transparent (and non human-intervention) authentication.</p>
 * 
 * @return The response retrieved from the REST API (usually an XML string with all the tokens)
 * @throws IOException
 * @throws UnauthenticatedSessionException
 * @throws AuthenticationException
 */
public ChatterAuthToken authenticate() throws IOException, UnauthenticatedSessionException, AuthenticationException {
    String clientId = chatterData.getClientKey();
    String clientSecret = chatterData.getClientSecret();
    String clientRedirect = chatterData.getClientCallback();
    String clientCode = this.clientCode;

    if (null == clientCode && null != chatterData.getClientCode()) {
        clientCode = chatterData.getClientCode();
    }

    String authenticationUrl = "TEST".equalsIgnoreCase(chatterData.getEnvironment()) ? TEST_AUTHENTICATION_URL : PRODUCTION_AUTHENTICATION_URL;
    PostMethod post = new PostMethod(authenticationUrl);

    NameValuePair[] data = { new NameValuePair("grant_type", "authorization_code"),
        new NameValuePair("client_id", clientId), new NameValuePair("client_secret", clientSecret),
        new NameValuePair("redirect_uri", clientRedirect), new NameValuePair("code", clientCode) };
    
    post.setRequestBody(data);
    int statusCode = getHttpClient().executeMethod(post);
    if (statusCode == HttpStatus.SC_OK) {
        return processResponse(post.getResponseBodyAsString());
    }

    throw new UnauthenticatedSessionException(statusCode + " " + post.getResponseBodyAsString());
}
 
Example 2
Source File: JobsClient.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * This method initiates http get request on the request url and returns the
 * response returned from the http get.
 * @param requestUrl url on which the http get request has to be performed.
 * @return the http get response.
 * @throws IOException if there are problems with the http get request.
 */
private byte[] httpGet(String requestUrl) throws IOException {
  GetMethod getMethod = new GetMethod(requestUrl);
  try {
    int responseCode = httpClient.executeMethod(getMethod);
    LOG.debug("Received response code: {} for the get request on the url: {}", responseCode, requestUrl);
    byte[] response = getMethod.getResponseBody();
    if (responseCode != HttpStatus.SC_OK) {
      throw new SamzaException(String.format("Received response code: %s for get request on: %s, with message: %s.",
                                             responseCode, requestUrl, StringUtils.newStringUtf8(response)));
    }
    return response;
  } finally {
    getMethod.releaseConnection();
  }
}
 
Example 3
Source File: HttpSOAPClient.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void send(String endpoint, SOAPMessageContext messageContext) throws SOAPException, SecurityException {
    PostMethod post = null;
    try {
        post = createPostMethod(endpoint, (HttpSOAPRequestParameters) messageContext.getSOAPRequestParameters(),
                (Envelope) messageContext.getOutboundMessage());

        int result = httpClient.executeMethod(post);
        log.debug("Received HTTP status code of {} when POSTing SOAP message to {}", result, endpoint);

        if (result == HttpStatus.SC_OK) {
            processSuccessfulResponse(post, messageContext);
        } else if (result == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            processFaultResponse(post, messageContext);
        } else {
            throw new SOAPClientException("Received " + result + " HTTP response status code from HTTP request to "
                    + endpoint);
        }
    } catch (IOException e) {
        throw new SOAPClientException("Unable to send request to " + endpoint, e);
    } finally {
        if (post != null) {
            post.releaseConnection();
        }
    }
}
 
Example 4
Source File: ExtractRegular.java    From HtmlExtractor with Apache License 2.0 6 votes vote down vote up
/**
 * 从配置管理WEB服务器下载规则(json表示)
 *
 * @param url 配置管理WEB服务器下载规则的地址
 * @return json字符串
 */
private String downJson(String url) {
    // 构造HttpClient的实例
    HttpClient httpClient = new HttpClient();
    // 创建GET方法的实例
    GetMethod method = new GetMethod(url);
    try {
        // 执行GetMethod
        int statusCode = httpClient.executeMethod(method);
        LOGGER.info("响应代码:" + statusCode);
        if (statusCode != HttpStatus.SC_OK) {
            LOGGER.error("请求失败: " + method.getStatusLine());
        }
        // 读取内容
        String responseBody = new String(method.getResponseBody(), "utf-8");
        return responseBody;
    } catch (IOException e) {
        LOGGER.error("检查请求的路径:" + url, e);
    } finally {
        // 释放连接
        method.releaseConnection();
    }
    return "";
}
 
Example 5
Source File: RestFuncTHelper.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
private static Callable<Boolean> restIsStarted() {
    return new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            try {
                String resourceUrl = getResourceUrl("version");
                HttpClient client = new HttpClientBuilder().insecure(true).useSystemProperties().build();
                HttpResponse response = client.execute(new HttpGet(resourceUrl));
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    return true;
                }
            } catch (IOException e) {
            }
            return false;
        }
    };
}
 
Example 6
Source File: OxElevenCryptoProvider.java    From oxAuth with MIT License 6 votes vote down vote up
@Override
public JSONObject generateKey(Algorithm algorithm, Long expirationTime, Use use) throws Exception {
    GenerateKeyRequest request = new GenerateKeyRequest();
    request.setSignatureAlgorithm(algorithm.toString());
    request.setExpirationTime(expirationTime);
    request.setAccessToken(accessToken);

    GenerateKeyClient client = new GenerateKeyClient(generateKeyEndpoint);
    client.setRequest(request);

    GenerateKeyResponse response = client.exec();
    if (response.getStatus() == HttpStatus.SC_OK && response.getKeyId() != null) {
        return response.getJSONEntity();
    } else {
        throw new Exception(response.getEntity());
    }
}
 
Example 7
Source File: GitMetadata.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * get the URL contents, from a local cache, if possible. Only a HTTP 200 error code is
 * considered a success. Redirects are not automatically followed.
 *
 * @param url the URL to request
 * @param inflate whether to interpret the results as deflated, and inflate them
 * @return the URI contents, inflated, if requested. If the HTTP response code != 200, returns
 *     NULL
 * @throws Exception
 */
protected byte[] getURIResponseBody(URI uri, boolean inflate, HttpMessage basemsg)
        throws Exception {
    byte[] data = null;
    if (log.isDebugEnabled()) log.debug("Debug: Requesting URI '" + uri + "'");

    // set a limit of 20 Git URIs to be followed for a single URI
    uriCount++;
    if (uriCount > 20) {
        throw new Exception(
                "Too many Git URLs requested for this URI: " + uriCount + ". Aborting");
    }
    // TODO: split out the Git MetaData from the SourceCodeDisclosure class (not as a nested
    // class)
    MessageCache messagecache = MessageCache.getSingleton(parent);
    HttpMessage msg = messagecache.getMessage(uri, basemsg, false);

    if (msg.getResponseHeader().getStatusCode() != HttpStatus.SC_OK) {
        throw new FileNotFoundException(uri.getURI());
    }
    // record the URI if it came back with a 200 (see the condition above)
    if (this.urisUsed == null || this.urisUsed.equals("")) this.urisUsed = uri.getURI();
    else this.urisUsed = this.urisUsed + ", " + uri.getURI();
    data = msg.getResponseBody().getBytes();

    if (inflate) {
        return inflate(data, inflateBufferSize);
    } else return data;
}
 
Example 8
Source File: QueueInformation.java    From sequenceiq-samples with Apache License 2.0 5 votes vote down vote up
public void printQueueInfo(HttpClient client, ObjectMapper mapper, String schedulerResourceURL) {
	
	//http://sandbox.hortonworks.com:8088/ws/v1/cluster/scheduler in case of HDP
	GetMethod get = new GetMethod(schedulerResourceURL);
    get.setRequestHeader("Accept", "application/json");
    try {
        int statusCode = client.executeMethod(get);

        if (statusCode != HttpStatus.SC_OK) {
        	LOGGER.error("Method failed: " + get.getStatusLine());
        }
        
		InputStream in = get.getResponseBodyAsStream();
		
		JsonNode jsonNode = mapper.readValue(in, JsonNode.class);
		ArrayNode queues = (ArrayNode) jsonNode.path("scheduler").path("schedulerInfo").path("queues").get("queue");
		for (int i = 0; i < queues.size(); i++) {
			JsonNode queueNode = queues.get(i);						
			LOGGER.info("queueName / usedCapacity / absoluteUsedCap / absoluteCapacity / absMaxCapacity: " + 
					queueNode.findValue("queueName") + " / " +
					queueNode.findValue("usedCapacity") + " / " + 
					queueNode.findValue("absoluteUsedCapacity") + " / " + 
					queueNode.findValue("absoluteCapacity") + " / " +
					queueNode.findValue("absoluteMaxCapacity"));
		}
	} catch (IOException e) {
		LOGGER.error("Exception occured", e);
	} finally {	        
		get.releaseConnection();
	}	      
        
}
 
Example 9
Source File: OldHttpClientApi.java    From javabase with Apache License 2.0 5 votes vote down vote up
private static byte[] executeMethod(HttpMethodBase method, int timeout) throws Exception {
    InputStream in = null;
    try {
        method.addRequestHeader("Connection", "close");
        HttpClient client = new HttpClient();
        HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
        //设置连接时候一些参数
        params.setConnectionTimeout(timeout);
        params.setSoTimeout(timeout);
        params.setStaleCheckingEnabled(false);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);

        int stat =  client.executeMethod(method);
        if (stat != HttpStatus.SC_OK)
            log.error("get失败!");

        //method.getResponseBody()
        in = method.getResponseBodyAsStream();
        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }
        return baos.toByteArray();
    }
    finally {
        if (in != null) {
            in.close();
        }
    }
}
 
Example 10
Source File: ExchangeDavMethod.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return method http status code.
 *
 * @return http status code
 * @throws HttpException on error
 */
public int getResponseStatusCode() throws HttpException {
    String responseDescription = getResponse().getResponseDescription();
    if ("HTTP/1.1 201 Created".equals(responseDescription)) {
        return HttpStatus.SC_CREATED;
    } else {
        return HttpStatus.SC_OK;
    }
}
 
Example 11
Source File: UriUtils.java    From cosmic with Apache License 2.0 5 votes vote down vote up
public static void checkUrlExistence(final String url) {
    if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) {
        final HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        final HeadMethod httphead = new HeadMethod(url);
        try {
            if (httpClient.executeMethod(httphead) != HttpStatus.SC_OK) {
                throw new IllegalArgumentException("Invalid URL: " + url);
            }
        } catch (final HttpException hte) {
            throw new IllegalArgumentException("Cannot reach URL: " + url);
        } catch (final IOException ioe) {
            throw new IllegalArgumentException("Cannot reach URL: " + url);
        }
    }
}
 
Example 12
Source File: EwsExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int sendEvent(String icsBody) throws IOException {
    String itemName = UUID.randomUUID().toString() + ".EML";
    byte[] mimeContent = new Event(DRAFTS, itemName, "urn:content-classes:calendarmessage", icsBody, null, null).createMimeContent();
    if (mimeContent == null) {
        // no recipients, cancel
        return HttpStatus.SC_NO_CONTENT;
    } else {
        sendMessage(null, mimeContent);
        return HttpStatus.SC_OK;
    }
}
 
Example 13
Source File: ESManager.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Refresh.
 *
 * @param index
 *            the index
 */
public static void refresh(String index) {
    try {
        Response refrehsResponse = invokeAPI("POST", index + "/" + "_refresh", null);
        if (refrehsResponse != null && HttpStatus.SC_OK != refrehsResponse.getStatusLine().getStatusCode()) {
                LOGGER.error("Refreshing index %s failed", index, refrehsResponse);
        }
    } catch (IOException e) {
        LOGGER.error("Error in refresh ",e); 
    }
    
}
 
Example 14
Source File: BigSwitchBcfApi.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
private String checkResponse(final HttpMethodBase m, final String errorMessageBase) throws BigSwitchBcfApiException,
IllegalArgumentException{
    String customErrorMsg = null;
    if (m.getStatusCode() == HttpStatus.SC_OK) {
        String hash = "";
        if (m.getResponseHeader(HASH_MATCH) != null) {
            hash = m.getResponseHeader(HASH_MATCH).getValue();
            set_hash(hash);
        }
        return hash;
    }
    if (m.getStatusCode() == HttpStatus.SC_CONFLICT) {
        if(m instanceof GetMethod) {
            return HASH_CONFLICT;
        }
        throw new BigSwitchBcfApiException("BCF topology sync required", true);
    }
    if (m.getStatusCode() == HttpStatus.SC_SEE_OTHER) {
        isMaster = false;
        set_hash(HASH_IGNORE);
        return HASH_IGNORE;
    }
    if (m.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
        if (m instanceof DeleteMethod){
            return "";
        }
    }
    if (m.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
        customErrorMsg = " Invalid data in BCF request";
        throw new IllegalArgumentException(customErrorMsg);
    }
    String errorMessage = responseToErrorMessage(m);
    m.releaseConnection();
    S_LOGGER.error(errorMessageBase + errorMessage);
    throw new BigSwitchBcfApiException(errorMessageBase + errorMessage + customErrorMsg);
}
 
Example 15
Source File: ElasticSearchRepository.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Refresh.
 *
 * @param index
 *            the index
 */
public static void refresh(String index) {
    try {
        Response refrehsResponse = invokeAPI("POST", index + "/" + "_refresh", null);
        if (refrehsResponse != null && HttpStatus.SC_OK != refrehsResponse.getStatusLine().getStatusCode()) {
                LOGGER.error("Refreshing index %s failed", index, refrehsResponse);
        }
    } catch (IOException e) {
        LOGGER.error("Error in refresh ",e); 
    }
    
}
 
Example 16
Source File: HTTPUtils.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * @param responseCode
 * @return
 */
public static boolean verifyResponseCode(int responseCode) {
    switch (responseCode) {
        case HttpStatus.SC_OK:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
            return true;
        default:
            return false;

    }
}
 
Example 17
Source File: SourceCodeDisclosureCVE20121823.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void scan() {
    try {

        if (!getBaseMsg().getResponseHeader().isText()) {
            return; // Ignore images, pdfs, etc.
        }
        if (getAlertThreshold() != AlertThreshold.LOW
                && getBaseMsg().getResponseHeader().isJavaScript()) {
            return;
        }
        // at Low or Medium strength, do not attack URLs which returned "Not Found"
        AttackStrength attackStrength = getAttackStrength();
        if ((attackStrength == AttackStrength.LOW || attackStrength == AttackStrength.MEDIUM)
                && (getBaseMsg().getResponseHeader().getStatusCode()
                        == HttpStatus.SC_NOT_FOUND)) return;

        URI originalURI = getBaseMsg().getRequestHeader().getURI();

        // construct a new URL based on the original URL, but without any of the original
        // parameters
        String attackParam = "?-s";
        URI attackURI = createAttackUri(originalURI, attackParam);
        if (attackURI == null) {
            return;
        }
        // and send it as a GET, unauthorised.
        HttpMessage attackmsg = new HttpMessage(attackURI);
        sendAndReceive(attackmsg, false); // do not follow redirects

        if (attackmsg.getResponseHeader().getStatusCode() == HttpStatus.SC_OK) {
            // double-check: does the response contain HTML encoded PHP?
            // Ignore the case where it contains encoded HTML for now, since thats not a source
            // code disclosure anyway
            // (HTML is always sent back to the web browser)
            String responseBody = new String(attackmsg.getResponseBody().getBytes());
            String responseBodyDecoded = new Source(responseBody).getRenderer().toString();

            Matcher matcher1 = PHP_PATTERN1.matcher(responseBodyDecoded);
            Matcher matcher2 = PHP_PATTERN2.matcher(responseBodyDecoded);
            boolean match1 = matcher1.matches();
            boolean match2 = matcher2.matches();

            if ((!responseBody.equals(responseBodyDecoded)) && (match1 || match2)) {

                if (log.isDebugEnabled()) {
                    log.debug("Source Code Disclosure alert for: " + originalURI.getURI());
                }

                String sourceCode = null;
                if (match1) {
                    sourceCode = matcher1.group(1);
                } else {
                    sourceCode = matcher2.group(1);
                }

                // bingo.
                newAlert()
                        .setConfidence(Alert.CONFIDENCE_MEDIUM)
                        .setDescription(
                                Constant.messages.getString(
                                        "ascanbeta.sourcecodedisclosurecve-2012-1823.desc"))
                        .setOtherInfo(sourceCode)
                        .setSolution(
                                Constant.messages.getString(
                                        "ascanbeta.sourcecodedisclosurecve-2012-1823.soln"))
                        .setMessage(attackmsg)
                        .raise();
            }
        }
    } catch (Exception e) {
        log.error(
                "Error scanning a Host for Source Code Disclosure via CVE-2012-1823: "
                        + e.getMessage(),
                e);
    }
}
 
Example 18
Source File: SystemRegistrationWorker.java    From olat with Apache License 2.0 4 votes vote down vote up
protected boolean doTheWork(String registrationData, String url, String version) {
    String registrationKey = propertyService.getStringProperty(PropertyLocator.SYSTEM_REG_SECRET_KEY);
    boolean regStatus = false;
    if (StringHelper.containsNonWhitespace(registrationData)) {
        // only send when there is something to send
        final HttpClient client = HttpClientFactory.getHttpClientInstance();
        client.getParams().setParameter("http.useragent", "OLAT Registration Agent ; " + version);

        log.info("URL:" + url, null);
        final PutMethod method = new PutMethod(url);
        if (registrationKey != null) {
            // updating
            method.setRequestHeader("Authorization", registrationKey);
            if (log.isDebugEnabled()) {
                log.debug("Authorization: " + registrationKey, null);
            } else {
                log.debug("Authorization: EXISTS", null);
            }
        } else {
            log.info("Authorization: NONE", null);
        }
        method.setRequestHeader("Content-Type", "application/xml; charset=utf-8");
        try {
            method.setRequestEntity(new StringRequestEntity(registrationData, "application/xml", "UTF8"));
            client.executeMethod(method);
            final int status = method.getStatusCode();
            if (status == HttpStatus.SC_NOT_MODIFIED || status == HttpStatus.SC_OK) {
                log.info("Successfully registered OLAT installation on olat.org server, thank you for your support!", null);
                registrationKey = method.getResponseBodyAsString();
                propertyService.setProperty(PropertyLocator.SYSTEM_REG_SECRET_KEY, registrationKey);
                regStatus = true;
            } else if (method.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                log.error("File could be created not on registration server::" + method.getStatusLine().toString(), null);
                regStatus = false;
            } else if (method.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
                log.info(method.getResponseBodyAsString() + method.getStatusText());
                regStatus = false;
            } else {
                log.error("Unexpected HTTP Status::" + method.getStatusLine().toString() + " during registration call", null);
                regStatus = false;
            }
        } catch (final Exception e) {
            log.error("Unexpected exception during registration call", e);
            regStatus = false;
        }
    } else {
        log.warn(
                "****************************************************************************************************************************************************************************",
                null);
        log.warn(
                "* This OLAT installation is not registered. Please, help us with your statistical data and register your installation under Adminisration - Systemregistration. THANK YOU! *",
                null);
        log.warn(
                "****************************************************************************************************************************************************************************",
                null);
    }
    return regStatus;
}
 
Example 19
Source File: ClientSecretTest.java    From JavaChatterRESTApi with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ExecuteMethodAnswer(String response) {
    this(response, HttpStatus.SC_OK);
}
 
Example 20
Source File: GoogleAjaxSearcher.java    From search with Apache License 2.0 4 votes vote down vote up
@Override
public SearchResult search(String keyword, int page) {
    int pageSize = 8;
    //谷歌搜索结果每页大小为8,start参数代表的是返回结果的开始数
    //如获取第一页则start=0,第二页则start=10,第三页则start=20,以此类推,抽象出模式:(page-1)*pageSize
    String url = "http://ajax.googleapis.com/ajax/services/search/web?start="+(page-1)*pageSize+"&rsz=large&v=1.0&q=" + keyword;
    
    SearchResult searchResult = new SearchResult();
    searchResult.setPage(page);
    List<Webpage> webpages = new ArrayList<>();
    try {
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);

        httpClient.executeMethod(getMethod);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler());

        int statusCode = httpClient.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("搜索失败: " + getMethod.getStatusLine());
            return null;
        }
        InputStream in = getMethod.getResponseBodyAsStream();
        byte[] responseBody = Tools.readAll(in);
        String response = new String(responseBody, "UTF-8");
        LOG.debug("搜索返回数据:" + response);
        JSONObject json = new JSONObject(response);
        String totalResult = json.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount");
        int totalResultCount = Integer.parseInt(totalResult);
        LOG.info("搜索返回记录数: " + totalResultCount);
        searchResult.setTotal(totalResultCount);

        JSONArray results = json.getJSONObject("responseData").getJSONArray("results");

        LOG.debug("搜索结果:");
        for (int i = 0; i < results.length(); i++) {
            Webpage webpage = new Webpage();
            JSONObject result = results.getJSONObject(i);
            //提取标题
            String title = result.getString("titleNoFormatting");
            LOG.debug("标题:" + title);
            webpage.setTitle(title);
            //提取摘要
            String summary = result.get("content").toString();
            summary = summary.replaceAll("<b>", "");
            summary = summary.replaceAll("</b>", "");
            summary = summary.replaceAll("\\.\\.\\.", "");
            LOG.debug("摘要:" + summary);
            webpage.setSummary(summary);
            //从URL中提取正文
            String _url = result.get("url").toString();
            webpage.setUrl(_url);
            String content = Tools.getHTMLContent(_url);
            LOG.debug("正文:" + content);
            webpage.setContent(content);
            webpages.add(webpage);
        }
    } catch (IOException | JSONException | NumberFormatException e) {
        LOG.error("执行搜索失败:", e);
    }
    searchResult.setWebpages(webpages);
    return searchResult;
}