KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpBindingSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.http;
18
19 import org.apache.servicemix.components.util.ComponentSupport;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.jbi.JBIException;
25 import javax.jbi.messaging.NormalizedMessage;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28
29 /**
30  * @version $Revision: 426415 $
31  */

32 public abstract class HttpBindingSupport extends ComponentSupport implements HttpBinding {
33     protected static final int BAD_REQUEST_STATUS_CODE = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
34     private HttpMarshaler marshaler;
35
36     public HttpMarshaler getMarshaler() {
37         if (marshaler == null) {
38             marshaler = createMarshaler();
39         }
40         return marshaler;
41     }
42
43     public void setMarshaler(HttpMarshaler marshaler) {
44         this.marshaler = marshaler;
45     }
46
47     public abstract void process(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
48             JBIException;
49
50     /**
51      * A factory method used to create the default {@link HttpMarshaler} to be used to turn the request into a
52      * {@link NormalizedMessage} and to turn a normalized message into a response.
53      *
54      * @return the newly created marshaler
55      */

56     protected HttpMarshaler createMarshaler() {
57       return new HttpMarshaler();
58     }
59
60     protected void outputException(HttpServletResponse JavaDoc response, Exception JavaDoc e) throws IOException JavaDoc {
61         response.setStatus(BAD_REQUEST_STATUS_CODE);
62         PrintWriter JavaDoc writer = response.getWriter();
63         writer.println("Request failed with error: " + e);
64         e.printStackTrace(writer);
65     }
66 }
67
Popular Tags