KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > httpconnector > HttpUtil


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.httpconnector;
17
18 import org.mortbay.http.HttpResponse;
19 import org.mortbay.http.HttpRequest;
20 import org.outerx.daisy.x10.ErrorDocument;
21 import org.outerx.daisy.x10.CauseType;
22 import org.outerj.daisy.repository.RepositoryException;
23 import org.outerj.daisy.repository.Repository;
24 import org.outerj.daisy.repository.variant.Branch;
25 import org.outerj.daisy.repository.variant.Language;
26 import org.outerj.daisy.repository.variant.BranchNotFoundException;
27 import org.outerj.daisy.repository.variant.LanguageNotFoundException;
28
29 import java.io.IOException JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 public class HttpUtil {
34     public static void sendCustomError(String JavaDoc message, int code, HttpResponse response)
35         throws IOException JavaDoc
36     {
37         response.setStatus(code);
38         response.setContentType("text/xml");
39         ErrorDocument errorDocument = ErrorDocument.Factory.newInstance();
40         ErrorDocument.Error errorXml = errorDocument.addNewError();
41         errorXml.setDescription(message);
42         errorDocument.save(response.getOutputStream());
43         response.commit();
44     }
45
46     public static void sendCustomError(Throwable JavaDoc throwable, int code, HttpResponse response)
47         throws IOException JavaDoc
48     {
49         response.setStatus(code);
50         response.setContentType("text/xml");
51
52         ErrorDocument errorDocument = buildErrorXml(throwable);
53         errorDocument.save(response.getOutputStream());
54         response.commit();
55     }
56
57     public static ErrorDocument buildErrorXml(Throwable JavaDoc throwable) {
58         ErrorDocument errorDocument = ErrorDocument.Factory.newInstance();
59         ErrorDocument.Error errorXml = errorDocument.addNewError();
60         CauseType causeXml = errorXml.addNewCause();
61         createCause(throwable, causeXml);
62         return errorDocument;
63     }
64
65     private static CauseType createCause(Throwable JavaDoc throwable, CauseType causeXml) {
66         CauseType.Exception exceptionXml = causeXml.addNewException();
67         exceptionXml.setType(throwable.getClass().getName());
68         exceptionXml.setMessage(throwable.getMessage());
69
70         if (throwable instanceof RepositoryException) {
71             RepositoryException repositoryException = (RepositoryException)throwable;
72             Map JavaDoc state = repositoryException.getState();
73             if (state != null) {
74                 CauseType.ExceptionData exceptionData = causeXml.addNewExceptionData();
75
76                 Iterator JavaDoc stateIt = state.entrySet().iterator();
77                 while (stateIt.hasNext()) {
78                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)stateIt.next();
79                     CauseType.ExceptionData.Parameter parameter = exceptionData.addNewParameter();
80                     parameter.setName((String JavaDoc)entry.getKey());
81                     parameter.setValue((String JavaDoc)entry.getValue());
82                 }
83             }
84         }
85
86         CauseType.StackTrace stackTraceXml = causeXml.addNewStackTrace();
87         StackTraceElement JavaDoc[] stackTraceElements = throwable.getStackTrace();
88         for (int i = 0; i < stackTraceElements.length; i++) {
89             StackTraceElement JavaDoc stackTraceElement = stackTraceElements[i];
90             CauseType.StackTrace.StackTraceElement stackTraceElementXml = stackTraceXml.addNewStackTraceElement();
91             stackTraceElementXml.setClassName(stackTraceElement.getClassName());
92             stackTraceElementXml.setFileName(stackTraceElement.getFileName());
93             stackTraceElementXml.setLineNumber(stackTraceElement.getLineNumber());
94             stackTraceElementXml.setMethodName(stackTraceElement.getMethodName());
95             stackTraceElementXml.setNativeMethod(stackTraceElement.isNativeMethod());
96         }
97
98         Throwable JavaDoc cause = throwable.getCause();
99         if (cause != null) {
100             CauseType causeXml2 = causeXml.addNewCause();
101             createCause(cause, causeXml2);
102         }
103
104         return causeXml;
105     }
106
107     public static String JavaDoc getStringParam(HttpRequest request, String JavaDoc name) throws Exception JavaDoc {
108         String JavaDoc stringValue = request.getParameter(name);
109         if (stringValue == null || stringValue.equals(""))
110             throw new BadRequestException("Missing request parameter: " + name);
111
112         return stringValue;
113     }
114
115     public static String JavaDoc getStringParam(HttpRequest request, String JavaDoc name, String JavaDoc defaultValue) throws Exception JavaDoc {
116         String JavaDoc stringValue = request.getParameter(name);
117         if (stringValue == null || stringValue.equals(""))
118             return defaultValue;
119
120         return stringValue;
121     }
122
123     public static long getLongParam(HttpRequest request, String JavaDoc name) throws Exception JavaDoc {
124         String JavaDoc stringValue = request.getParameter(name);
125         if (stringValue == null || stringValue.equals(""))
126             throw new BadRequestException("Missing request parameter: " + name);
127
128         try {
129             long longValue = Long.parseLong(stringValue);
130             return longValue;
131         } catch (NumberFormatException JavaDoc e) {
132             throw new BadRequestException("The value of the request parameter \"" + name + "\" should be an integer value, got: " + stringValue);
133         }
134     }
135
136     public static boolean getBooleanParam(HttpRequest request, String JavaDoc name) throws Exception JavaDoc {
137         String JavaDoc stringValue = request.getParameter(name);
138         if (stringValue == null || stringValue.equals(""))
139             throw new BadRequestException("Missing request parameter: " + name);
140
141         return stringValue.equalsIgnoreCase("true");
142     }
143
144     public static long getBranchId(HttpRequest request, Repository repository) throws BadRequestException, RepositoryException {
145         return getBranchId(request, repository, "branch");
146     }
147
148     public static long getBranchId(HttpRequest request, Repository repository, String JavaDoc paramName) throws BadRequestException, RepositoryException {
149         String JavaDoc branchParam = request.getParameter(paramName);
150         if (branchParam == null || branchParam.length() == 0) {
151             return Branch.MAIN_BRANCH_ID;
152         } else if (Character.isDigit(branchParam.charAt(0))) {
153             try {
154                 return Long.parseLong(branchParam);
155             } catch (NumberFormatException JavaDoc e) {
156                 throw new BadRequestException("Invalid value in \"" + paramName + "\" request parameter: " + branchParam);
157             }
158         } else {
159             try {
160                 return repository.getVariantManager().getBranch(branchParam, false).getId();
161             } catch (BranchNotFoundException e) {
162                 throw new BadRequestException("Non-existing branch in \"" + paramName + "\" request parameter: " + branchParam);
163             }
164         }
165     }
166
167     public static long getLanguageId(HttpRequest request, Repository repository) throws BadRequestException, RepositoryException {
168         return getLanguageId(request, repository, "language");
169     }
170
171     public static long getLanguageId(HttpRequest request, Repository repository, String JavaDoc paramName) throws BadRequestException, RepositoryException {
172         String JavaDoc languageParam = request.getParameter(paramName);
173         if (languageParam == null || languageParam.length() == 0) {
174             return Language.DEFAULT_LANGUAGE_ID;
175         } else if (Character.isDigit(languageParam.charAt(0))) {
176             try {
177                 return Long.parseLong(languageParam);
178             } catch (NumberFormatException JavaDoc e) {
179                 throw new BadRequestException("Invalid value in \"" + paramName + "\" request parameter: " + languageParam);
180             }
181         } else {
182             try {
183                 return repository.getVariantManager().getLanguage(languageParam, false).getId();
184             } catch (LanguageNotFoundException e) {
185                 throw new BadRequestException("Non-existing language in \"" + paramName + "\" request parameter: " + languageParam);
186             }
187         }
188     }
189
190     public static long parseId(String JavaDoc name, String JavaDoc value) throws BadRequestException {
191         try {
192             return Long.parseLong(value);
193         } catch (NumberFormatException JavaDoc e) {
194             throw new BadRequestException("Invalid " + name + " ID: " + value);
195         }
196     }
197 }
198
Popular Tags