Project

General

Profile

1 2737 jones
/**
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$'
7
 *     '$Date$'
8
 * '$Revision$'
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 2818 jones
import java.util.regex.Matcher;
28 2737 jones
import java.util.regex.Pattern;
29
30 2818 jones
import org.apache.commons.logging.Log;
31
import org.apache.commons.logging.LogFactory;
32
33
import com.ibm.lsid.ExpiringResponse;
34 2737 jones
import com.ibm.lsid.LSID;
35 2818 jones
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 2737 jones
import com.ibm.lsid.wsdl.LSIDDataPort;
40
import com.ibm.lsid.wsdl.LSIDMetadataPort;
41
import com.ibm.lsid.wsdl.SOAPLocation;
42
43 2819 jones
public class LSIDAuthorityMain extends SimpleAuthority
44
{
45 2737 jones
46 2819 jones
    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 2818 jones
        logger.info("Starting LSIDAuthorityMain.");
53 2819 jones
        lookup = new LSIDDataLookup();
54
    }
55 2737 jones
56 2819 jones
    public ExpiringResponse getKnownURIs() throws LSIDServerException
57
    {
58 2818 jones
        logger.debug("In LSIDAuthorityMain.getKnownURIs()");
59 2819 jones
        return null;
60
    }
61 2737 jones
62 2819 jones
    public LSIDMetadataPort[] getMetadataLocations(LSID lsid, String url)
63
    {
64 2818 jones
        logger.debug("In LSIDAuthorityMain.getMetadataLocations()");
65
        if (lookup == null)
66 2819 jones
            return null;
67 2737 jones
68 2819 jones
        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 2737 jones
78 2819 jones
        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 2737 jones
86 2819 jones
    public LSIDDataPort[] getDataLocations(LSID lsid, String url)
87
    {
88 2818 jones
        logger.debug("In LSIDAuthorityMain.getDataLocations()");
89 2819 jones
        if (lookup == null)
90
            return null;
91 2737 jones
92 2819 jones
        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 2737 jones
104 2819 jones
        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 2818 jones
            logger.debug("Creating a HostDescriptor for: " + url);
125 2819 jones
            host = "localhost";
126
            port = -1;
127
            pathPrefix = "";
128
            if (url != null || url.length() > 0) {
129 2818 jones
                logger.debug("HostDescriptor: url is > 0 length");
130 2819 jones
                Matcher m = HOST_PTN.matcher(url);
131
                if (m.lookingAt()) {
132
                    host = m.group(1);
133 2818 jones
                    logger.debug("HostDescriptor.host: " + host);
134 2819 jones
                    if ((m.group(2) != null) && (m.group(2).length() > 0)) {
135
                        port = Integer.parseInt(m.group(2));
136 2818 jones
                    }
137
                    logger.debug("HostDescriptor.port: " + port);
138 2819 jones
                    pathPrefix = m.group(3);
139 2818 jones
                    logger.debug("HostDescriptor.pathPrefix: " + pathPrefix);
140 2819 jones
                }
141
            }
142
            if (port > 0) {
143
                baseURL = "http://" + host + ":" + port + pathPrefix
144
                          + "/authority/";
145 2818 jones
            } else {
146 2819 jones
                baseURL = "http://" + host + pathPrefix + "/authority/";
147 2818 jones
            }
148
            logger.debug("HostDescriptor.baseURL: " + baseURL);
149 2819 jones
        }
150
    }
151 2737 jones
}