Project

General

Profile

« Previous | Next » 

Revision 473

Added by berkley about 24 years ago

added support for urls of the form "metacat://server.xyz.com?docid=XXX:yy"

View differences:

src/edu/ucsb/nceas/metacat/metacatURL.java
15 15
package edu.ucsb.nceas.metacat;
16 16

  
17 17
import java.net.MalformedURLException;
18
import java.util.Hashtable;
19
import java.util.Enumeration;
18 20

  
19 21
public class metacatURL
20 22
{
21 23
  private String[][] params = new String[200][2];
24
  private Hashtable paramsHash = new Hashtable();
22 25
  private String urlType = null;
26
  private String host = null;
23 27
  private String url;
24 28
  
25 29
  /**
......
63 67
    
64 68
    int urlTypeIndex = url.indexOf("://"); //anything before this is the urltype
65 69
    this.urlType = url.substring(0, urlTypeIndex);
70
    paramsHash.put("urlType", this.urlType);
71
    
66 72
    if(this.urlType.equals("http"))
67 73
    {//if this is an http url
68 74
      params[0][0] = "httpurl";
69 75
      params[0][1] = url.substring(0, url.length());
76
      paramsHash.put(params[0][0], params[0][1]);
70 77
      for(int i=url.length()-1; i>0; i--)
71 78
      {
72 79
        if(url.charAt(i) == '/')
......
83 90
      }
84 91
      params[1][0] = "filename";
85 92
      params[1][1] = temp;
93
      paramsHash.put(params[1][0], params[1][1]);
86 94
    }
87 95
    else
88 96
    {//other urls that meet the metacat type url structure.
89
      for(int i=urlTypeIndex + 3; i<url.length(); i++)
97
      int hostIndex = url.indexOf("?");
98
      this.host = url.substring(urlTypeIndex + 3, hostIndex);
99
      paramsHash.put("host", this.host);
100
      for(int i=hostIndex + 1; i<url.length(); i++)
90 101
      { //go throught the remainder of the url one character at a time.
91
        if(url.charAt(i) == '#')
102
        if(url.charAt(i) == '=')
92 103
        { //if the current char is a # then the preceding should be a parametet
93 104
          //name
94 105
          if(!poundflag && ampflag)
......
108 119
        { //the text preceding the & should be the param value.
109 120
          if(i == url.length() - 1)
110 121
          { //if we are at the end of the string grab the last value and append it.
111
            if(url.charAt(i) != '&')
122
            if(url.charAt(i) != '=')
112 123
            { //ignore an extra & on the end of the string
113 124
              temp += url.charAt(i);
114 125
            }
......
117 128
          if(!ampflag && poundflag)
118 129
          {
119 130
            params[arrcount][1] = temp.trim();
131
            paramsHash.put(params[arrcount][0], params[arrcount][1]);
120 132
            temp = "";
121 133
            arrcount++; //increment the array to the next row.
122 134
          }
......
153 165
    return this.params;
154 166
  }
155 167
  
168
  /** 
169
   * Returns the parameters in a hashtable.
170
   */
171
  public Hashtable getHashParams()
172
  {
173
    return this.paramsHash;
174
  }
175
  
156 176
  /**
177
   * returns a single parameter from the hash by name
178
   * @param paramname the name of the parameter to return.
179
   */
180
  public Object getHashParam(String paramname)
181
  {
182
    return this.paramsHash.get(paramname);
183
  }
184
  
185
  /**
157 186
   * returns a string representation of this metacatURL
158 187
   */
159 188
  public String toString()
......
161 190
    return this.url;
162 191
  }
163 192
  
193
  public void printHashParams()
194
  {
195
    Enumeration e = this.paramsHash.keys();
196
    System.out.println("name          value");
197
    System.out.println("-------------------");
198
    while(e.hasMoreElements())
199
    {
200
      String key = (String)e.nextElement();
201
      System.out.print(key);
202
      System.out.print("          ");
203
      System.out.println((String)this.paramsHash.get(key));
204
    }
205
  }
206
  
164 207
  /**
165 208
   * Prints the parameters neatly to system.out
166 209
   */
......
204 247
   */
205 248
  public static void main(String args[])
206 249
  {
207
    String testurl =  "metacat://docid#NCEAS:2&docid#NCEAS:5&username#chad";
250
    String testurl =  "metacat://dev.nceas.ucsb.edu?docid=NCEAS:10&username=chad&pasword=xyz";
251
    String testurl2 = "http://dev.nceas.ucsb.edu/berkley/testdata.dat";
208 252
    try
209 253
    {
210 254
      metacatURL murl = new metacatURL(testurl);
255
      metacatURL murl2 = new metacatURL(testurl2);
211 256
      String[][] p = null;
212 257
      System.out.println("url type: " + murl.getURLType());
213 258
      System.out.println("parameters: ");
214 259
      p = murl.getParams();
215
      System.out.println("name          value");
216
      System.out.println("-------------------");
217
      for(int i=0; i<p.length; i++)
218
      {
219
        if(p[i][0] != null)
220
        {
221
          System.out.print(p[i][0]);
222
          System.out.print("          ");
223
          System.out.print(p[i][1]);
224
          System.out.println();
225
        }
226
      }
260
      Hashtable h = murl.getHashParams();
261
      murl.printParams();
262
      murl2.printParams();
263
      System.out.println("hash params: " );
264
      murl.printHashParams();
265
      murl2.printHashParams();
227 266
    }
228 267
    catch(MalformedURLException murle)
229 268
    {
src/edu/ucsb/nceas/metacat/DBQuery.java
176 176
          
177 177
          if(returndocVec.size() != 0 && !returndocVec.contains(doctype))
178 178
          { //there are returndocs to match (backtracking can now be performed). 
179
            //System.out.println("olddoctype: " + doctype);
179
  System.out.println("olddoctype: " + doctype);
180 180
            StringBuffer btBuf = new StringBuffer();
181 181
            btBuf.append("select object from xml_relation where ");
182 182
            btBuf.append("objdoctype in (");
......
191 191
            }
192 192
            btBuf.append(") ");
193 193
            btBuf.append("and subject like '");
194
            btBuf.append("metacat://docid#").append(docid).append("'");
194
            btBuf.append("metacat://").append(util.getOption("server"));
195
            btBuf.append("?docid=").append(docid).append("'");
195 196
            pstmt = conn.prepareStatement(btBuf.toString());
196 197
            pstmt.execute();
197 198
            ResultSet btrs = pstmt.getResultSet();
......
199 200
            if(hasBtRows)
200 201
            { //there was a backtrackable document found
201 202
              DocumentImpl xmldoc = null;
202
              //System.out.println("document found is: " + btrs.getString(1));
203
   System.out.println("document found is: " + btrs.getString(1));
203 204
              metacatURL objURL = new metacatURL(btrs.getString(1));
204 205
              try
205 206
              {
......
302 303
        Enumeration docidkeys = docListResult.keys();
303 304
        while(docidkeys.hasMoreElements())
304 305
        {
306
          String connstring = "metacat://"+util.getOption("server")+"?docid=";
305 307
          String docidkey = (String)docidkeys.nextElement();
306 308
          pstmt = conn.prepareStatement(
307
                  qspec.printRelationSQL("metacat://docid#" + docidkey));
309
                  qspec.printRelationSQL(connstring + docidkey));
308 310
          pstmt.execute();
309 311
          rs = pstmt.getResultSet();
310 312
          tableHasRows = rs.next();
......
715 717

  
716 718
/**
717 719
 * '$Log$
720
 * 'Revision 1.21  2000/09/26 22:06:51  berkley
721
 * 'Added backtrack functionality.  Backtracking works by passing a returndoc parameter.  There can be more than one.  If a document that is hit by a query is not of type returndoc then it searches the database for a related file of type returndoc.  If one is found it is displayed, if no relation is found, the original is displayed.
722
 * '
723
 * 'Support was also added for an index of relations.  the table xml_relation handles the all of the relation indexing.
724
 * '
718 725
 * 'Revision 1.20  2000/09/15 19:52:11  berkley
719 726
 * 'Added functionality for package specifications.  metacatservlet now contains a new action called getrelateddocument that handles retrieving related documents using the metacatURL specification (metacatURL.java).  DBQuery contains new code in runQuery that embeds relation tags in the returned hashtable describing the documents related to each docid.  querySpecification contains a new method which prints the sql that does the relation query.
720 727
 * '
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
667 667
        metacatURL murl = new metacatURL(((String[])params.get("url"))[0]);
668 668
        if(murl.getURLType().equals("metacat"))
669 669
        {//get the document from the database if it is the right type of url
670
          String[] murlParams = murl.getParam(0);
671
          if(murlParams[0].equals("docid"))
670
          Hashtable murlParams = murl.getHashParams();
671
          if(murlParams.containsKey("docid"))
672 672
          {//the docid should be first
673
            //murl.printParams();
674
            docid = murlParams[1]; //get the docid value
673
            docid = (String)murlParams.get("docid"); //get the docid value
675 674
            conn = util.getConnection();
676 675
            xmldoc = new DocumentImpl(conn, docid);
677 676
            
......
679 678
            //the style sheet handling code needs to be put here. 
680 679
            out.println(xmldoc.toString());
681 680
            //**************************************************
681
            util.returnConnection(conn);
682 682
          }
683 683
          else
684 684
          {
......
688 688
        }
689 689
        else if(murl.getURLType().equals("http"))
690 690
        {//get the document from the internet
691
          String[] murlParams = murl.getParam(0);
692
          if(murlParams[0].equals("httpurl"))
691
          Hashtable murlParams = murl.getHashParams();
692
          if(murlParams.containsKey("httpurl"))
693 693
          {//httpurl is the param name for an http url.
694
            URL urlconn = new URL(murlParams[1]);  //create a new url obj.
694
            URL urlconn = new URL((String)murlParams.get("httpurl"));  
695
            //create a new url obj.
695 696
            //DataInputStream htmldoc = new DataInputStream(urlconn.openStream());
696 697
            BufferedReader htmldoc = new BufferedReader(
697 698
                                   new InputStreamReader(urlconn.openStream()));
......
1021 1022
      if(murl.getURLType().equals("metacat"))
1022 1023
      {
1023 1024
        //get the document from the database
1024
        xmldoc = new DocumentImpl(conn, (murl.getParam(0))[1]);
1025
        xmldoc = new DocumentImpl(conn, (String)murl.getHashParam("docid"));
1025 1026
        bytestring = (xmldoc.toString()).getBytes();
1026
        zentry = new ZipEntry((murl.getParam(0))[1] + ".xml");
1027
        zentry = new ZipEntry(murl.getHashParam("docid") + ".xml");
1027 1028
        //create a new zip entry and write the file to the stream
1028 1029
        zentry.setSize(bytestring.length);
1029 1030
        zout.putNextEntry(zentry);
......
1032 1033
      }
1033 1034
      else if(murl.getURLType().equals("http"))
1034 1035
      {
1035
        String[] murlParams = murl.getParam(0);
1036
        if(murlParams[0].equals("httpurl"))
1036
        Hashtable murlParams = murl.getHashParams();
1037
        if(murlParams.containsKey("httpurl"))
1037 1038
        {//httpurl is the param name for an http url.
1038
          URL urlconn = new URL(murlParams[1]);  //create a new url obj.
1039
          URL urlconn = new URL((String)murlParams.get("httpurl"));  
1040
          //create a new url obj.
1039 1041
          BufferedReader htmldoc = new BufferedReader(
1040 1042
                                   new InputStreamReader(urlconn.openStream()));
1041 1043
          //get the data from the web server
1042 1044
          try
1043 1045
          { //zip the document
1044 1046
            String line=null;
1045
            zentry = new ZipEntry((murl.getParam(1))[1]);
1047
            zentry = new ZipEntry((String)murlParams.get("filename"));
1046 1048
            //get just the filename from the URL.
1047 1049
            zout.putNextEntry(zentry);
1048 1050
            //make a new entry in the zip file stream

Also available in: Unified diff