001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of 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,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mock.web.portlet;
018:
019: import java.io.BufferedReader;
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.io.Reader;
025: import java.io.UnsupportedEncodingException;
026:
027: import javax.portlet.ActionRequest;
028: import javax.portlet.PortalContext;
029: import javax.portlet.PortletContext;
030:
031: /**
032: * Mock implementation of the {@link javax.portlet.ActionRequest} interface.
033: *
034: * @author John A. Lewis
035: * @author Juergen Hoeller
036: * @since 2.0
037: */
038: public class MockActionRequest extends MockPortletRequest implements
039: ActionRequest {
040:
041: private String characterEncoding;
042:
043: private byte[] content;
044:
045: private String contentType;
046:
047: /**
048: * Create a new MockActionRequest with a default {@link MockPortalContext}
049: * and a default {@link MockPortletContext}.
050: * @see MockPortalContext
051: * @see MockPortletContext
052: */
053: public MockActionRequest() {
054: super ();
055: }
056:
057: /**
058: * Create a new MockActionRequest with a default {@link MockPortalContext}.
059: * @param portletContext the PortletContext that the request runs in
060: * @see MockPortalContext
061: */
062: public MockActionRequest(PortletContext portletContext) {
063: super (portletContext);
064: }
065:
066: /**
067: * Create a new MockActionRequest.
068: * @param portalContext the PortalContext that the request runs in
069: * @param portletContext the PortletContext that the request runs in
070: */
071: public MockActionRequest(PortalContext portalContext,
072: PortletContext portletContext) {
073: super (portalContext, portletContext);
074: }
075:
076: public void setContent(byte[] content) {
077: this .content = content;
078: }
079:
080: public InputStream getPortletInputStream() throws IOException {
081: if (this .content != null) {
082: return new ByteArrayInputStream(this .content);
083: } else {
084: return null;
085: }
086: }
087:
088: public void setCharacterEncoding(String characterEncoding) {
089: this .characterEncoding = characterEncoding;
090: }
091:
092: public BufferedReader getReader()
093: throws UnsupportedEncodingException {
094: if (this .content != null) {
095: InputStream sourceStream = new ByteArrayInputStream(
096: this .content);
097: Reader sourceReader = (this .characterEncoding != null) ? new InputStreamReader(
098: sourceStream, this .characterEncoding)
099: : new InputStreamReader(sourceStream);
100: return new BufferedReader(sourceReader);
101: } else {
102: return null;
103: }
104: }
105:
106: public String getCharacterEncoding() {
107: return characterEncoding;
108: }
109:
110: public void setContentType(String contentType) {
111: this .contentType = contentType;
112: }
113:
114: public String getContentType() {
115: return contentType;
116: }
117:
118: public int getContentLength() {
119: return (this .content != null ? content.length : -1);
120: }
121:
122: }
|