Java Code Examples for org.apache.http.HttpStatus#SC_NOT_MODIFIED

The following examples show how to use org.apache.http.HttpStatus#SC_NOT_MODIFIED . 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: HurlStack.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if a response message contains a body.
 *
 * @param requestMethod request method
 * @param responseCode response status code
 * @return whether the response has a body
 * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
 */
/*
 * 检查请求结果 Response 是否存在 body
 *
 * 规则是这样的,要 必须 满足 5 点:
 * 1. 请求方法不是 HEAD 方法
 * 2. Status > 100
 * 3. Status < 200
 * 4. Status != 204
 * 5. Status != 304
 *
 */
private static boolean hasResponseBody(int requestMethod, int responseCode) {
    return requestMethod != Request.Method.HEAD &&
            !(HttpStatus.SC_CONTINUE <= responseCode && responseCode < HttpStatus.SC_OK) &&
            responseCode != HttpStatus.SC_NO_CONTENT &&
            responseCode != HttpStatus.SC_NOT_MODIFIED;
}
 
Example 2
Source File: DatasyncDirectory.java    From datasync with MIT License 6 votes vote down vote up
/**
 * Returns the contents of the given directory within this DatasyncDirectory.
 * @param path the path to the directory of interest, relative to this DatasyncDirectory,
 *             e.g. '/completed' or '/completed/y/m/d/signatures/'
 * @return a list of the contents within the directory
 */
public List<String> lsDirectory(String path) throws URISyntaxException, IOException {
    URI uri = baseUri.setPath(baseFolder + path).build();
    try(CloseableHttpResponse response = http.get(uri, ContentType.APPLICATION_JSON.getMimeType())) {
        int status = response.getStatusLine().getStatusCode();
        if (status == HttpStatus.SC_OK || status == HttpStatus.SC_NOT_MODIFIED) {
            @SuppressWarnings("unchecked")
            ArrayList<String> ret =
                mapper.readValue(response.getEntity().getContent(), ArrayList.class);
            return ret;
        } else {
            // it isn't a show-stopper to be unable to read directories
            return new ArrayList<>();
        }
    }
}
 
Example 3
Source File: DriverTest.java    From esigate with Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * Test bug Bug 142 (2nd case).
 * <p>
 * 0000142: Error with 304 backend responses
 * <p>
 * NPE in Apache HTTP CLient cache
 * <p>
 * See http://www.esigate.org/mantisbt/view.php?id=142
 * 
 * @throws Exception
 */
public void testBug142SecondCase() throws Exception {
    Properties properties = new Properties();
    properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://www.foo.com/");
    properties.put(Parameters.TTL.getName(), "43200");

    HttpResponse response =
            new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_MODIFIED, "Not Modified");
    response.addHeader("Etag", "a86ecd6cc6d361776ed05f063921aa34");
    response.addHeader("Date", "Thu, 13 Dec 2012 08:55:37 GMT");
    response.addHeader("Cache-Control", "max-age=2051, public");
    response.addHeader("Expires", "Thu, 13 Dec 2012 09:29:48 GMT");
    response.addHeader("Vary", "Accept-Encoding");

    mockConnectionManager.setResponse(response);
    Driver driver = createMockDriver(properties, mockConnectionManager);

    // First request
    request = TestUtils.createIncomingRequest("http://www.bar142-2.com/foobar142-2/");
    request.addHeader("If-None-Match", "a86ecd6cc6d361776ed05f063921aa34");
    CloseableHttpResponse driverResponse = driver.proxy("/foobar142-2/", request.build());
    assertEquals(HttpStatus.SC_NOT_MODIFIED, driverResponse.getStatusLine().getStatusCode());
}
 
Example 4
Source File: DavCollection.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * This method requests the GET method for the DAV. If the ETag value is non-null, add an If-None-Match header.
 * @param pathValue Path
 * @param eTag ETag Value
 * @return WebDAV object that contains the stream If no update, then return null.
 * @throws DaoException Exception thrown
 */
public WebDAV getStreamWebDAV(String pathValue, String eTag) throws DaoException {
    String url = UrlUtils.append(this.getPath(), pathValue);
    DcResponse res = null;
    int statusCode = 0;
    try {
        res = RestAdapterFactory.create(this.accessor).get(url, "application/octet-stream", eTag);
        statusCode = res.getStatusCode();
    } catch (DaoException e) {
        // TODO 300系をエラーとして処理することの可否は検討が必要
        /** TODO Propriety of treating it as an error 300 system needs to be considered. */
        if (Integer.parseInt(e.getCode()) != HttpStatus.SC_NOT_MODIFIED) {
            throw e;
        } else {
            statusCode = Integer.parseInt(e.getCode());
        }
    }

    WebDAV webDAV = new WebDAV();
    webDAV.setStatusCode(statusCode);
    if (statusCode < REDIRECTION_CODE) {
        webDAV.setResHeaders(res.getHeaderList());
        // レスポンスボディを取得
        /** Get the response body. */
        webDAV.setStreamBody(res.bodyAsStream());
    }

    return webDAV;
}
 
Example 5
Source File: DefaultLeaderMetaFetcher.java    From hermes with Apache License 2.0 5 votes vote down vote up
@Override
public Meta fetchMetaInfo(MetaInfo metaInfo) {
	if (metaInfo == null) {
		return null;
	}

	long start = System.currentTimeMillis();
	try {
		if (Networks.forIp().getLocalHostAddress().equals(metaInfo.getHost())
		      && m_config.getMetaServerPort() == metaInfo.getPort()) {
			return null;
		}

		String url = String.format("http://%s:%s/meta/complete", metaInfo.getHost(), metaInfo.getPort());
		Meta meta = m_metaHolder.getMeta();

		if (meta != null) {
			url += "?version=" + meta.getVersion();
		}

		HttpResponse response = Request.Get(url)//
		      .connectTimeout(m_config.getFetcheMetaFromLeaderConnectTimeout())//
		      .socketTimeout(m_config.getFetcheMetaFromLeaderReadTimeout())//
		      .execute()//
		      .returnResponse();

		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			String responseContent = EntityUtils.toString(response.getEntity());
			return JSON.parseObject(responseContent, Meta.class);
		} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
			return meta;
		}

	} catch (Exception e) {
		log.error("Failed to fetch meta from leader({}:{})", metaInfo.getHost(), metaInfo.getPort(), e);
	} finally {
		log.info("Fetch leader info cost {}ms", (System.currentTimeMillis() - start));
	}
	return null;
}
 
Example 6
Source File: DavCollection.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to request the GET method for DAV. If the ETag value is non-null, add an If-None-Match
 * header.
 * @param pathValue Path
 * @param eTag ETag value
 * @param charset character code
 * @return WebDAV objects that contain the string If no update, then return null.
 * @throws DaoException DAO exception
 */
public WebDAV getStringWebDAV(String pathValue, String eTag, String charset) throws DaoException {
    String url = UrlUtils.append(this.getPath(), pathValue);
    DcResponse res = null;
    int statusCode = 0;
    try {
        res = RestAdapterFactory.create(this.accessor).get(url, "text/plain", eTag);
        statusCode = res.getStatusCode();
    } catch (DaoException e) {
        // TODO 300系をエラーとして処理することの可否は検討が必要
        /** TODO Propriety of treating it as an error 300 system needs to be considered. */
        if (Integer.parseInt(e.getCode()) != HttpStatus.SC_NOT_MODIFIED) {
            throw e;
        } else {
            statusCode = Integer.parseInt(e.getCode());
        }
    }

    WebDAV webDAV = new WebDAV();
    webDAV.setStatusCode(statusCode);
    if (statusCode < REDIRECTION_CODE) {
        webDAV.setResHeaders(res.getHeaderList());
        // レスポンスボディを取得
        /** Get the response body. */
        webDAV.setStringBody(res.bodyAsString(charset));
    }

    return webDAV;
}
 
Example 7
Source File: HttpClientRequestExecutorTest.java    From esigate with Apache License 2.0 4 votes vote down vote up
/**
 * Test that we do not return a 304 to a non-conditional request when ttl forced.
 * 
 * @throws Exception
 */
public void testDoNotReturn304ForNonConditionalRequestWhenTtlSet() throws Exception {

    properties = new PropertiesBuilder() //
            .set(Parameters.REMOTE_URL_BASE, "http://localhost:8080") //
            .set(Parameters.TTL, 1) //
            .set(Parameters.X_CACHE_HEADER, true) //
            .build();

    createHttpClientRequestExecutor();

    DriverRequest originalRequest = TestUtils.createDriverRequest(driver);
    OutgoingRequest request1 =
            httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", false);
    request1.addHeader("If-None-Match", "etag");

    HttpResponse response =
            new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
                    HttpStatus.SC_NOT_MODIFIED, "Not modified"));
    response.addHeader("Date", "Mon, 10 Dec 2012 19:37:52 GMT");
    response.addHeader("Etag", "etag");
    response.addHeader("Cache-Control", "max-age=0");
    mockConnectionManager.setResponse(response);

    // First request returns a 304
    HttpResponse result1 = httpClientRequestExecutor.execute(request1);
    assertEquals(HttpStatus.SC_NOT_MODIFIED, result1.getStatusLine().getStatusCode());
    assertTrue(result1.getFirstHeader("X-cache").getValue(), result1.getFirstHeader("X-cache").getValue()
            .startsWith("MISS"));
    assertNull(result1.getEntity());

    // Second request should use the cache and return a
    // 304 again
    HttpResponse result2 = httpClientRequestExecutor.execute(request1);
    assertEquals(HttpStatus.SC_NOT_MODIFIED, result1.getStatusLine().getStatusCode());
    assertTrue(result2.getFirstHeader("X-cache").getValue(), result2.getFirstHeader("X-cache").getValue()
            .startsWith("HIT"));
    assertNull(result2.getEntity());

    HttpResponse response2 =
            new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_OK, "Ok"));
    response2.addHeader("Date", "Mon, 10 Dec 2012 19:37:52 GMT");
    response2.addHeader("Etag", "etag");
    response2.addHeader("Cache-Control", "max-age=0");
    response2.setEntity(new StringEntity("test"));
    mockConnectionManager.setResponse(response2);

    // Third request not conditional ! Should call backend server as we
    // don't have the entity in the cache.
    OutgoingRequest request2 =
            httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", false);
    HttpResponse result3 = httpClientRequestExecutor.execute(request2);
    assertEquals(HttpStatus.SC_OK, result3.getStatusLine().getStatusCode());
    assertTrue(result3.getFirstHeader("X-cache").getValue(), !result3.getFirstHeader("X-cache").getValue()
            .startsWith("HIT"));
    assertNotNull(result3.getEntity());
}
 
Example 8
Source File: DavCollection.java    From io with Apache License 2.0 4 votes vote down vote up
/**
 * This method requests the GET method for the DAV and retruns in WebDAV object.
 * @param pathValue Path
 * @param charset Character Code
 * @return WebDAV GET WebDAV object that contains a string
 * @throws DaoException Exception thrown
 */
public WebDAV getStringWebDAV(String pathValue, String charset) throws DaoException {
    String url = UrlUtils.append(this.getPath(), pathValue);

    // まずはキャッシュから検索する
    /** First search from cache. */
    CacheMap cm = this.accessor.getContext().getCacheMap();
    CacheEntry ce = cm.search(url);

    IRestAdapter rest = RestAdapterFactory.create(this.accessor);
    DcResponse res;
    try {
        if (ce == null) {
            // キャッシュになければ新規取得
            /** If not found in cache, then acquire. */
            res = rest.get(url, "text/plain");
        } else {
            // キャッシュにあれば、IF_NONE_MATCHにETag指定
            /** If you are on the cache and ETag specified in IF_NONE_MATCH. */
            res = rest.get(url, "text/plain", ce.getEtag());
        }
    } catch (DaoException e) {
        // 304 NOT_MODIFIEDの場合は、キャッシュの値を返却する
        /** In the case of 304 NOT_MODIFIED, to return the value of the cache. */
        if (Integer.parseInt(e.getCode()) == HttpStatus.SC_NOT_MODIFIED && ce != null) {
            WebDAV webDAVCache = new WebDAV();
            webDAVCache.setStringBody(ce.getBody());
            webDAVCache.setResHeaders(ce.getHeaders());
            webDAVCache.setStatusCode(HttpStatus.SC_NOT_MODIFIED);
            return webDAVCache;
        }
        throw e;
    }

    // レスポンスボディを取得
    /** Get the response body. */
    String body = res.bodyAsString(charset);

    WebDAV webDAV = new WebDAV();
    webDAV.setStringBody(body);
    webDAV.setResHeaders(res.getHeaderList());
    webDAV.setStatusCode(res.getStatusCode());

    if (ce == null) {
        // キャッシュになければ新規にキャッシュに保存
        /** Save the new one to cache. */
        cm.appendEntry(new CacheEntry(url, res.getHeaderList(), body));
    } else {
        // キャッシュにあれば、キャッシュ情報を更新
        /** If was present on the cache, then update its new value. */
        ce.setHeaders(res.getHeaderList());
        ce.setBody(body);
    }
    return webDAV;
}
 
Example 9
Source File: CachingHttpClient.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
HttpResponse revalidateCacheEntry(HttpHost target, HttpRequest request,
		HttpContext context, HttpCacheEntry cacheEntry) throws IOException,
		ProtocolException {
	HttpRequest conditionalRequest = conditionalRequestBuilder
			.buildConditionalRequest(request, cacheEntry);

	Date requestDate = getCurrentDate();
	HttpResponse backendResponse = backend.execute(target,
			conditionalRequest, context);
	Date responseDate = getCurrentDate();

	final Header entryDateHeader = cacheEntry.getFirstHeader("Date");
	final Header responseDateHeader = backendResponse
			.getFirstHeader("Date");
	if (entryDateHeader != null && responseDateHeader != null) {
		try {
			Date entryDate = DateUtils
					.parseDate(entryDateHeader.getValue());
			Date respDate = DateUtils.parseDate(responseDateHeader
					.getValue());
			if (respDate.before(entryDate)) {
				HttpRequest unconditional = conditionalRequestBuilder
						.buildUnconditionalRequest(request, cacheEntry);
				requestDate = getCurrentDate();
				backendResponse = backend.execute(target, unconditional,
						context);
				responseDate = getCurrentDate();
			}
		} catch (DateParseException e) {
			// either backend response or cached entry did not have a valid
			// Date header, so we can't tell if they are out of order
			// according to the origin clock; thus we can skip the
			// unconditional retry recommended in 13.2.6 of RFC 2616.
		}
	}

	backendResponse.addHeader("Via", generateViaHeader(backendResponse));

	int statusCode = backendResponse.getStatusLine().getStatusCode();
	if (statusCode == HttpStatus.SC_NOT_MODIFIED
			|| statusCode == HttpStatus.SC_OK) {
		cacheUpdates.getAndIncrement();
		setResponseStatus(context, CacheResponseStatus.VALIDATED);
	}

	if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
		HttpCacheEntry updatedEntry = responseCache.updateCacheEntry(
				target, request, cacheEntry, backendResponse, requestDate,
				responseDate);
		if (suitabilityChecker.isConditional(request)
				&& suitabilityChecker.allConditionalsMatch(request,
						updatedEntry, new Date())) {
			return responseGenerator
					.generateNotModifiedResponse(updatedEntry);
		}
		return responseGenerator.generateResponse(updatedEntry);
	}

	return handleBackendResponse(target, conditionalRequest, requestDate,
			responseDate, backendResponse);
}
 
Example 10
Source File: GtfsUpdatedModule.java    From core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Gets the GTFS file via http from the configured URL urlStr and stores it
 * in the configured directory dirName.
 * <p>
 * If file on web server last modified time is not newer than the local file
 * then the file will not actually be downloaded.
 * <p>
 * If file is downloaded then users and e-mailed.
 */
public static void get() {
	logger.info("Checking to see if GTFS should be downloaded "
			+ "because it was modified. {}", url.getValue());
	
	// Construct the getter
	HttpGetFile httpGetFile = 
			new HttpGetFile(url.getValue(), dirName.getValue());

	// If file hasn't been modified then don't want to download it
	// since it can be large. Therefore determine age of previously 
	// downloaded file and use If-Modified-Since to only actually
	// get the file if it is newer on the web server
	File file = new File(httpGetFile.getFullFileName());
	boolean fileAlreadyExists = file.exists();
	if (fileAlreadyExists) {
		// Get the last modified time of the local file. Add 10 minutes
		// since the web server might be load balanced and the files
		// on the different servers might have slightly different last 
		// modified times. To make sure that don't keep downloading 
		// from the different servers until get the file with most 
		// recent modified time add 10 minutes to the time to indicate
		// that as long as the local file is within 10 minutes of the
		// remote file that it is ok.
		long lastModified = file.lastModified() + 10*Time.MS_PER_MIN;

		httpGetFile.addRequestHeader("If-Modified-Since",
				Time.httpDate(lastModified));
		
		logger.debug("The file {} already exists so using "
				+ "If-Modified-Since header of \"{}\" or {} msec.", 
				httpGetFile.getFullFileName(), Time.httpDate(lastModified), 
				lastModified);
	}
	
	try {
		// Actually get the file from web server
		int httpResponseCode = httpGetFile.getFile();

		// If got a new file (instead of getting a NOT MODIFIED
		// response) then send message to those monitoring so that
		// the GTFS file can be processed.
		if (httpResponseCode == HttpStatus.SC_OK) {
			if (fileAlreadyExists)
				logger.info("Got remote file because version on web server "
						+ "is newer. Url={} dir={}", 
						httpGetFile.getFullFileName(), dirName.getValue());
			else
				logger.info("Got remote file because didn't have a local "
						+ "copy of it. Url={} dir={}",
						httpGetFile.getFullFileName(), dirName.getValue());
			
			// Email message
			String subject = "GTFS file was updated for " 
					+ AgencyConfig.getAgencyId();
			String message = "For " + AgencyConfig.getAgencyId() 
					+ " the GTFS file " + url.getValue() 
					+ " was updated so was downloaded to " 
					+ httpGetFile.getFullFileName();
			emailSender.send(MonitorBase.recipientsGlobal(), subject, 
					message);
			
			// Make copy of GTFS zip file in separate directory for archival
			archive(httpGetFile.getFullFileName());
		} else if (httpResponseCode == HttpStatus.SC_NOT_MODIFIED) {
			// If not modified then don't need to do anything
			logger.info("Remote GTFS file {} not updated (got "
					+ "HTTP NOT_MODIFIED status 304) since the local "
					+ "one  at {} has last modified date of {}",
					url.getValue(), httpGetFile.getFullFileName(), 
					new Date(file.lastModified()));
		} else {
			// Got unexpected response so log issue
			logger.error("Error retrieving remote GTFS file {} . Http "
					+ "response code={}", 
					url.getValue(), httpResponseCode);
		}
	} catch (IOException e) {
		logger.error("Error retrieving {} . {}", 
				url.getValue(), e.getMessage());
	}
}
 
Example 11
Source File: CachedHttpResponseGenerator.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * Generate a 304 - Not Modified response from a {@link CacheEntry}. This
 * should be used to respond to conditional requests, when the entry exists
 * or has been revalidated.
 * 
 * @param entry
 * @return
 */
HttpResponse generateNotModifiedResponse(HttpCacheEntry entry) {

	HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
			HttpStatus.SC_NOT_MODIFIED, "Not Modified");

	// The response MUST include the following headers
	// (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)

	// - Date, unless its omission is required by section 14.8.1
	Header dateHeader = entry.getFirstHeader("Date");
	if (dateHeader == null) {
		dateHeader = new BasicHeader("Date",
				DateUtils.formatDate(new Date()));
	}
	response.addHeader(dateHeader);

	// - ETag and/or Content-Location, if the header would have been sent
	// in a 200 response to the same request
	Header etagHeader = entry.getFirstHeader("ETag");
	if (etagHeader != null) {
		response.addHeader(etagHeader);
	}

	Header contentLocationHeader = entry.getFirstHeader("Content-Location");
	if (contentLocationHeader != null) {
		response.addHeader(contentLocationHeader);
	}

	// - Expires, Cache-Control, and/or Vary, if the field-value might
	// differ from that sent in any previous response for the same
	// variant
	Header expiresHeader = entry.getFirstHeader("Expires");
	if (expiresHeader != null) {
		response.addHeader(expiresHeader);
	}

	Header cacheControlHeader = entry.getFirstHeader("Cache-Control");
	if (cacheControlHeader != null) {
		response.addHeader(cacheControlHeader);
	}

	Header varyHeader = entry.getFirstHeader("Vary");
	if (varyHeader != null) {
		response.addHeader(varyHeader);
	}

	return response;
}
 
Example 12
Source File: HttpUtils_Deprecated.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
public static String getResponseFromGetUrl(String url, String logininfo,
                                           String params) throws Exception {
    Log.d("Chen", "url--" + url);
    if (null != params && !"".equals(params)) {

        url = url + "?";

        String[] paramarray = params.split(",");

        for (int index = 0; null != paramarray && index < paramarray.length; index++) {

            if (index == 0) {

                url = url + paramarray[index];
            } else {

                url = url + "&" + paramarray[index];
            }

        }

    }

    HttpGet httpRequest = new HttpGet(url);

    httpRequest.addHeader("Cookie", logininfo);

    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = 30000;
    HttpConnectionParams.setConnectionTimeout(httpParameters,
            timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 30000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

    // DefaultHttpClient httpclient = new DefaultHttpClient();
    StringBuffer sb = new StringBuffer();


    try {
        HttpResponse httpResponse = httpclient.execute(httpRequest);

        String inputLine = "";
        // Log.d("Chen","httpResponse.getStatusLine().getStatusCode()"+httpResponse.getStatusLine().getStatusCode());
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

            InputStreamReader is = new InputStreamReader(httpResponse
                    .getEntity().getContent());
            BufferedReader in = new BufferedReader(is);
            while ((inputLine = in.readLine()) != null) {

                sb.append(inputLine);
            }

            in.close();

        } else if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
            return "not_modify";
        }
    } catch (Exception e) {
        e.printStackTrace();
        return "net_error";
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

    return sb.toString();

}
 
Example 13
Source File: HttpGetter.java    From commafeed with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @param url
 *            the url to retrive
 * @param lastModified
 *            header we got last time we queried that url, or null
 * @param eTag
 *            header we got last time we queried that url, or null
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 * @throws NotModifiedException
 *             if the url hasn't changed since we asked for it last time
 */
public HttpResult getBinary(String url, String lastModified, String eTag, int timeout) throws ClientProtocolException, IOException,
		NotModifiedException {
	HttpResult result = null;
	long start = System.currentTimeMillis();

	CloseableHttpClient client = newClient(timeout);
	CloseableHttpResponse response = null;
	try {
		HttpGet httpget = new HttpGet(url);
		HttpClientContext context = HttpClientContext.create();

		httpget.addHeader(HttpHeaders.ACCEPT_LANGUAGE, ACCEPT_LANGUAGE);
		httpget.addHeader(HttpHeaders.PRAGMA, PRAGMA_NO_CACHE);
		httpget.addHeader(HttpHeaders.CACHE_CONTROL, CACHE_CONTROL_NO_CACHE);
		httpget.addHeader(HttpHeaders.USER_AGENT, userAgent);

		if (lastModified != null) {
			httpget.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified);
		}
		if (eTag != null) {
			httpget.addHeader(HttpHeaders.IF_NONE_MATCH, eTag);
		}

		try {
			response = client.execute(httpget, context);
			int code = response.getStatusLine().getStatusCode();
			if (code == HttpStatus.SC_NOT_MODIFIED) {
				throw new NotModifiedException("'304 - not modified' http code received");
			} else if (code >= 300) {
				throw new HttpResponseException(code, "Server returned HTTP error code " + code);
			}

		} catch (HttpResponseException e) {
			if (e.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
				throw new NotModifiedException("'304 - not modified' http code received");
			} else {
				throw e;
			}
		}
		Header lastModifiedHeader = response.getFirstHeader(HttpHeaders.LAST_MODIFIED);
		String lastModifiedHeaderValue = lastModifiedHeader == null ? null : StringUtils.trimToNull(lastModifiedHeader.getValue());
		if (lastModifiedHeaderValue != null && StringUtils.equals(lastModified, lastModifiedHeaderValue)) {
			throw new NotModifiedException("lastModifiedHeader is the same");
		}

		Header eTagHeader = response.getFirstHeader(HttpHeaders.ETAG);
		String eTagHeaderValue = eTagHeader == null ? null : StringUtils.trimToNull(eTagHeader.getValue());
		if (eTag != null && StringUtils.equals(eTag, eTagHeaderValue)) {
			throw new NotModifiedException("eTagHeader is the same");
		}

		HttpEntity entity = response.getEntity();
		byte[] content = null;
		String contentType = null;
		if (entity != null) {
			content = EntityUtils.toByteArray(entity);
			if (entity.getContentType() != null) {
				contentType = entity.getContentType().getValue();
			}
		}

		String urlAfterRedirect = url;
		if (context.getRequest() instanceof HttpUriRequest) {
			HttpUriRequest req = (HttpUriRequest) context.getRequest();
			HttpHost host = context.getTargetHost();
			urlAfterRedirect = req.getURI().isAbsolute() ? req.getURI().toString() : host.toURI() + req.getURI();
		}

		long duration = System.currentTimeMillis() - start;
		result = new HttpResult(content, contentType, lastModifiedHeaderValue, eTagHeaderValue, duration, urlAfterRedirect);
	} finally {
		IOUtils.closeQuietly(response);
		IOUtils.closeQuietly(client);
	}
	return result;
}
 
Example 14
Source File: ServiceCombConfigClient.java    From spring-cloud-huawei with Apache License 2.0 4 votes vote down vote up
/**
 * @param serviceCombConfigProperties
 * @param project
 * @return
 * @throws RemoteOperationException
 */
public Map<String, String> loadFromKie(ServiceCombConfigProperties serviceCombConfigProperties,
    String project, boolean isWatch)
    throws RemoteOperationException {
  Response response = null;
  Map<String, String> result = new HashMap<>();
  try {
    String stringBuilder = configCenterConfig.getUrl()
        + "/"
        + ConfigConstants.DEFAULT_KIE_API_VERSION
        + "/"
        + project
        + "/kie/kv?label=app:"
        + serviceCombConfigProperties.getAppName()
        + "&revision="
        + revision;
    if (isWatch && !isFirst.get()) {
      stringBuilder +=
          "&wait=" + serviceCombConfigProperties.getWatch().getPollingWaitTimeInSeconds() + "s";
    }
    isFirst.compareAndSet(true, false);
    response = httpTransport.sendGetRequest(stringBuilder);
    if (response == null) {
      return null;
    }
    if (response.getStatusCode() == HttpStatus.SC_OK) {
      revision = response.getHeader("X-Kie-Revision");
      LOGGER.debug(response.getContent());
      KVResponse allConfigList = JsonUtils.OBJ_MAPPER
          .readValue(response.getContent(), KVResponse.class);
      return KieUtil.getConfigByLabel(serviceCombConfigProperties, allConfigList);
    } else if (response.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
      LOGGER.info(response.getStatusMessage());
      return null;
    } else if (response.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
      return null;
    } else {
      throw new RemoteOperationException(
          "read response failed. status:" + response.getStatusCode() + "; message:" + response
              .getStatusMessage() + "; content:" + response.getContent());
    }
  } catch (Exception e) {
    configCenterConfig.toggle();
    throw new RemoteOperationException("read response failed. " + response, e);
  }
}
 
Example 15
Source File: HurlStack.java    From TitanjumNote with Apache License 2.0 3 votes vote down vote up
/**
 * Checks if a response message contains a body.
 * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
 * @param requestMethod request method
 * @param responseCode response status code
 * @return whether the response has a body
 */
private static boolean hasResponseBody(int requestMethod, int responseCode) {
    return requestMethod != Request.Method.HEAD
        && !(HttpStatus.SC_CONTINUE <= responseCode && responseCode < HttpStatus.SC_OK)
        && responseCode != HttpStatus.SC_NO_CONTENT
        && responseCode != HttpStatus.SC_NOT_MODIFIED;
}
 
Example 16
Source File: HurlStack.java    From product-emm with Apache License 2.0 3 votes vote down vote up
/**
 * Checks if a response message contains a body.
 * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
 * @param requestMethod request method
 * @param responseCode response status code
 * @return whether the response has a body
 */
private static boolean hasResponseBody(int requestMethod, int responseCode) {
    return requestMethod != Request.Method.HEAD
        && !(HttpStatus.SC_CONTINUE <= responseCode && responseCode < HttpStatus.SC_OK)
        && responseCode != HttpStatus.SC_NO_CONTENT
        && responseCode != HttpStatus.SC_NOT_MODIFIED;
}
 
Example 17
Source File: HurlStack.java    From DaVinci with Apache License 2.0 3 votes vote down vote up
/**
 * Checks if a response message contains a body.
 * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
 * @param requestMethod request method
 * @param responseCode response status code
 * @return whether the response has a body
 */
private static boolean hasResponseBody(int requestMethod, int responseCode) {
    return requestMethod != Method.HEAD
        && !(HttpStatus.SC_CONTINUE <= responseCode && responseCode < HttpStatus.SC_OK)
        && responseCode != HttpStatus.SC_NO_CONTENT
        && responseCode != HttpStatus.SC_NOT_MODIFIED;
}
 
Example 18
Source File: HurlStack.java    From volley_demo with Apache License 2.0 3 votes vote down vote up
/**
 * Checks if a response message contains a body.
 * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
 * @param requestMethod request method
 * @param responseCode response status code
 * @return whether the response has a body
 */
private static boolean hasResponseBody(int requestMethod, int responseCode) {
    return requestMethod != Request.Method.HEAD
        && !(HttpStatus.SC_CONTINUE <= responseCode && responseCode < HttpStatus.SC_OK)
        && responseCode != HttpStatus.SC_NO_CONTENT
        && responseCode != HttpStatus.SC_NOT_MODIFIED;
}
 
Example 19
Source File: HurlStack.java    From pearl with Apache License 2.0 3 votes vote down vote up
/**
 * Checks if a response message contains a body.
 * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
 * @param requestMethod request method
 * @param responseCode response status code
 * @return whether the response has a body
 */
private static boolean hasResponseBody(int requestMethod, int responseCode) {
    return requestMethod != Request.Method.HEAD
        && !(HttpStatus.SC_CONTINUE <= responseCode && responseCode < HttpStatus.SC_OK)
        && responseCode != HttpStatus.SC_NO_CONTENT
        && responseCode != HttpStatus.SC_NOT_MODIFIED;
}
 
Example 20
Source File: HurlStack.java    From jus with Apache License 2.0 3 votes vote down vote up
/**
 * Checks if a response message contains a body.
 * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
 * @param requestMethod request method
 * @param responseCode response status code
 * @return whether the response has a body
 */
private static boolean hasResponseBody(int requestMethod, int responseCode) {
    return requestMethod != Request.Method.HEAD
        && !(HttpStatus.SC_CONTINUE <= responseCode && responseCode < HttpStatus.SC_OK)
        && responseCode != HttpStatus.SC_NO_CONTENT
        && responseCode != HttpStatus.SC_NOT_MODIFIED;
}