01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
22: import javax.servlet.http.HttpServletResponse;
23: import javax.servlet.ServletException;
24: import javax.jbi.JBIException;
25: import javax.jbi.messaging.NormalizedMessage;
26: import java.io.IOException;
27: import java.io.OutputStream;
28: import java.io.PrintWriter;
29:
30: /**
31: * @version $Revision: 449651 $
32: */
33: public abstract class HttpBindingSupport extends ComponentSupport
34: implements HttpBinding {
35: protected static final int BAD_REQUEST_STATUS_CODE = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
36: private HttpMarshaler marshaler;
37:
38: public HttpMarshaler getMarshaler() {
39: if (marshaler == null) {
40: marshaler = createMarshaler();
41: }
42: return marshaler;
43: }
44:
45: public void setMarshaler(HttpMarshaler marshaler) {
46: this .marshaler = marshaler;
47: }
48:
49: public abstract void process(HttpServletRequest request,
50: HttpServletResponse response) throws ServletException,
51: IOException, JBIException;
52:
53: /**
54: * A factory method used to create the default {@link HttpMarshaler} to be used to turn the request into a
55: * {@link NormalizedMessage} and to turn a normalized message into a response.
56: *
57: * @return the newly created marshaler
58: */
59: protected HttpMarshaler createMarshaler() {
60: return new HttpMarshaler();
61: }
62:
63: protected void outputException(HttpServletResponse response,
64: Exception e) throws IOException {
65: response.setStatus(BAD_REQUEST_STATUS_CODE);
66: PrintWriter writer = null;
67: try {
68: writer = response.getWriter();
69: } catch (IllegalStateException ise) {
70: OutputStream os = response.getOutputStream();
71: writer = new PrintWriter(os);
72: }
73: writer.println("Request failed with error: " + e);
74: e.printStackTrace(writer);
75: }
76: }
|