001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.http.server;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020:
021: import javax.servlet.http.HttpServlet;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024:
025: /**
026: * Servlet component of the
027: * {@link com.google.gwt.http.client.RequestBuilderTest RequestBuilderTest}.
028: */
029: public class RequestBuilderTestServlet extends HttpServlet {
030:
031: private static String getPathInfoBase() {
032: return "/com.google.gwt.http.RequestBuilderTest/testRequestBuilder/";
033: }
034:
035: @Override
036: protected void doDelete(HttpServletRequest request,
037: HttpServletResponse response) {
038: response.setStatus(HttpServletResponse.SC_OK);
039: }
040:
041: @Override
042: protected void doGet(HttpServletRequest request,
043: HttpServletResponse response) throws IOException {
044: String method = request.getMethod();
045: String pathInfo = request.getPathInfo();
046: if (pathInfo.equals(getPathInfoBase() + "setRequestHeader")) {
047: String value = request.getHeader("Foo");
048: response.getWriter().print("Hello");
049: if (value.equals("Bar1")) {
050: response.setStatus(HttpServletResponse.SC_OK);
051: } else {
052: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
053: }
054: } else if (pathInfo.equals(getPathInfoBase()
055: + "sendRequest_GET")) {
056: response.setStatus(HttpServletResponse.SC_OK);
057: response.getWriter().write(
058: "<html><body>hello</body></html>");
059: response.setContentType("text/html");
060: } else if (pathInfo.equals(getPathInfoBase()
061: + "setTimeout/timeout")) {
062: // cause a timeout on the client
063: try {
064: Thread.sleep(5000);
065: } catch (InterruptedException e) {
066: e.printStackTrace();
067: }
068: response.setStatus(HttpServletResponse.SC_OK);
069: } else if (pathInfo.equals(getPathInfoBase()
070: + "setTimeout/noTimeout")) {
071: // wait but not long enough to timeout
072: try {
073: Thread.sleep(1000);
074: } catch (InterruptedException e) {
075: // TODO Auto-generated catch block
076: e.printStackTrace();
077: }
078: response.getWriter().print("setTimeout/noTimeout");
079: response.setStatus(HttpServletResponse.SC_OK);
080: } else {
081: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
082: }
083: }
084:
085: @Override
086: protected void doHead(HttpServletRequest request,
087: HttpServletResponse response) {
088: response.setStatus(HttpServletResponse.SC_OK);
089: }
090:
091: @Override
092: protected void doPost(HttpServletRequest request,
093: HttpServletResponse response) {
094: String parameter = request.getParameter("method");
095: if ("test request".equals(parameter)) {
096: response.setStatus(HttpServletResponse.SC_OK);
097: } else {
098: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
099: }
100: }
101:
102: @Override
103: protected void doPut(HttpServletRequest request,
104: HttpServletResponse response) throws IOException {
105: BufferedReader reader = request.getReader();
106: String content = reader.readLine();
107: if (content.equals("<html><body>Put Me</body></html>")) {
108: response.setStatus(HttpServletResponse.SC_OK);
109: } else {
110: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
111: }
112: }
113: }
|