put(URI, URLConnection): This method is available in the java.net.ResponseCache class of Java.
Syntax:
CacheRequest java.net.ResponseCache.put(URI uri, URLConnection conn) throws IOException
This method takes two arguments. 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. If the resource is to be cached, then put() must return a CacheRequest object which contains an OutputStream that the protocol handler will use to write the resource into the cache.
If the resource is not to be cached, then the put must return null.
Parameters: Two parameters are required for this method.
uri: a URI used to reference the requested network resource.
conn: a URLConnection instance that is used to fetch the response to be cached.
Returns: a CacheRequest for recording the response to be cached. Null return indicates that the caller does not intend to cache the response.
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.io.OutputStream;import java.io.PrintStream;import java.net.CacheRequest;import java.net.CacheResponse;import java.net.ResponseCache;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;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 ResponseCacheput {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 new CacheRequest() {@Overridepublic OutputStream getBody() throws IOException {return PrintStream.nullOutputStream();}@Overridepublic void abort() {}};}};URI uri = new URI("http://localhost:8080");Map<String, List<String>> rqstHeaders = new HashMap<String, List<String>>();List<String> token = new ArrayList<String>();token.add("abcgdggdg");rqstHeaders.put("Authorization", token);URL url = new URL("https://beingcodeexpert.blogspot.com/");ResponseCache.setDefault(responseCache);URLConnection conn = url.openConnection();System.out.println(responseCache.put(uri, conn));}}
Output:
com.ResponseCache.ResponseCacheput$1$2@10dba097
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