Changeset 273


Ignore:
Timestamp:
03/22/10 17:27:53 (14 years ago)
Author:
oschonef
Message:
  • further work on GWDG handle service integration
File:
1 edited

Legend:

Unmodified
Added
Removed
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/model/GWDGPersistentIdentifierProvider.java

    r250 r273  
    44import java.net.URISyntaxException;
    55import java.util.ArrayList;
     6import java.util.Collections;
     7import java.util.HashMap;
    68import java.util.List;
    79import java.util.Map;
     
    2123import org.apache.http.impl.client.DefaultHttpClient;
    2224import org.apache.http.message.BasicNameValuePair;
     25import org.apache.http.params.HttpProtocolParams;
    2326import org.apache.http.protocol.BasicHttpContext;
    2427import org.apache.http.protocol.HttpContext;
     
    3134public class GWDGPersistentIdentifierProvider extends
    3235                PersistentIdentifierProvider {
     36        private static enum Attribute {
     37                PID, URL, CREATOR, EXPDATE;
     38               
     39                public static Attribute fromString(String s) {
     40                        if (s.equalsIgnoreCase("pid")) {
     41                                return PID;
     42                        } else if (s.equalsIgnoreCase("url")) {
     43                                return URL;
     44                        } else if (s.equalsIgnoreCase("creator")) {
     45                                return CREATOR;
     46                        } else if (s.equalsIgnoreCase("expdate")) {
     47                                return EXPDATE;
     48                        }
     49                        return null;
     50                }
     51               
     52                public String toString() {
     53                        switch (this) {
     54                        case PID:
     55                                return "pid";
     56                        case URL:
     57                                return "url";
     58                        case CREATOR:
     59                                return "creator";
     60                        case EXPDATE:
     61                                return "expdate";
     62                        default:
     63                                throw new InternalError();
     64                        }
     65                }
     66        } // private enum Attribute
    3367        public static final String USERNAME = "pid_provider.username";
    3468        public static final String PASSWORD = "pid_provider.password";
    35         private static final URI SERVICE_URI =
    36                 URI.create("http://handle.gwdg.de:8080/pidservice/");
     69        private static final String SERVICE_URI_BASE =
     70                "http://handle.gwdg.de:8080/pidservice/";
     71        private static final String USER_AGENT =
     72                "CLARIN-VirtualCollectionRegisty/1.0";
    3773        private static final Logger logger =
    3874                LoggerFactory.getLogger(GWDGPersistentIdentifierProvider.class);
     
    4076        private String username = null;
    4177        private String password = null;
     78        private XMLInputFactory factory;
    4279
    4380        /* XXX: refactor Internal and GWDG PID class/providers, so only one
     
    63100                this.username = getParameter(config, USERNAME);
    64101                this.password = getParameter(config, PASSWORD);
     102               
     103                this.factory = XMLInputFactory.newInstance();
     104                factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
     105                factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
     106                                                        Boolean.TRUE);
    65107        }
    66108
    67109        public PersistentIdentifier createPersistentIdentifier(VirtualCollection vc)
    68110                        throws VirtualCollectionRegistryException {
    69                 logger.debug("creating handle for virtual collection {}", vc.getUUID());
    70                 URI target    = makeCollectionURI(vc);
    71                 String handle = createHandle(target);
    72                 logger.info("created handle {} for virtual collection {}",
    73                                         handle, vc.getUUID());
    74                 return new GWDGPersistentIdentifier(vc, handle);
    75         }
    76 
    77         private URI makeCollectionURI(VirtualCollection vc) {
    78                 return URI.create(base_uri +
    79                           "service/clarin-virtualcollection/" + vc.getUUID());
    80         }
    81 
    82         private String createHandle(URI target) {
    83                 logger.debug("pid target: {}", target);
    84                 foo(target);
    85                 return "12345/007";
     111                logger.debug("creating handle for virtual collection \"{}\"",
     112                                vc.getUUID());
     113                try {
     114                        String target  = makeCollectionURI(vc);
     115                        // XXX: testing
     116                        // URI serviceURI = URI.create(SERVICE_URI_BASE + "write/create");
     117                        URI serviceURI = URI.create(SERVICE_URI_BASE + "write/modify");
     118
     119                        List<NameValuePair> params = new ArrayList<NameValuePair>();
     120                        params.add(new BasicNameValuePair("url", target));
     121                        // XXX: testing
     122                        params.add(new BasicNameValuePair("pid", "11858/00-232Z-0000-0000-40AE-F"));
     123                        Map<Attribute, String> props = invokeWebService(serviceURI, params);
     124                        String pid = props.get(Attribute.PID);
     125                        if (pid == null) {
     126                                throw new VirtualCollectionRegistryException(
     127                                        "no handle returned");
     128                        }
     129                        logger.info("created handle \"{}\" for virtual collection \"{}\"",
     130                                        pid, vc.getUUID());
     131                        return new GWDGPersistentIdentifier(vc, pid);
     132                } catch (VirtualCollectionRegistryException e) {
     133                        throw new RuntimeException("failed to create handle", e);
     134                }
     135        }
     136
     137        @SuppressWarnings("unused")
     138        private void update(String pid, URI target) throws VirtualCollectionRegistryException {
     139                List<NameValuePair> params = new ArrayList<NameValuePair>();
     140                params.add(new BasicNameValuePair("pid", pid));
     141                params.add(new BasicNameValuePair("url", target.toString()));
     142                URI serviceURI = URI.create(SERVICE_URI_BASE + "write/modify");
     143                invokeWebService(serviceURI, params);
     144        }
     145
     146        private String makeCollectionURI(VirtualCollection vc) {
     147                return base_uri + "service/clarin-virtualcollection/" + vc.getUUID();
    86148        }
    87149
     
    101163        }
    102164
    103         private void foo(URI target) {
     165        private Map<Attribute, String> invokeWebService(URI serviceTargetURI,
     166                        List<NameValuePair> formparams)
     167                        throws VirtualCollectionRegistryException {
     168                // force xml encoding
     169                formparams.add(new BasicNameValuePair("encoding", "xml"));
     170
     171                DefaultHttpClient client = null;
    104172                try {
    105                         URI serviceURI = URI.create(SERVICE_URI.toString() + "write/modify");
    106                         DefaultHttpClient client = new DefaultHttpClient();
    107                         int port = serviceURI.getPort() != -1
    108                                  ? serviceURI.getPort()
     173                        client = new DefaultHttpClient();
     174                        int port = serviceTargetURI.getPort() != -1
     175                                 ? serviceTargetURI.getPort()
    109176                     : AuthScope.ANY_PORT
    110177                     ;
    111178                        client.getCredentialsProvider().setCredentials(
    112                                         new AuthScope(serviceURI.getHost(), port),
     179                                        new AuthScope(serviceTargetURI.getHost(), port),
    113180                                        new UsernamePasswordCredentials(username, password)
    114181                        );
    115                         List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    116                         formparams.add(new BasicNameValuePair("encoding", "xml"));
    117                         formparams.add(new BasicNameValuePair("url", target.toString()));
    118                         formparams.add(new BasicNameValuePair("pid", "11858/00-232Z-0000-0000-40AE-F"));
    119                        
    120                         HttpPost request = new HttpPost(serviceURI);
     182                        // disable expect continue, GWDG does not like very well
     183                        client.getParams()
     184                                .setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, Boolean.FALSE);
     185                        // set a proper user agent
     186                        client.getParams()
     187                                .setParameter(HttpProtocolParams.USER_AGENT, USER_AGENT);
     188                        HttpPost request = new HttpPost(serviceTargetURI);
     189                        request.addHeader("Accept", "text/xml, application/xml");
    121190                        request.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
    122191                        HttpContext ctx = new BasicHttpContext();
    123                        
     192
     193                        logger.debug("invoking GWDG service at {}", serviceTargetURI);
    124194                        HttpResponse response = client.execute(request, ctx);
    125195                        StatusLine status = response.getStatusLine();
    126196                        HttpEntity entity = response.getEntity();
    127                        
     197                        Map<Attribute, String> props = Collections.emptyMap();
     198
    128199                        logger.debug("GWDG Service status: {}", status.toString());
    129200                        if ((status.getStatusCode() >= 200) &&
     
    135206                                }
    136207
    137                                 logger.debug("type={}, length={}",
    138                                                 entity.getContentType(), entity.getContentLength());
    139                                 logger.debug("encoding={}", encoding);
    140 
    141                                 XMLInputFactory factory = XMLInputFactory.newInstance();
    142                                 factory.setProperty(XMLInputFactory.IS_COALESCING, true);
    143                                 factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
    144                                 XMLStreamReader reader = factory.createXMLStreamReader(response.getEntity().getContent(), encoding);
     208                                XMLStreamReader reader = factory
     209                                                .createXMLStreamReader(entity.getContent(), encoding);
     210                                props = new HashMap<Attribute, String>();
    145211                                while (reader.hasNext()) {
    146212                                        reader.next();
    147                                         if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
    148                                                 if (reader.getLocalName().equalsIgnoreCase("pid")) {
    149                                                         reader.next();
    150                                                         logger.trace("-> PID: " + reader.getText());
    151                                                 } else if (reader.getLocalName().equalsIgnoreCase("expdate")) {
    152                                                         reader.next();
    153                                                         logger.trace("-> EXP-Date: " + reader.getText());
    154                                                 } else if (reader.getLocalName().equalsIgnoreCase("url")) {
    155                                                         reader.next();
    156                                                         logger.trace("-> URI: " + reader.getText());
     213                                       
     214                                        int type = reader.getEventType();
     215                                        if (type != XMLStreamConstants.START_ELEMENT) {
     216                                                continue;
     217                                        }
     218                                        Attribute attribute =
     219                                                Attribute.fromString(reader.getLocalName());
     220                                        if (attribute != null) {
     221                                                if (!reader.hasNext()) {
     222                                                        throw new VirtualCollectionRegistryException(
     223                                                                "unexpected end of data stream");
     224                                                }
     225                                                reader.next();
     226                                                if (reader.getEventType() !=
     227                                                                XMLStreamConstants.CHARACTERS) {
     228                                                        throw new VirtualCollectionRegistryException(
     229                                                                "unexpected element type: " +
     230                                                                reader.getEventType());
     231                                                }
     232                                                String value = reader.getText();
     233                                                if (value == null) {
     234                                                        throw new VirtualCollectionRegistryException(
     235                                                                "element \"" + attribute + "\" was empty");
     236                                                }
     237                                                value = value.trim();
     238                                                if (!value.isEmpty()) {
     239                                                        props.put(attribute, value);
    157240                                                }
    158241                                        }
    159242                                }
     243
    160244                        } else {
    161                                 logger.error("http failed: {}", status);
    162                         }
    163                         client.getConnectionManager().shutdown();
     245                                logger.debug("GWDG Handle service failed: {}", status);
     246                                request.abort();
     247                                throw new VirtualCollectionRegistryException(
     248                                        "error invoking GWDG handle service");
     249                        }
     250                        return props;
     251                } catch (VirtualCollectionRegistryException e) {
     252                        throw e;
    164253                } catch (Exception e) {
    165                         logger.error("http failed", e);
     254                        logger.debug("GWDG Handle service failed", e);
     255                        throw new VirtualCollectionRegistryException(
     256                                        "error invoking GWDG handle service", e);
     257                } finally {
     258                        if (client != null) {
     259                                client.getConnectionManager().shutdown();
     260                        }
    166261                }
    167262        }
Note: See TracChangeset for help on using the changeset viewer.