Changeset 5338


Ignore:
Timestamp:
06/08/14 18:35:22 (10 years ago)
Author:
Oliver Schonefeld
Message:
  • add new handle resolver
Location:
CMDIValidator/trunk/cmdi-validator-core/src/main/java/eu/clarin/cmdi/validator/plugins
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • CMDIValidator/trunk/cmdi-validator-core/src/main/java/eu/clarin/cmdi/validator/plugins/CheckPidPluginFactory.java

    r5204 r5338  
    11package eu.clarin.cmdi.validator.plugins;
    22
    3 import java.io.IOException;
    43import java.net.URI;
    54import java.net.URISyntaxException;
    65
    7 import org.apache.http.HttpResponse;
    86import org.apache.http.HttpStatus;
    9 import org.apache.http.StatusLine;
    10 import org.apache.http.client.HttpClient;
    11 import org.apache.http.client.methods.HttpHead;
    12 import org.apache.http.client.params.ClientPNames;
    13 import org.apache.http.client.utils.HttpClientUtils;
    14 import org.apache.http.impl.client.DefaultHttpClient;
    15 import org.apache.http.params.CoreProtocolPNames;
    16 import org.apache.http.params.HttpParams;
    17 import org.apache.http.util.EntityUtils;
    187import org.slf4j.Logger;
    198import org.slf4j.LoggerFactory;
     
    3120import eu.clarin.cmdi.validator.CMDIValidatorInitException;
    3221import eu.clarin.cmdi.validator.CMDIValidatorResultImpl;
    33 import eu.clarin.cmdi.validator.utils.LRUCache;
    3422
    3523
     
    4129    private static final String HDL_PROXY_HOST = "hdl.handle.net";
    4230    private static final String URN_SCHEME = "urn";
     31    @SuppressWarnings("unused")
    4332    private static final Logger logger =
    4433            LoggerFactory.getLogger(CheckPidPlugin.class);
     34    private final HandleResolver resolver = new HandleResolver();
    4535
    4636
     
    5242
    5343    public class CheckPidPlugin implements CMDIValidationPlugin {
    54         private final HttpClient httpClient;
    5544        private final XPathExecutable xpath;
    56         private final LRUCache<URI, Integer> cache =
    57                 new LRUCache<URI, Integer>(4*4096);
    5845
    5946        private CheckPidPlugin(final Processor processor)
    6047                throws CMDIValidatorInitException {
    61             this.httpClient = new DefaultHttpClient();
    62 
    63             final HttpParams params = this.httpClient.getParams();
    64             params.setParameter(CoreProtocolPNames.USER_AGENT,
    65                     CheckPidPlugin.class.getPackage().getName() + "/0.0.1");
    66             params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.TRUE);
    67             params.setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    68             params.setIntParameter(ClientPNames.MAX_REDIRECTS, 16);
    69 
    7048            try {
    7149                final XPathCompiler compiler = processor.newXPathCompiler();
     
    151129        private void checkHandle2(URI uri, CMDIValidatorResultImpl result)
    152130                throws CMDIValidatorException {
    153             Integer code = cache.get(uri);
    154             if (code == null) {
    155                 code = checkHandle3(uri, result);
    156                 // cache result
    157                 cache.put(uri, code);
    158             }
    159 
     131            int code = resolver.resolve(uri);
    160132            switch (code) {
    161133            case HttpStatus.SC_OK:
     
    181153            }
    182154        }
    183 
    184 
    185         private int checkHandle3(URI uri, CMDIValidatorResultImpl result)
    186                 throws CMDIValidatorException {
    187             HttpHead request = null;
    188             HttpResponse response = null;
    189             try {
    190                 request = new HttpHead(uri);
    191                 response = httpClient.execute(request);
    192 
    193                 final StatusLine status = response.getStatusLine();
    194                 logger.debug("checked handle: file={}, uri={}, status={}",
    195                         result.getFile(), uri, status.getStatusCode());
    196                 return status.getStatusCode();
    197             } catch (IOException e) {
    198                 throw new CMDIValidatorException("error performing handle check", e);
    199             } finally {
    200                 if (response != null) {
    201                     try {
    202                         EntityUtils.consume(response.getEntity());
    203                     } catch (IOException ex) {
    204                         /* IGNORE */
    205                     }
    206 
    207                     /* make sure to release allocated resources */
    208                     HttpClientUtils.closeQuietly(response);
    209                 }
    210             }
    211         }
    212155    }
    213156
Note: See TracChangeset for help on using the changeset viewer.