001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.safehaus.asyncweb.example.helloworld;
021:
022: import java.io.PrintWriter;
023: import java.io.StringWriter;
024: import java.nio.charset.Charset;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.mina.common.ByteBuffer;
031: import org.safehaus.asyncweb.common.Cookie;
032: import org.safehaus.asyncweb.common.DefaultHttpResponse;
033: import org.safehaus.asyncweb.common.HttpRequest;
034: import org.safehaus.asyncweb.common.HttpResponseStatus;
035: import org.safehaus.asyncweb.common.MutableHttpResponse;
036: import org.safehaus.asyncweb.common.content.ByteBufferContent;
037: import org.safehaus.asyncweb.service.HttpService;
038: import org.safehaus.asyncweb.service.HttpServiceContext;
039:
040: /**
041: * A simple <code>HttpService</code> which sends "hello world"
042: * responses to every request.
043: *
044: * Note that normally we wouldn't be generating html directly in a service :o)
045: *
046: * @author irvingd
047: *
048: */
049: public class HelloWorldHttpService implements HttpService {
050:
051: private String message = "Hello from AsyncWeb!!";
052:
053: /**
054: * Sends the configured message as an HTTP response
055: */
056: public void handleRequest(HttpServiceContext context)
057: throws Exception {
058: MutableHttpResponse response = new DefaultHttpResponse();
059:
060: StringWriter buf = new StringWriter();
061: PrintWriter writer = new PrintWriter(buf);
062: writer
063: .println("<html><body><b>Your message of the day:</b><br/><br/>");
064: writer.println("<h2><i>" + message + "</h2></i><br/><br/>");
065: writeHeaders(context.getRequest(), writer);
066: writer.println("<br/>");
067: writeParameters(context.getRequest(), writer);
068: writer.println("<br/>");
069: writeCookies(context.getRequest(), writer);
070: writer.flush();
071:
072: ByteBuffer bb = ByteBuffer.allocate(1024);
073: bb.setAutoExpand(true);
074: bb.putString(buf.toString(), Charset.forName("UTF-8")
075: .newEncoder());
076: bb.flip();
077: response.setContent(new ByteBufferContent(bb));
078:
079: response.setHeader("Pragma", "no-cache");
080: response.setHeader("Cache-Control", "no-cache");
081: response.setStatus(HttpResponseStatus.OK);
082:
083: context.commitResponse(response);
084: }
085:
086: /**
087: * Sets the message to return in responses.
088: * This is called for you by the framework!
089: *
090: * @param message The message
091: */
092: public void setMessage(String message) {
093: this .message = message;
094: }
095:
096: /**
097: * Writes headers from the request to the specified writer
098: *
099: * @param request The request
100: * @param writer The writer
101: */
102: private void writeHeaders(HttpRequest request, PrintWriter writer) {
103: writer
104: .println("You sent these headers with your request:<br/>");
105: writer.println("<ul>");
106: for (String headerName : request.getHeaders().keySet()) {
107: String headerValue = request.getHeader(headerName);
108: writer.print("<li>" + headerName + " = " + headerValue
109: + "</li>");
110: }
111: writer.println("</ul>");
112: }
113:
114: /**
115: * Writes cookies from the request to the specified writer
116: *
117: * @param request The request
118: * @param writer The writer
119: */
120: private void writeCookies(HttpRequest request, PrintWriter writer) {
121: Collection<Cookie> cookies = request.getCookies();
122: if (!cookies.isEmpty()) {
123: writer
124: .println("You sent these cookies with your request:<br/>");
125: writer.println("<ul>");
126: for (Cookie cookie : cookies) {
127: writer.println("<li>Name = " + cookie.getName()
128: + " Value = " + cookie.getValue());
129: writer
130: .println(" Path = " + cookie.getPath()
131: + " Version = " + cookie.getVersion()
132: + "</li>");
133: }
134: writer.println("</ul>");
135: }
136: }
137:
138: /**
139: * Writes request parameters to the specified writer
140: *
141: * @param request The request
142: * @param writer The writer
143: */
144: private void writeParameters(HttpRequest request, PrintWriter writer) {
145: if (request.getParameters().size() > 0) {
146: writer
147: .println("You sent these parameters with your request:<br/><br/>");
148: writer.println("<ul>");
149:
150: for (Map.Entry<String, List<String>> entry : request
151: .getParameters().entrySet()) {
152: writer.println("<li>");
153: writer.print("'" + entry.getKey() + "' = ");
154: for (Iterator<String> i = entry.getValue().iterator(); i
155: .hasNext();) {
156: String value = i.next();
157: writer.print("'" + value + "'");
158: if (i.hasNext()) {
159: writer.print(", ");
160: }
161: }
162: writer.println("</li/>");
163: }
164:
165: writer.println("</ul>");
166: }
167: }
168:
169: public void start() {
170: // Dont care
171: }
172:
173: public void stop() {
174: // Dont care
175: }
176:
177: }
|