001: package com.xoetrope.service.servlet;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.net.URLEncoder;
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: /**
011: * Extends XServiceServlet by adding a reliability parameter that can be used to
012: * control how the response is returned. This parameter is defined by a number
013: * of constants and set with the setRiability method. The service is intended to
014: * simulate the breakdown in communication services so that yo can test the
015: * resiliance of your application. Normally the class would not appear in
016: * anything other than a stress test environment.
017: *
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * <p> $Revision: 1.5 $</p>
023: */
024: public class XUnreliableServiceServlet extends XServiceServlet {
025: /**
026: * A proper response is returned to the client as soon as is possible.
027: */
028: public final static int RELIABLE = 0x1;
029:
030: /**
031: * An incomplete response is returned to the client. The response will be
032: * truncated by approximately 50 percent.
033: */
034: public final static int PARTIAL_RESPONSE = 0x2;
035:
036: /**
037: * Additional bytes are appended to an otherwise correct response
038: */
039: public final static int EXTRA_RESPONSE = 0x4;
040:
041: /**
042: * No response is made and the request returns immediately
043: */
044: public final static int NO_RESPONSE = 0x8;
045:
046: /**
047: * No response is made and nothing is returned to the client
048: */
049: public final static int TIME_OUT = 0x10;
050:
051: /**
052: * The returned values are corrupt.
053: */
054: public final static int CORRUPT_RESPONSE = 0x20;
055:
056: /**
057: * An http error is returned as part of the response
058: */
059: public final static int ERROR_RESPONSE = 0x40;
060:
061: /**
062: * The request is only processed on every fourth attempt
063: */
064: public final static int FOURTH_RESPONSE = 0x80;
065:
066: /**
067: * Construct a new servlet with the default options
068: */
069: public XUnreliableServiceServlet() {
070: }
071:
072: /**
073: * Initialize the servlet
074: */
075: public void init() throws ServletException {
076: super .init();
077: getServletContext().setAttribute("reliability",
078: new Integer(RELIABLE).toString());
079: getServletContext().setAttribute("requestCounter",
080: new Integer(0).toString());
081: }
082:
083: /**
084: * Process the HTTP Get request
085: * @param request the incoming request
086: * @param response the response object
087: */
088: public void doGet(HttpServletRequest request,
089: HttpServletResponse response) throws ServletException,
090: IOException {
091: int requestCounter = new Integer((String) getServletContext()
092: .getAttribute("requestCounter")).intValue();
093: getServletContext().setAttribute("requestCounter",
094: new Integer(requestCounter + 1).toString());
095: try {
096: Object reliability = request.getParameter("reliability");
097: if (reliability != null)
098: setReliability(reliability.toString());
099: else
100: reliability = getReliability();
101:
102: Object result = URLEncoder.encode((String) getResponse(
103: request, response), "UTF-8");
104: if (result != null) {
105:
106: PrintWriter out = response.getWriter();
107: response.setContentType(CONTENT_TYPE);
108: String resStr = result.toString();
109: int len = resStr.length();
110: int reliabilityValue = Integer.parseInt(reliability
111: .toString());
112: if ((reliabilityValue & RELIABLE) == RELIABLE)
113: out.print(resStr);
114: else if ((reliabilityValue & PARTIAL_RESPONSE) == PARTIAL_RESPONSE)
115: out.print(resStr.substring(0, len / 2));
116: else if ((reliabilityValue & EXTRA_RESPONSE) == EXTRA_RESPONSE) {
117: out.print(resStr);
118: out.print("EXTRA INSERTED DATA HERE BY REQUEST");
119: } else if ((reliabilityValue & NO_RESPONSE) == NO_RESPONSE)
120: response.reset();
121: else if ((reliabilityValue & CORRUPT_RESPONSE) == CORRUPT_RESPONSE) {
122: out.print(resStr.substring(len / 2));
123: out.print(resStr.substring(0, len / 2));
124: } else if ((reliabilityValue & FOURTH_RESPONSE) == FOURTH_RESPONSE) {
125: if ((requestCounter % 4) == 0)
126: out.print(resStr);
127: else
128: response
129: .sendError(response.SC_INTERNAL_SERVER_ERROR);
130: }
131:
132: if ((reliabilityValue & ERROR_RESPONSE) == ERROR_RESPONSE)
133: response
134: .sendError(response.SC_INTERNAL_SERVER_ERROR);
135:
136: if ((reliabilityValue & TIME_OUT) == TIME_OUT)
137: Thread.currentThread().sleep(100000000l);
138:
139: out.flush();
140: } else
141: response.sendError(response.SC_BAD_REQUEST);
142: } catch (Exception ex) {
143: ex.printStackTrace();
144: }
145: }
146:
147: /**
148: * Sets a reliablity flag for the servlet to indicate how the servlet is to
149: * handle the response
150: * @param value the new reliability value
151: */
152: private void setReliability(String value) {
153: getServletContext().setAttribute("reliability", value);
154: }
155:
156: /**
157: * Gets the reliablity flag for the servlet to indicating how the servlet is to
158: * handle the response
159: * @return the new reliability value
160: */
161: private Object getReliability() {
162: return getServletContext().getAttribute("reliability");
163: }
164: }
|