Example usage for org.apache.http.impl.cookie DateUtils formatDate

List of usage examples for org.apache.http.impl.cookie DateUtils formatDate

Introduction

In this page you can find the example usage for org.apache.http.impl.cookie DateUtils formatDate.

Prototype

public static String formatDate(final Date date) 

Source Link

Document

Formats the given date according to the RFC 1123 pattern.

Usage

From source file:com.mothsoft.alexis.util.NetworkingUtil.java

public static HttpClientResponse get(final URL url, final String etag, final Date lastModifiedDate)
        throws IOException {

    final HttpGet get = new HttpGet(url.toExternalForm());

    get.addHeader("Accept-Charset", "UTF-8");

    if (etag != null) {
        get.addHeader("If-None-Match", etag);
    }//from ww  w  .j ava2 s.c  om

    if (lastModifiedDate != null) {
        get.addHeader("If-Modified-Since", DateUtils.formatDate(lastModifiedDate));
    }

    int statusCode = -1;
    String responseEtag = null;
    Date responseLastModifiedDate = null;

    final HttpClient client = getClient();
    HttpResponse response = client.execute(get);
    statusCode = response.getStatusLine().getStatusCode();

    InputStream is = null;
    Charset charset = null;

    if (statusCode == 304) {
        responseEtag = etag;
        responseLastModifiedDate = lastModifiedDate;
    } else {
        final Header responseEtagHeader = response.getFirstHeader("Etag");
        if (responseEtagHeader != null) {
            responseEtag = responseEtagHeader.getValue();
        }

        final Header lastModifiedDateHeader = response.getFirstHeader("Last-Modified");
        if (lastModifiedDateHeader != null) {
            try {
                responseLastModifiedDate = DateUtils.parseDate(lastModifiedDateHeader.getValue());
            } catch (DateParseException e) {
                responseLastModifiedDate = null;
            }
        }

        final HttpEntity entity = response.getEntity();

        // here's where to do intelligent checking of content type, content
        // length, etc.
        if (entity.getContentLength() > MAX_CONTENT_LENGTH) {
            get.abort();
            throw new IOException("Exceeded maximum content length, length is: " + entity.getContentLength());
        }

        is = entity.getContent();
        charset = getCharset(entity);
    }
    return new HttpClientResponse(get, statusCode, responseEtag, responseLastModifiedDate, is, charset);
}

From source file:org.wrml.runtime.syntax.DateSyntaxHandler.java

@Override
public String formatSyntaxValue(final Date date) {

    return DateUtils.formatDate(date);
}

From source file:org.jboss.tools.aerogear.hybrid.core.test.TestBundleHttpStorage.java

private HttpCacheEntry makeHttpCacheEntry() {
    final Date now = new Date();
    final StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
    final Header[] headers = { new BasicHeader("Date", DateUtils.formatDate(now)),
            new BasicHeader("Server", "MockServer/1.0") };
    final Resource resource = new HeapResource(new byte[0]);
    HttpCacheEntry entry = new HttpCacheEntry(now, now, statusLine, headers, resource);
    return entry;
}

From source file:edu.berkeley.boinc.adapter.NoticesListAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final Notice listItem = entries.get(position);

    LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = vi.inflate(R.layout.notices_layout_listitem, null);

    ImageView ivIcon = (ImageView) v.findViewById(R.id.projectIcon);
    Bitmap icon = getIcon(position);//from w  ww.j ava2 s .  c  o  m
    // if available set icon, if not boinc logo
    if (icon == null) {
        ivIcon.setImageDrawable(getContext().getResources().getDrawable(R.drawable.boinc));
    } else {
        ivIcon.setImageBitmap(icon);
    }

    TextView tvProjectName = (TextView) v.findViewById(R.id.projectName);
    tvProjectName.setText(listItem.project_name);

    TextView tvNoticeTitle = (TextView) v.findViewById(R.id.noticeTitle);
    tvNoticeTitle.setText(listItem.title);

    TextView tvNoticeContent = (TextView) v.findViewById(R.id.noticeContent);
    tvNoticeContent.setText(Html.fromHtml(listItem.description));

    TextView tvNoticeTime = (TextView) v.findViewById(R.id.noticeTime);
    tvNoticeTime.setText(DateUtils.formatDate(new java.util.Date((long) listItem.create_time * 1000)));

    v.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Logging.DEBUG)
                Log.d(Logging.TAG, "noticeClick: " + listItem.link);

            if (listItem.link != null && !listItem.link.isEmpty()) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(listItem.link));
                activity.startActivity(i);
            }

        }
    });

    return v;
}

From source file:Global.java

@SuppressWarnings("rawtypes")
private Action<Void> getDefaultAction(final Http.Request request) {
    final long start = System.currentTimeMillis();
    return new Action.Simple() {
        public Result call(Http.Context ctx) throws Throwable {
            UserApp.initTokenUser();//from w ww  .jav a 2 s  .co  m
            UserApp.updatePreferredLanguage();
            ctx.response().setHeader("Date", DateUtils.formatDate(new Date()));
            ctx.response().setHeader("Cache-Control", "no-cache");
            Result result = delegate.call(ctx);
            AccessLogger.log(request, result, start);
            return result;
        }
    };
}

From source file:com.apigee.sdk.apm.http.impl.client.cache.CachedHttpResponseGenerator.java

/**
 * 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.//w ww .j a va 2  s  .  c  om
 * 
 * @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;
}

From source file:com.barchart.netty.server.http.handlers.TestStaticResourceHandler.java

@Test
public void testFileCacheExpired() throws Exception {

    HttpGet get = new HttpGet("http://localhost:" + port + "/file/test.css");
    HttpResponse response = client.execute(get);

    final String modified = response.getFirstHeader(HttpHeaders.LAST_MODIFIED).getValue();
    final long modtime = DateUtils.parseDate(modified).getTime();

    get = new HttpGet("http://localhost:" + port + "/file/test.css");
    get.addHeader(HttpHeaders.IF_MODIFIED_SINCE, DateUtils.formatDate(new Date(modtime - 1000)));
    response = client.execute(get);//from w  ww  .  ja va 2  s  . c om

    assertEquals(200, response.getStatusLine().getStatusCode());
    assertTrue(IOUtils.contentEquals(
            new FileInputStream(
                    new File(System.getProperty("user.dir") + "/src/test/resources/files/test.css")),
            response.getEntity().getContent()));

}

From source file:com.scut.easyfe.network.kjFrame.http.Network.java

/**
 * Respondeader?Cachetag/* ww w  .jav  a  2s .  c om*/
 * 
 * @param headers
 * @param entry
 */
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    if (entry == null) {
        return;
    }
    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }
    if (entry.serverDate > 0) {
        Date refTime = new Date(entry.serverDate);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}