001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.requests;
019
020import io.stallion.plugins.javascript.Sandbox;
021import io.stallion.users.IOrg;
022import io.stallion.users.IUser;
023
024import javax.servlet.http.Cookie;
025
026import java.io.BufferedReader;
027import java.io.IOException;
028import java.util.*;
029
030import static io.stallion.utils.Literals.*;
031
032
033public class SandboxedRequest implements IRequest {
034    private IRequest request;
035    private Sandbox sandbox;
036
037    public SandboxedRequest(Sandbox sandbox, IRequest request) {
038        this.sandbox = sandbox;
039        this.request = request;
040    }
041
042    @Override
043    public String requestUrl() {
044        return request.requestUrl();
045    }
046
047
048
049    @Override
050    public String getScheme() {
051        return request.getScheme();
052    }
053
054
055    @Override
056    public String getQueryString() {
057        return request.getQueryString();
058    }
059
060
061    @Override
062    public String getRemoteAddr() {
063        return request.getRemoteAddr();
064    }
065
066    @Override
067    public String getActualIp() {
068        return request.getActualIp();
069    }
070
071    @Override
072    public Object getBodyObject(Class clazz) {
073        return request.getBodyObject(clazz);
074    }
075
076    @Override
077    public Map<String, Object> getBodyMap() {
078        return request.getBodyMap();
079    }
080
081    @Override
082    public Object getBodyParam(String name) {
083        return request.getBodyParam(name);
084    }
085
086    @Override
087    public Map<String, String> getQueryParams() {
088        return request.getQueryParams();
089    }
090
091    @Override
092    public Cookie[] getCookies() {
093        List<Cookie> cookies = Arrays.asList(request.getCookies());
094        cookies = filter(cookies, cookie -> {
095            if (sandbox.getWhitelist().getCookies().contains(cookie.getName())) {
096                return true;
097            }
098            return false;
099        });
100        Cookie[] cookieArray = cookies.toArray(new Cookie[cookies.size()]);
101        return cookieArray;
102    }
103
104    @Override
105    public Cookie getCookie(String cookieName) {
106        if (sandbox.getWhitelist().getCookies().contains(cookieName)) {
107            return request.getCookie(cookieName);
108        }
109        return null;
110    }
111
112    @Override
113    public String getPath() {
114        return request.getPath();
115    }
116
117    @Override
118    public void setPath(String path) {
119
120    }
121
122    @Override
123    public String getHeader(String name) {
124        if (sandbox.getWhitelist().getHeaders().contains(name)) {
125            return request.getHeader(name);
126        }
127        return "";
128    }
129
130    @Override
131    public String getHost() {
132        return request.getHost();
133    }
134
135    @Override
136    public BufferedReader getReader() throws IOException {
137        return null;
138    }
139
140    @Override
141    public String getContent() {
142        return request.getContent();
143    }
144
145    @Override
146    public Enumeration<String> getHeaderNames() {
147        return request.getHeaderNames();
148    }
149
150    @Override
151    public IUser getUser() {
152        return null;
153    }
154
155    @Override
156    public void setUser(IUser user) {
157
158    }
159
160    @Override
161    public IOrg getOrg() {
162        return null;
163    }
164
165    @Override
166    public void setOrg(IOrg org) {
167
168    }
169
170    @Override
171    public String getMethod() {
172        return request.getMethod();
173    }
174
175    @Override
176    public String getParameter(String paramName) {
177        return request.getParameter(paramName);
178    }
179
180    @Override
181    public Boolean getIsJsonRequest() {
182        return request.getIsJsonRequest();
183    }
184
185    @Override
186    public void setIsJsonRequest(Boolean isJsonRequest) {
187
188    }
189
190    @Override
191    public void setQuery(String query) {
192
193    }
194
195    @Override
196    public Map<String, Object> getItems() {
197        return request.getItems();
198    }
199
200    @Override
201    public void setItems(Map<String, Object> items) {
202
203    }
204
205    public SandboxedRequest getSandboxedRequest(Sandbox box) {
206        return this;
207    }
208
209    @Override
210    public Set<String> getScopes() {
211        return request.getScopes();
212    }
213
214    @Override
215    public StRequest setScopes(Set<String> scopes) {
216        return null;
217    }
218
219    @Override
220    public boolean isScoped() {
221        return request.isScoped();
222    }
223
224    @Override
225    public StRequest setScoped(boolean scoped) {
226        return null;
227    }
228}