get(URI, String, Map): This method is available in the java.net.ResponseCache class of Java.
Syntax:
CacheResponse java.net.ResponseCache.get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException
This method takes three arguments. This method retrieves the cached response based on the requested URI, the request method, and the request headers. Typically this method is called by the protocol handler before it sends out the request to get the network resource. If a cached response is returned, that resource is used instead.
Parameters: Three parameters are required for this method.
uri: a URI used to reference the requested network resource.
rqstMethod: a String representing the request method.
rqstHeaders: a Map from request header field names to lists of field values representing the current request headers.
Returns: a CacheResponse instance if available from the cache, or null otherwise.
Throws:
1. IOException - if an I/O error occurs.
2. IllegalArgumentException - if any one of the arguments is null.
Approach
Java
package com.ResponseCache;import java.io.IOException;import java.io.InputStream;import java.net.CacheRequest;import java.net.CacheResponse;import java.net.ResponseCache;import java.net.URI;import java.net.URISyntaxException;import java.net.URLConnection;import java.util.ArrayList;import java.util.HashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.TreeMap;public class ResponseCacheget {public static void main(String[] args) throws URISyntaxException, IOException {ResponseCache responseCache = new ResponseCache() {@Overridepublic CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)throws IOException {CacheResponse cr = new CacheResponse() {@Overridepublic Map<String, List<String>> getHeaders() throws IOException {Map<Integer, List<String>> map = new TreeMap<Integer, List<String>>();List<String> list = new LinkedList<String>();list.add("Hello");map.put(1, list);return null;}@Overridepublic InputStream getBody() throws IOException {return new InputStream() {@Overridepublic int read() throws IOException {return 0;}};}};return cr;}@Overridepublic CacheRequest put(URI uri, URLConnection conn) throws IOException {return null;}};URI uri = new URI("http://localhost:8080");String rqstMethod = "GET";Map<String, List<String>> rqstHeaders = new HashMap<String, List<String>>();List<String> token = new ArrayList<String>();token.add("abcgdggdg");rqstHeaders.put("Authorization", token);ResponseCache.setDefault(responseCache);System.out.println(responseCache.get(uri, rqstMethod, rqstHeaders));}}
Output:
com.ResponseCache.ResponseCacheget$1$1@4926097b
Some other methods of ResponseCache class
get(URI, String, Map): This method retrieves the cached response based on the requested URI, the request method, and the request headers. Typically this method is called by the protocol handler before it sends out the request to get the network resource. If a cached response is returned, that resource is used instead.
ResponseCache.getDefault(): This method gets the system-wide response cache.
put(URI, URLConnection): The protocol handler calls this method after a resource has been retrieved, and the ResponseCache must decide whether or not to store the resource in its cache.
ResponseCache.setDefault(ResponseCache): This method sets (or unsets) the system-wide cache.
No comments:
Post a Comment