Project

General

Profile

« Previous | Next » 

Revision 2822

Added by Duane Costa over 18 years ago

Eliminate hard-coding of the knb context from the advanced search code. Advanced search servlet determines the metacat context name by reading the metacat.properties file at servlet initialization.

View differences:

src/edu/ucsb/nceas/metacat/advancedsearch/AdvancedSearchServlet.java
24 24

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

  
27
import java.io.File;
27 28
import java.io.IOException;
28
import java.util.Enumeration;
29 29

  
30 30
import javax.servlet.RequestDispatcher;
31
import javax.servlet.ServletConfig;
31 32
import javax.servlet.ServletContext;
32 33
import javax.servlet.ServletException;
33 34
import javax.servlet.http.HttpServlet;
......
36 37
import javax.servlet.http.HttpSession;
37 38

  
38 39
import edu.ucsb.nceas.metacat.client.*;
40
import edu.ucsb.nceas.utilities.Options;
39 41

  
40 42
/** 
41 43
 * @author dcosta
......
44 46
 */
45 47
public class AdvancedSearchServlet extends HttpServlet {
46 48

  
47
  // Instance Variables -- (Not used because they are not thread-safe.)
49
  /*
50
   * Class fields
51
   */
52
  private static final String CONFIG_DIR = "WEB-INF";
53
  private static final String CONFIG_NAME = "metacat.properties";
54
	   
55
  // Instance Variables
56
  private String contextString = "";
48 57

  
49 58
  // Methods
50 59
  
......
74 83
      MetacatHelper metacatHelper = new MetacatHelper();
75 84
      String serverName = request.getServerName();
76 85
      int serverPort = request.getServerPort();
77
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
86
      metacatURL = 
87
    	  metacatHelper.constructMetacatURL(serverName, serverPort, contextString);
78 88
    }
79 89

  
80 90
    // Get the advancedSearchBean object that has been loaded up with user
......
93 103
    dispatcher.forward(request, response);
94 104
  }
95 105
  
106
  /**
107
   * Initializes the servlet. Reads properties and initializes object fields.
108
   * 
109
   * @throws ServletException
110
   */
111
  public void init(ServletConfig config) throws ServletException {
112
	ServletContext context = null;
113
    String dirPath;
114
    Options options = null;
115

  
116
    super.init(config);
117
    context = config.getServletContext();
118
    dirPath = context.getRealPath(CONFIG_DIR);
119
    File propertyFile = new File(dirPath, CONFIG_NAME);
120
    
121
    try {
122
      options = Options.initialize(propertyFile);
123
    }
124
    catch (IOException e) {
125
      System.err.println("Error in loading options: " + e.getMessage());
126
    }
127
    
128
    this.contextString = options.getOption("context");
129
  }
130

  
96 131
}
src/edu/ucsb/nceas/metacat/advancedsearch/BrowseServlet.java
24 24

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

  
27
import java.io.File;
27 28
import java.io.IOException;
28 29

  
29 30
import javax.servlet.RequestDispatcher;
31
import javax.servlet.ServletConfig;
30 32
import javax.servlet.ServletContext;
31 33
import javax.servlet.ServletException;
32 34
import javax.servlet.http.HttpServlet;
......
35 37
import javax.servlet.http.HttpSession;
36 38

  
37 39
import edu.ucsb.nceas.metacat.client.*;
40
import edu.ucsb.nceas.utilities.Options;
38 41

  
39 42
/** 
40 43
 * @author dcosta
......
44 47
 */
45 48
public class BrowseServlet extends HttpServlet {
46 49

  
47
  // Instance Variables -- (Not used because they are not thread-safe.)
50
  /*
51
   * Class fields
52
   */
53
  private static final String CONFIG_DIR = "WEB-INF";
54
  private static final String CONFIG_NAME = "metacat.properties";
55
		   
56
  // Instance Variables
57
  private String contextString = "";
48 58

  
49 59
  // Methods
50 60
  
......
74 84
      MetacatHelper metacatHelper = new MetacatHelper();
75 85
      String serverName = request.getServerName();
76 86
      int serverPort = request.getServerPort();
77
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
87
      metacatURL = 
88
    	  metacatHelper.constructMetacatURL(serverName, serverPort, contextString);
78 89
    }
79 90

  
80 91
    // Tell the web server that the response is HTML
......
94 105
    dispatcher.forward(request, response);
95 106
  }
96 107
  
108
  /**
109
   * Initializes the servlet. Reads properties and initializes object fields.
110
   * 
111
   * @throws ServletException
112
   */
113
  public void init(ServletConfig config) throws ServletException {
114
	ServletContext context = null;
115
    String dirPath;
116
    Options options = null;
117

  
118
    super.init(config);
119
    context = config.getServletContext();
120
    dirPath = context.getRealPath(CONFIG_DIR);
121
    File propertyFile = new File(dirPath, CONFIG_NAME);
122
    
123
    try {
124
      options = Options.initialize(propertyFile);
125
    }
126
    catch (IOException e) {
127
      System.err.println("Error in loading options: " + e.getMessage());
128
    }
129
    
130
    this.contextString = options.getOption("context");
131
  }
132

  
97 133
}
src/edu/ucsb/nceas/metacat/advancedsearch/SearchServlet.java
24 24

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

  
27
import java.io.File;
27 28
import java.io.IOException;
28 29
import javax.servlet.RequestDispatcher;
30
import javax.servlet.ServletConfig;
29 31
import javax.servlet.ServletContext;
30 32
import javax.servlet.ServletException;
31 33
import javax.servlet.http.HttpServlet;
......
34 36
import javax.servlet.http.HttpSession;
35 37

  
36 38
import edu.ucsb.nceas.metacat.client.*;
39
import edu.ucsb.nceas.utilities.Options;
37 40

  
38 41
/** 
39 42
 * @author dcosta
......
43 46
 */
44 47
public class SearchServlet extends HttpServlet {
45 48

  
46
  // Instance Variables -- (Not used because they are not thread-safe.)
49
  /*
50
   * Class fields
51
   */
52
  private static final String CONFIG_DIR = "WEB-INF";
53
  private static final String CONFIG_NAME = "metacat.properties";
54
			   
55
  // Instance Variables
56
  private String contextString = "";
47 57

  
48 58
  // Methods
49 59
  
......
73 83
      MetacatHelper metacatHelper = new MetacatHelper();
74 84
      String serverName = request.getServerName();
75 85
      int serverPort = request.getServerPort();
76
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
86
      metacatURL = 
87
          metacatHelper.constructMetacatURL(serverName, serverPort, contextString);
77 88
    }
78 89

  
79 90
    // Tell the web server that the response is HTML
......
93 104
    dispatcher.forward(request, response);
94 105
  }
95 106
  
107

  
108
  /**
109
   * Initializes the servlet. Reads properties and initializes object fields.
110
   * 
111
   * @throws ServletException
112
   */
113
  public void init(ServletConfig config) throws ServletException {
114
	ServletContext context = null;
115
    String dirPath;
116
    Options options = null;
117

  
118
    super.init(config);
119
    context = config.getServletContext();
120
    dirPath = context.getRealPath(CONFIG_DIR);
121
    File propertyFile = new File(dirPath, CONFIG_NAME);
122
    
123
    try {
124
      options = Options.initialize(propertyFile);
125
    }
126
    catch (IOException e) {
127
      System.err.println("Error in loading options: " + e.getMessage());
128
    }
129
    
130
    this.contextString = options.getOption("context");
131
  }
132

  
96 133
}
src/edu/ucsb/nceas/metacat/advancedsearch/AdvancedSearch.java
933 933
   * @param argv[0]   The server name, e.g. "earth.lternet.edu"
934 934
   * @param argv[1]   The server port, e.g. "8080", or 0 if no port needs
935 935
   *                    to be specified.
936
   * @param argv[2]   The path to the resultset.xsl stylesheet, e.g.
936
   * @param argv[2]   The context string, e.g. "knb"
937
   * @param argv[3]   The path to the resultset.xsl stylesheet, e.g.
937 938
   *                    "C:/Tomcat5/webapps/query/style/common/resultset.xsl"
938 939
   */
939 940
  public static void main(String[] argv) {
940 941
    AdvancedSearch advancedSearch;
941 942
    AdvancedSearchBean advancedSearchBean = new AdvancedSearchBean();
943
    String contextString = argv[2];
942 944
    String htmlString = "";
943 945
    MetacatHelper metacatHelper = new MetacatHelper();
944 946
    String metacatURL;
945 947
    final String serverName = argv[0];
946 948
    final Integer serverPortInteger = new Integer(argv[1]);
947 949
    final int serverPort = serverPortInteger.intValue();
948
    final String xslPath = argv[2];
950
    final String xslPath = argv[3];
949 951

  
950 952
    advancedSearchBean.setSubjectField("ALL");
951 953
    advancedSearchBean.setSubjectValue("bird");    
......
962 964
    //advancedSearchBean.setStartDate("2001-01-01");
963 965
    //advancedSearchBean.setEndDate("2001-07-01");
964 966
    advancedSearch = new AdvancedSearch(advancedSearchBean);
965
    metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
967
    metacatURL = 
968
        metacatHelper.constructMetacatURL(serverName, serverPort, contextString);
966 969
    htmlString =advancedSearch.executeAdvancedSearch(metacatURL, null, xslPath);
967 970
  }
968 971
  
src/edu/ucsb/nceas/metacat/advancedsearch/LoginServlet.java
24 24

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

  
27
import java.io.File;
27 28
import java.io.IOException;
28 29

  
29 30
import javax.servlet.RequestDispatcher;
31
import javax.servlet.ServletConfig;
30 32
import javax.servlet.ServletContext;
31 33
import javax.servlet.ServletException;
32 34
import javax.servlet.http.HttpServlet;
......
35 37
import javax.servlet.http.HttpSession;
36 38

  
37 39
import edu.ucsb.nceas.metacat.client.*;
40
import edu.ucsb.nceas.utilities.Options;
38 41

  
39 42
/** 
40 43
 * @author dcosta
......
43 46
 */
44 47
public class LoginServlet extends HttpServlet {
45 48

  
46
  // Instance Variables -- (Not used because they are not thread-safe.)
49
  /*
50
   * Class fields
51
   */
52
  private static final String CONFIG_DIR = "WEB-INF";
53
  private static final String CONFIG_NAME = "metacat.properties";
54
				   
55
  // Instance Variables
56
  private String contextString = "";
47 57

  
48 58
  // Methods
49 59
  
......
74 84
      MetacatHelper metacatHelper = new MetacatHelper();
75 85
      String serverName = request.getServerName();
76 86
      int serverPort = request.getServerPort();
77
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
87
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort, "knb");
78 88
    }
79 89
    
80 90
    if (metacat == null) {
......
107 117
    dispatcher.forward(request, response);
108 118
  }
109 119

  
120

  
121
  /**
122
   * Initializes the servlet. Reads properties and initializes object fields.
123
   * 
124
   * @throws ServletException
125
   */
126
  public void init(ServletConfig config) throws ServletException {
127
	ServletContext context = null;
128
    String dirPath;
129
    Options options = null;
130

  
131
    super.init(config);
132
    context = config.getServletContext();
133
    dirPath = context.getRealPath(CONFIG_DIR);
134
    File propertyFile = new File(dirPath, CONFIG_NAME);
135
    
136
    try {
137
      options = Options.initialize(propertyFile);
138
    }
139
    catch (IOException e) {
140
      System.err.println("Error in loading options: " + e.getMessage());
141
    }
142
    
143
    this.contextString = options.getOption("context");
144
  }
145

  
110 146
}
src/edu/ucsb/nceas/metacat/advancedsearch/MetacatHelper.java
51 51
  
52 52

  
53 53
  /**
54
   * Constructs a URL to the metacat servlet. Assumes that metacat is always
55
   * configured to run in the "knb" context (the defacto standard).
54
   * Constructs a URL to the metacat servlet.
56 55
   * 
57 56
   * @param serverName   A server name, e.g. "prairie.lternet.edu"
58 57
   * @param serverPort   A server port, e.g. 8080. If no port is required in
59 58
   *                     the URL, pass a 0 and the argument will be ignored.
59
   * @param contextString The context under which metacat is running, e.g. "knb".
60 60
   * @return metacatURL  The URL to the metacat servlet.
61 61
   */
62 62
  public String constructMetacatURL(final String serverName, 
63
                                    final int serverPort) {
63
                                    final int serverPort,
64
                                    final String contextString) {
64 65
    String metacatURL = "http://" + serverName;
65 66
    
66 67
    if (serverPort > 0) {
67 68
      final Integer serverPortInteger = new Integer(serverPort);
68 69
      final String serverPortString = serverPortInteger.toString();
69
      metacatURL += ":" + serverPortString + "/knb/metacat";
70
      metacatURL += ":" + serverPortString;
70 71
    }
71
    else {
72
      metacatURL += "/knb/metacat";
73
    }
74 72
    
73
    metacatURL += "/" + contextString + "/metacat";
74

  
75 75
    return metacatURL;
76 76
  }
77 77

  

Also available in: Unified diff