本文整理汇总了Java中org.apache.http.client.utils.DateUtils类的典型用法代码示例。如果您正苦于以下问题:Java DateUtils类的具体用法?Java DateUtils怎么用?Java DateUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DateUtils类属于org.apache.http.client.utils包,在下文中一共展示了DateUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: pollInternal
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
public void pollInternal() {
HttpHeaders requestHeaders = new HttpHeaders();
if (lastModified != null) {
requestHeaders.set(HttpHeaders.IF_MODIFIED_SINCE, DateUtils.formatDate(lastModified));
}
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
ResponseEntity<Feed> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Feed.class);
if (response.getStatusCode() != HttpStatus.NOT_MODIFIED) {
log.trace("data has been modified");
Feed feed = response.getBody();
for (Entry entry : feed.getEntries()) {
if ((lastModified == null) || (entry.getUpdated().after(lastModified))) {
Invoice invoice = restTemplate
.getForEntity(entry.getContents().get(0).getSrc(), Invoice.class).getBody();
log.trace("saving invoice {}", invoice.getId());
invoiceService.generateInvoice(invoice);
}
}
if (response.getHeaders().getFirst(HttpHeaders.LAST_MODIFIED) != null) {
lastModified = DateUtils.parseDate(response.getHeaders().getFirst(HttpHeaders.LAST_MODIFIED));
log.trace("Last-Modified header {}", lastModified);
}
} else {
log.trace("no new data");
}
}
示例2: requestWithLastModifiedReturns304
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void requestWithLastModifiedReturns304() {
Order order = new Order();
order.setCustomer(customerRepository.findAll().iterator().next());
orderRepository.save(order);
ResponseEntity<Feed> response = restTemplate.exchange(feedUrl(), HttpMethod.GET, new HttpEntity(null),
Feed.class);
Date lastModified = DateUtils.parseDate(response.getHeaders().getFirst("Last-Modified"));
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("If-Modified-Since", DateUtils.formatDate(lastModified));
HttpEntity requestEntity = new HttpEntity(requestHeaders);
response = restTemplate.exchange(feedUrl(), HttpMethod.GET, requestEntity, Feed.class);
assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode());
}
示例3: pollInternal
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
public void pollInternal() {
HttpHeaders requestHeaders = new HttpHeaders();
if (lastModified != null) {
requestHeaders.set(HttpHeaders.IF_MODIFIED_SINCE, DateUtils.formatDate(lastModified));
}
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
ResponseEntity<Feed> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Feed.class);
if (response.getStatusCode() != HttpStatus.NOT_MODIFIED) {
log.trace("data has been modified");
Feed feed = response.getBody();
for (Entry entry : feed.getEntries()) {
if ((lastModified == null) || (entry.getUpdated().after(lastModified))) {
Shipment shipping = restTemplate
.getForEntity(entry.getContents().get(0).getSrc(), Shipment.class).getBody();
log.trace("saving shipping {}", shipping.getId());
shipmentService.ship(shipping);
}
}
if (response.getHeaders().getFirst("Last-Modified") != null) {
lastModified = DateUtils.parseDate(response.getHeaders().getFirst(HttpHeaders.LAST_MODIFIED));
log.trace("Last-Modified header {}", lastModified);
}
} else {
log.trace("no new data");
}
}
示例4: isLastModifiedChanged
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
private boolean isLastModifiedChanged(final HttpResponse response) {
final Header newLastModifiedHeader = response.getFirstHeader(HttpHeaders.LAST_MODIFIED);
if (newLastModifiedHeader != null) {
final Date newLastModified = DateUtils.parseDate(newLastModifiedHeader.getValue());
if (newLastModified == null) {
throw new IllegalArgumentException("Invalid last modified date");
}
if (!_previousLastModified.isPresent() || newLastModified.after(_previousLastModified.get())) {
LOGGER.debug()
.setMessage("Uri last modified changed")
.addData("uri", _uri)
.addData("newLastModified", newLastModified)
.addData("previousLastModified", _previousLastModified)
.log();
_previousLastModified = Optional.of(newLastModified);
return true;
}
}
return false;
}
示例5: revalidationResponseIsTooOld
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
private boolean revalidationResponseIsTooOld(final HttpResponse backendResponse,
final HttpCacheEntry cacheEntry) {
final Header entryDateHeader = cacheEntry.getFirstHeader(HTTP.DATE_HEADER);
final Header responseDateHeader = backendResponse.getFirstHeader(HTTP.DATE_HEADER);
if (entryDateHeader != null && responseDateHeader != null) {
final Date entryDate = DateUtils.parseDate(entryDateHeader.getValue());
final Date respDate = DateUtils.parseDate(responseDateHeader.getValue());
if (entryDate == null || respDate == null) {
// 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.
return false;
}
if (respDate.before(entryDate)) {
return true;
}
}
return false;
}
示例6: alreadyHaveNewerCacheEntry
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
private boolean alreadyHaveNewerCacheEntry(final HttpHost target, final HttpRequestWrapper request,
final HttpResponse backendResponse) {
HttpCacheEntry existing = null;
try {
existing = responseCache.getCacheEntry(target, request);
} catch (final IOException ioe) {
// nop
}
if (existing == null) {
return false;
}
final Header entryDateHeader = existing.getFirstHeader(HTTP.DATE_HEADER);
if (entryDateHeader == null) {
return false;
}
final Header responseDateHeader = backendResponse.getFirstHeader(HTTP.DATE_HEADER);
if (responseDateHeader == null) {
return false;
}
final Date entryDate = DateUtils.parseDate(entryDateHeader.getValue());
final Date responseDate = DateUtils.parseDate(responseDateHeader.getValue());
if (entryDate == null || responseDate == null) {
return false;
}
return responseDate.before(entryDate);
}
示例7: expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
private boolean expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl(
final HttpResponse response) {
if (response.getFirstHeader(HeaderConstants.CACHE_CONTROL) != null) {
return false;
}
final Header expiresHdr = response.getFirstHeader(HeaderConstants.EXPIRES);
final Header dateHdr = response.getFirstHeader(HTTP.DATE_HEADER);
if (expiresHdr == null || dateHdr == null) {
return false;
}
final Date expires = DateUtils.parseDate(expiresHdr.getValue());
final Date date = DateUtils.parseDate(dateHdr.getValue());
if (expires == null || date == null) {
return false;
}
return expires.equals(date) || expires.before(date);
}
示例8: testSmallEnoughResponsesAreCached
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void testSmallEnoughResponsesAreCached() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final HttpRequest request = new HttpGet("http://foo.example.com/bar");
final Date now = new Date();
final Date requestSent = new Date(now.getTime() - 3 * 1000L);
final Date responseGenerated = new Date(now.getTime() - 2 * 1000L);
final Date responseReceived = new Date(now.getTime() - 1 * 1000L);
final HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES - 1));
originResponse.setHeader("Cache-Control","public, max-age=3600");
originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated));
originResponse.setHeader("ETag", "\"etag\"");
final HttpResponse result = impl.cacheAndReturnResponse(host, request, originResponse, requestSent, responseReceived);
assertEquals(1, backing.map.size());
assertTrue(backing.map.containsKey((new CacheKeyGenerator()).getURI(host, request)));
assertTrue(HttpTestUtils.semanticallyTransparent(originResponse, result));
}
示例9: testForwardsExistingWarningHeadersOnResponseThatArrivesStale
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void testForwardsExistingWarningHeadersOnResponseThatArrivesStale()
throws Exception {
originResponse.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
originResponse.setHeader("Cache-Control","public, max-age=5");
originResponse.setHeader("ETag","\"etag\"");
originResponse.addHeader("Age","10");
final String warning = "110 fred \"Response is stale\"";
originResponse.addHeader("Warning",warning);
backendExpectsAnyRequest().andReturn(originResponse);
replayMocks();
final HttpResponse result = impl.execute(route, request, context, null);
verifyMocks();
assertEquals(warning, result.getFirstHeader("Warning").getValue());
}
示例10: flushesEntryIfFresherAndSpecifiedByRelativeContentLocation
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void flushesEntryIfFresherAndSpecifiedByRelativeContentLocation()
throws Exception {
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
final String cacheKey = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", "/bar");
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
when(mockStorage.getEntry(cacheKey)).thenReturn(entry);
impl.flushInvalidatedCacheEntries(host, request, response);
verify(mockStorage).getEntry(cacheKey);
verify(mockStorage).removeEntry(cacheKey);
}
示例11: testDoNotFlushContentLocationEntryIfSafeRequest
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void testDoNotFlushContentLocationEntryIfSafeRequest()
throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final HttpRequest req = new HttpGet("/foo");
final HttpResponse resp = HttpTestUtils.make200Response();
resp.setHeader("Content-Location", "/bar");
final String key = (new CacheKeyGenerator()).getURI(host, new HttpGet("/bar"));
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
new BasicHeader("ETag", "\"old-etag\"")
});
backing.map.put(key, entry);
impl.flushInvalidatedCacheEntriesFor(host, req, resp);
assertEquals(entry, backing.map.get(key));
}
示例12: doesNotFlushEntrySpecifiedByContentLocationIfResponseHasNoEtag
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void doesNotFlushEntrySpecifiedByContentLocationIfResponseHasNoEtag()
throws Exception {
response.removeHeaders("ETag");
response.setHeader("Date", DateUtils.formatDate(now));
final String theURI = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", theURI);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
when(mockStorage.getEntry(theURI)).thenReturn(entry);
impl.flushInvalidatedCacheEntries(host, request, response);
verify(mockStorage).getEntry(theURI);
verifyNoMoreInteractions(mockStorage);
}
示例13: expiresEqualToDateWithNoCacheControlIsNotCacheable
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void expiresEqualToDateWithNoCacheControlIsNotCacheable()
throws Exception {
final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultRequest());
final HttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Expires", DateUtils.formatDate(now));
resp1.removeHeaders("Cache-Control");
backendExpectsAnyRequestAndReturn(resp1);
final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultRequest());
req2.setHeader("Cache-Control", "max-stale=1000");
final HttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("ETag", "\"etag2\"");
backendExpectsAnyRequestAndReturn(resp2);
replayMocks();
impl.execute(route, req1, context, null);
final HttpResponse result = impl.execute(route, req2, context, null);
verifyMocks();
assertTrue(HttpTestUtils.semanticallyTransparent(resp2, result));
}
示例14: returnsStaleCacheEntryWithOnlyIfCachedAndMaxStale
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void returnsStaleCacheEntryWithOnlyIfCachedAndMaxStale()
throws Exception {
final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultRequest());
final HttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Cache-Control","max-age=5");
backendExpectsAnyRequestAndReturn(resp1);
final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultRequest());
req2.setHeader("Cache-Control", "max-stale=20, only-if-cached");
replayMocks();
impl.execute(route, req1, context, null);
final HttpResponse result = impl.execute(route, req2, context, null);
verifyMocks();
assertTrue(HttpTestUtils.semanticallyTransparent(resp1, result));
}
示例15: testFlushContentLocationEntryIfUnSafeRequest
import org.apache.http.client.utils.DateUtils; //导入依赖的package包/类
@Test
public void testFlushContentLocationEntryIfUnSafeRequest()
throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final HttpRequest req = new HttpPost("/foo");
final HttpResponse resp = HttpTestUtils.make200Response();
resp.setHeader("Content-Location", "/bar");
resp.setHeader(HeaderConstants.ETAG, "\"etag\"");
final String key = (new CacheKeyGenerator()).getURI(host, new HttpGet("/bar"));
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
new BasicHeader("ETag", "\"old-etag\"")
});
backing.map.put(key, entry);
impl.flushInvalidatedCacheEntriesFor(host, req, resp);
assertNull(backing.map.get(key));
}