001/* 002 * Copyright 2002-2016 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.InputStream; 020import java.net.MalformedURLException; 021import java.net.URL; 022import java.util.Collections; 023import java.util.Enumeration; 024import java.util.Set; 025import javax.portlet.PortletContext; 026import javax.portlet.PortletRequestDispatcher; 027import javax.servlet.ServletContext; 028 029import org.springframework.util.Assert; 030 031/** 032 * Mock implementation of the {@link javax.portlet.PortletContext} interface, 033 * wrapping an underlying {@link javax.servlet.ServletContext}. 034 * 035 * @author Juergen Hoeller 036 * @since 3.0 037 * @see MockPortletContext 038 */ 039public class ServletWrappingPortletContext implements PortletContext { 040 041 private final ServletContext servletContext; 042 043 044 /** 045 * Create a new PortletContext wrapping the given ServletContext. 046 * @param servletContext the ServletContext to wrap 047 */ 048 public ServletWrappingPortletContext(ServletContext servletContext) { 049 Assert.notNull(servletContext, "ServletContext must not be null"); 050 this.servletContext = servletContext; 051 } 052 053 /** 054 * Return the underlying ServletContext that this PortletContext wraps. 055 */ 056 public final ServletContext getServletContext() { 057 return this.servletContext; 058 } 059 060 061 @Override 062 public String getServerInfo() { 063 return this.servletContext.getServerInfo(); 064 } 065 066 @Override 067 public PortletRequestDispatcher getRequestDispatcher(String path) { 068 return null; 069 } 070 071 @Override 072 public PortletRequestDispatcher getNamedDispatcher(String name) { 073 return null; 074 } 075 076 @Override 077 public InputStream getResourceAsStream(String path) { 078 return this.servletContext.getResourceAsStream(path); 079 } 080 081 @Override 082 public int getMajorVersion() { 083 return 2; 084 } 085 086 @Override 087 public int getMinorVersion() { 088 return 0; 089 } 090 091 @Override 092 public String getMimeType(String file) { 093 return this.servletContext.getMimeType(file); 094 } 095 096 @Override 097 public String getRealPath(String path) { 098 return this.servletContext.getRealPath(path); 099 } 100 101 @Override 102 public Set<String> getResourcePaths(String path) { 103 return this.servletContext.getResourcePaths(path); 104 } 105 106 @Override 107 public URL getResource(String path) throws MalformedURLException { 108 return this.servletContext.getResource(path); 109 } 110 111 @Override 112 public Object getAttribute(String name) { 113 return this.servletContext.getAttribute(name); 114 } 115 116 @Override 117 public Enumeration<String> getAttributeNames() { 118 return this.servletContext.getAttributeNames(); 119 } 120 121 @Override 122 public String getInitParameter(String name) { 123 return this.servletContext.getInitParameter(name); 124 } 125 126 @Override 127 public Enumeration<String> getInitParameterNames() { 128 return this.servletContext.getInitParameterNames(); 129 } 130 131 @Override 132 public void log(String msg) { 133 this.servletContext.log(msg); 134 } 135 136 @Override 137 public void log(String message, Throwable throwable) { 138 this.servletContext.log(message, throwable); 139 } 140 141 @Override 142 public void removeAttribute(String name) { 143 this.servletContext.removeAttribute(name); 144 } 145 146 @Override 147 public void setAttribute(String name, Object object) { 148 this.servletContext.setAttribute(name, object); 149 } 150 151 @Override 152 public String getPortletContextName() { 153 return this.servletContext.getServletContextName(); 154 } 155 156 @Override 157 public Enumeration<String> getContainerRuntimeOptions() { 158 return Collections.enumeration(Collections.<String>emptySet()); 159 } 160 161}