View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  
14  package org.abstracthorizon.danube.tapestry;
15  
16  import java.io.IOException;
17  import java.io.OutputStream;
18  import java.io.PrintWriter;
19  
20  import org.abstracthorizon.danube.http.HTTPConnection;
21  import org.abstracthorizon.danube.http.Status;
22  
23  import org.apache.tapestry.util.ContentType;
24  import org.apache.tapestry.web.WebResponse;
25  
26  /**
27   * {@link WebResponse} interface implementation.
28   *
29   * @author Daniel Sendula
30   */
31  public class DanubeResponse implements WebResponse {
32  
33      /** Reference to connection */
34      protected HTTPConnection connection;
35  
36      /** If true {@link #getPrintWriter(ContentType)} will reset connection. */
37      protected boolean needReset = false;
38  
39      /**
40       * Constructor
41       * @param connection connection
42       */
43      public DanubeResponse(HTTPConnection connection) {
44          this.connection = connection;
45      }
46  
47      /**
48       * Returns output stream and sets content type
49       * @param contentType
50       * @return output stream
51       */
52      public OutputStream getOutputStream(ContentType contentType) throws IOException {
53          connection.getResponseHeaders().putOnly("Content-Type", contentType.getMimeType());
54          return (OutputStream)connection.adapt(OutputStream.class);
55      }
56  
57      /**
58       * Returns print writer. It will reset connection ({@link HTTPConnection#reset()}) if this method is called
59       * second time.
60       */
61      public PrintWriter getPrintWriter(ContentType contentType) throws IOException {
62          if (needReset) {
63              connection.reset();
64          }
65          needReset = true;
66          connection.getRequestHeaders().putOnly("Content-Type", contentType.getMimeType());
67          return (PrintWriter)connection.adapt(PrintWriter.class);
68      }
69  
70      /**
71       * Encodes url.
72       * @param url to be encoded
73       * @return encoded url
74       */
75      public String encodeURL(String url) {
76          return url; // TODO
77      }
78  
79      /**
80       * Resets connection. It calls {@link HTTPConnection#reset()}.
81       */
82      public void reset() {
83          connection.reset();
84      }
85  
86      /**
87       * Sets content length. It sets appropriate header.
88       * @param len content length
89       */
90      public void setContentLength(int len) {
91          connection.getResponseHeaders().putOnly("Content-Length", Integer.toString(len));
92      }
93  
94      /**
95       * Returns namespace
96       * @return namespace
97       */
98      public String getNamespace() {
99          return ""; // TODO
100     }
101 
102     /**
103      * Sets date header
104      * @param name header name
105      * @param date date
106      */
107     public void setDateHeader(String name, long date) {
108         // TODO
109     }
110 
111     /**
112      * Sets header
113      * @param name header name
114      * @param value header value
115      */
116     public void setHeader(String name, String value) {
117         connection.getRequestHeaders().putOnly(name, value);
118     }
119 
120     /**
121      * Sets integer header
122      * @param name header name
123      * @param i integer value
124      */
125     public void setIntHeader(String name, int i) {
126         connection.getRequestHeaders().putOnly(name, Integer.toString(i));
127     }
128 
129     /**
130      * Sets HTTP status
131      * @param code status code
132      */
133     public void setStatus(int code) {
134         // TODO check static fields of Status class for the given code
135         Status status = new Status(Integer.toString(code), Integer.toString(code));
136         connection.setResponseStatus(status);
137     }
138 
139     /**
140      * Sends error to the client
141      * @param code error code
142      * @param msg error message
143      * @throws IOException
144      */
145     public void sendError(int code, String msg) throws IOException {
146         Status status = new Status(Integer.toString(code), msg);
147         connection.setResponseStatus(status);
148         // TODO do something!
149     }
150 
151 }