001/* 002 * Copyright 2002-2012 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 * https://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 017package org.springframework.mock.web.portlet; 018 019import java.io.BufferedReader; 020import java.io.ByteArrayInputStream; 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.InputStreamReader; 024import java.io.Reader; 025import java.io.UnsupportedEncodingException; 026import javax.portlet.ClientDataRequest; 027import javax.portlet.PortalContext; 028import javax.portlet.PortletContext; 029 030/** 031 * Mock implementation of the {@link javax.portlet.ClientDataRequest} interface. 032 * 033 * @author Juergen Hoeller 034 * @since 3.0 035 */ 036public class MockClientDataRequest extends MockPortletRequest implements ClientDataRequest { 037 038 private String characterEncoding; 039 040 private byte[] content; 041 042 private String contentType; 043 044 private String method; 045 046 047 /** 048 * Create a new MockClientDataRequest with a default {@link MockPortalContext} 049 * and a default {@link MockPortletContext}. 050 * @see org.springframework.mock.web.portlet.MockPortalContext 051 * @see org.springframework.mock.web.portlet.MockPortletContext 052 */ 053 public MockClientDataRequest() { 054 super(); 055 } 056 057 /** 058 * Create a new MockClientDataRequest with a default {@link MockPortalContext}. 059 * @param portletContext the PortletContext that the request runs in 060 */ 061 public MockClientDataRequest(PortletContext portletContext) { 062 super(portletContext); 063 } 064 065 /** 066 * Create a new MockClientDataRequest. 067 * @param portalContext the PortalContext that the request runs in 068 * @param portletContext the PortletContext that the request runs in 069 */ 070 public MockClientDataRequest(PortalContext portalContext, PortletContext portletContext) { 071 super(portalContext, portletContext); 072 } 073 074 075 public void setContent(byte[] content) { 076 this.content = content; 077 } 078 079 @Override 080 public InputStream getPortletInputStream() throws IOException { 081 if (this.content != null) { 082 return new ByteArrayInputStream(this.content); 083 } 084 else { 085 return null; 086 } 087 } 088 089 @Override 090 public void setCharacterEncoding(String characterEncoding) { 091 this.characterEncoding = characterEncoding; 092 } 093 094 @Override 095 public BufferedReader getReader() throws UnsupportedEncodingException { 096 if (this.content != null) { 097 InputStream sourceStream = new ByteArrayInputStream(this.content); 098 Reader sourceReader = (this.characterEncoding != null) ? 099 new InputStreamReader(sourceStream, this.characterEncoding) : new InputStreamReader(sourceStream); 100 return new BufferedReader(sourceReader); 101 } 102 else { 103 return null; 104 } 105 } 106 107 @Override 108 public String getCharacterEncoding() { 109 return this.characterEncoding; 110 } 111 112 public void setContentType(String contentType) { 113 this.contentType = contentType; 114 } 115 116 @Override 117 public String getContentType() { 118 return this.contentType; 119 } 120 121 @Override 122 public int getContentLength() { 123 return (this.content != null ? content.length : -1); 124 } 125 126 public void setMethod(String method) { 127 this.method = method; 128 } 129 130 @Override 131 public String getMethod() { 132 return this.method; 133 } 134 135}