Project

General

Profile

« Previous | Next » 

Revision 5076

Added by daigle over 14 years ago

Change RequestUtil forwardRequest() method to throw MetacatUtilException.

View differences:

RequestUtil.java
30 30
import java.io.IOException;
31 31
import java.io.BufferedReader;
32 32
import java.io.PrintWriter;
33
import java.io.StringReader;
34 33
import java.net.MalformedURLException;
35 34
import java.net.URL;
36 35
import java.net.URLConnection;
......
52 51
import org.apache.commons.httpclient.methods.PostMethod;
53 52
import org.apache.log4j.Logger;
54 53

  
54
import edu.ucsb.nceas.metacat.properties.PropertyService;
55 55
import edu.ucsb.nceas.metacat.service.SessionService;
56 56
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
57
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
57 58

  
58 59
public class RequestUtil {
59 60
	
......
75 76
	 *            that can be used for writing output to the client
76 77
	 * @param destination
77 78
	 *            the context-relative URL to which the request is forwarded
79
	 * @param params the request parameters.  these will be added to the request
78 80
	 */
79
	public static void forwardRequest(HttpServletRequest request,
80
			HttpServletResponse response, String destinationUrl, Hashtable<String, String[]> params)
81
			throws IOException, ServletException {
81
	public static void forwardRequest(HttpServletRequest request, HttpServletResponse response, 
82
			String destinationUrl, Hashtable<String, String[]> params) throws MetacatUtilException {
82 83

  
83 84
		destinationUrl += "?" + paramsToQuery(params);
84 85
		
......
86 87
		ServletContext servletContext = request.getSession()
87 88
				.getServletContext();
88 89

  
89
		servletContext.getRequestDispatcher(destinationUrl).forward(request,
90
				response);
90
		try {
91
			servletContext.getRequestDispatcher(destinationUrl).forward(request, response);
92
		}  catch (IOException ioe) {
93
			throw new MetacatUtilException("RequestUtil.forwardRequest - I/O error when forwarding to " + 
94
					destinationUrl + " : " + ioe.getMessage());			
95
		} catch (ServletException se) {
96
			throw new MetacatUtilException("RequestUtil.forwardRequest - Servlet error when forwarding to " + 
97
					destinationUrl + " : " + se.getMessage());			
98
		}
91 99
	}
100
	
101
	/**
102
	 * Forward a request that was received by this servlet on to another JSP
103
	 * page or servlet to continue handling the request.  In this case, the page
104
	 * must be referenced in a paramter named "forwardto".  If the qformat is 
105
	 * provided, the file will be retrieved from that skin.  Otherwise, the file 
106
	 * will be retrieved from the system default skin.
107
	 * 
108
	 * For more specific file location, use: forwardRequest(request,response, destinationUrl, params)
109
	 * 
110
	 * @param request
111
	 *            to be forwarded
112
	 * @param response
113
	 *            that can be used for writing output to the client
114
	 * @param params
115
	 *            the request parameters.  these will be added to the request.
116
	 */
117
	public static void forwardRequest(HttpServletRequest request, HttpServletResponse response, 
118
			Hashtable<String, String[]> params) throws MetacatUtilException {
92 119

  
120
		String forwardTos[] = params.get("forwardto");
121
		if (forwardTos == null || forwardTos[0].equals("")) {
122
			throw new MetacatUtilException("RequestUtil.forwardRequest - forwardto must be set in parameters when forwarding.");			
123
		}
124
		
125
		String forwardTo = forwardTos[0];
126
		String qformat = null;
127
		
128
		String qformats[] = params.get("qformat");
129
		if (qformats == null || qformats.length == 0) {
130
			try {
131
				qformat = PropertyService.getProperty("application.default-style");
132
			} catch (PropertyNotFoundException pnfe) {
133
				qformat = "default";
134
				logMetacat.warn("RequestUtil.forwardRequest - could not get property " + 
135
						"'application.default-style'. Using 'default'");
136
			}
137
		} else {
138
			qformat = qformats[0];
139
		}
140
		
141
		String destinationUrl = "/style/skins/" + qformat + "/" + forwardTo;
142
		destinationUrl += "?" + paramsToQuery(params);
143
		
144
		logMetacat.debug("RequestUtil.forwardRequest - Forwarding request to " + destinationUrl);
145
		ServletContext servletContext = request.getSession()
146
				.getServletContext();
147
		try {
148
			servletContext.getRequestDispatcher(destinationUrl).forward(request, response);
149
		} catch (IOException ioe) {
150
			throw new MetacatUtilException("RequestUtil.forwardRequest - I/O error when forwarding to " + 
151
					destinationUrl + " : " + ioe.getMessage());			
152
		} catch (ServletException se) {
153
			throw new MetacatUtilException("RequestUtil.forwardRequest - Servlet error when forwarding to " + 
154
					destinationUrl + " : " + se.getMessage());			
155
		}
156
	}
157
	
158

  
159

  
93 160
	/**
94 161
	 * Post a request and return the response body
95 162
	 * 
......
251 318
	 * @param cookieName
252 319
	 *            the name of the cookie to look for
253 320
	 */
321
	@SuppressWarnings("unchecked")
254 322
	public static Hashtable<String, String[]> getParameters(HttpServletRequest request)  {
255 323
		Hashtable<String, String[]> params = new Hashtable<String, String[]>();
256 324
		

Also available in: Unified diff