Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000-2005 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: jones $'
7
 *     '$Date: 2005-12-07 10:59:03 -0800 (Wed, 07 Dec 2005) $'
8
 * '$Revision: 2819 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24

    
25
package edu.ucsb.nceas.metacat.lsid;
26

    
27
import java.util.regex.Matcher;
28
import java.util.regex.Pattern;
29

    
30
import org.apache.commons.logging.Log;
31
import org.apache.commons.logging.LogFactory;
32

    
33
import com.ibm.lsid.ExpiringResponse;
34
import com.ibm.lsid.LSID;
35
import com.ibm.lsid.server.LSIDServerException;
36
import com.ibm.lsid.server.LSIDServiceConfig;
37
import com.ibm.lsid.server.impl.SimpleAuthority;
38
import com.ibm.lsid.wsdl.HTTPLocation;
39
import com.ibm.lsid.wsdl.LSIDDataPort;
40
import com.ibm.lsid.wsdl.LSIDMetadataPort;
41
import com.ibm.lsid.wsdl.SOAPLocation;
42

    
43
public class LSIDAuthorityMain extends SimpleAuthority
44
{
45

    
46
    private LSIDDataLookup lookup = null;
47
    private static Log logger = LogFactory
48
                    .getLog("edu.ucsb.nceas.metacat.lsid");
49

    
50
    public void initService(LSIDServiceConfig cf) throws LSIDServerException
51
    {
52
        logger.info("Starting LSIDAuthorityMain.");
53
        lookup = new LSIDDataLookup();
54
    }
55

    
56
    public ExpiringResponse getKnownURIs() throws LSIDServerException
57
    {
58
        logger.debug("In LSIDAuthorityMain.getKnownURIs()");
59
        return null;
60
    }
61

    
62
    public LSIDMetadataPort[] getMetadataLocations(LSID lsid, String url)
63
    {
64
        logger.debug("In LSIDAuthorityMain.getMetadataLocations()");
65
        if (lookup == null)
66
            return null;
67

    
68
        int lsType;
69
        try {
70
            lsType = lookup.lsidType(lsid);
71
        } catch (LSIDServerException ex) {
72
            ex.printStackTrace();
73
            lsType = LSIDDataLookup.UNKNOWN;
74
        }
75
        if (lsType == LSIDDataLookup.UNKNOWN)
76
            return null;
77

    
78
        HostDescriptor hd = new HostDescriptor(url);
79
        return new LSIDMetadataPort[] {
80
                        // thau added http metadata port here
81
                        new HTTPLocation(hd.host, hd.port,
82
                                         hd.pathPrefix + "/authority/metadata"),
83
                        new SOAPLocation(hd.baseURL + "metadata")};
84
    }
85

    
86
    public LSIDDataPort[] getDataLocations(LSID lsid, String url)
87
    {
88
        logger.debug("In LSIDAuthorityMain.getDataLocations()");
89
        if (lookup == null)
90
            return null;
91

    
92
        int lsType;
93
        try {
94
            lsType = lookup.lsidType(lsid);
95
        } catch (LSIDServerException ex) {
96
            ex.printStackTrace();
97
            lsType = LSIDDataLookup.UNKNOWN;
98
        }
99
        if (lsType == LSIDDataLookup.UNKNOWN)
100
            return null;
101
        if (lsType == LSIDDataLookup.ABSTRACT)
102
            return new LSIDDataPort[0];
103

    
104
        HostDescriptor hd = new HostDescriptor(url);
105
        return new LSIDDataPort[] {
106
                        new HTTPLocation(hd.host, hd.port, hd.pathPrefix
107
                                                           + "/authority/data"),
108
                        new SOAPLocation(hd.baseURL + "data")};
109
    }
110

    
111
    private static final Pattern HOST_PTN = Pattern
112
                    .compile("https?://([^/:]+)(?::(\\d+))?(.*)/authority(.*)");
113

    
114
    /* Q&D implementation */
115
    private class HostDescriptor
116
    {
117
        public String host;
118
        public int port;
119
        public String pathPrefix;
120
        public String baseURL;
121

    
122
        public HostDescriptor(String url)
123
        {
124
            logger.debug("Creating a HostDescriptor for: " + url);
125
            // Thread.dumpStack();
126
            host = "localhost";
127
            port = -1;
128
            pathPrefix = "";
129
            if (url != null || url.length() > 0) {
130
                logger.debug("HostDescriptor: url is > 0 length");
131
                Matcher m = HOST_PTN.matcher(url);
132
                if (m.lookingAt()) {
133
                    host = m.group(1);
134
                    logger.debug("HostDescriptor.host: " + host);
135
                    if ((m.group(2) != null) && (m.group(2).length() > 0)) {
136
                        port = Integer.parseInt(m.group(2));
137
                    }
138
                    logger.debug("HostDescriptor.port: " + port);
139
                    pathPrefix = m.group(3);
140
                    logger.debug("HostDescriptor.pathPrefix: " + pathPrefix);
141
                }
142
            }
143
            if (port > 0) {
144
                baseURL = "http://" + host + ":" + port + pathPrefix
145
                          + "/authority/";
146
            } else {
147
                baseURL = "http://" + host + pathPrefix + "/authority/";
148
            }
149
            logger.debug("HostDescriptor.baseURL: " + baseURL);
150
        }
151
    }
152
}
(2-2/4)