All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.threewks.thundr.request.servlet.ServletRequest Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://3wks.github.io/thundr/
 * Copyright (C) 2015 3wks, 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.threewks.thundr.request.servlet;

import static com.atomicleopard.expressive.Expressive.iterable;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.atomicleopard.expressive.ETransformer;
import com.atomicleopard.expressive.Expressive;
import com.threewks.thundr.exception.BaseException;
import com.threewks.thundr.http.Cookie;
import com.threewks.thundr.request.BaseRequest;
import com.threewks.thundr.request.Request;
import com.threewks.thundr.route.HttpMethod;
import com.threewks.thundr.route.Route;

public class ServletRequest extends BaseRequest implements Request {
	protected static final ETransformer, Map>> CookieLookup = Expressive.Transformers.toBeanLookup("name", Cookie.class);

	protected HttpServletRequest req;

	public ServletRequest(HttpServletRequest servletRequest, HttpMethod httpMethod, Route route) {
		super(httpMethod, route);
		this.req = servletRequest;
	}

	@Override
	protected Object getRawRequest() {
		return req;
	}

	@Override
	public URI getRequestUri() {
		String reqUrl = req.getRequestURL().toString();
		String queryString = req.getQueryString();
		if (queryString != null) {
			reqUrl += "?" + queryString;
		}
		return URI.create(reqUrl);
	}

	@Override
	public String getContentTypeString() {
		return req.getContentType();
	}

	@Override
	public String getCharacterEncoding() {
		return req.getCharacterEncoding();
	}

	@Override
	public String getHeader(String name) {
		return req.getHeader(name);
	}

	@Override
	public List getHeaders(String name) {
		return ServletSupport.getHeaders(name, req);
	}

	@Override
	public Map> getAllHeaders() {
		return ServletSupport.getHeaderMap(req);
	}

	@Override
	public String getParameter(String name) {
		return req.getParameter(name);
	}

	@Override
	public List getParameters(String name) {
		String[] values = req.getParameterValues(name);
		return values == null ? null : Arrays.asList(values);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map> getAllParameters() {
		Map parameters = req.getParameterMap();
		Map> result = new LinkedHashMap<>();
		for (Map.Entry entry : parameters.entrySet()) {
			result.put(entry.getKey(), Arrays.asList(entry.getValue()));
		}
		return result;
	}

	@Override
	public long getContentLength() {
		return req.getContentLength();
	}

	@Override
	public String getRequestPath() {
		return req.getRequestURI();
	}

	@Override
	public Cookie getCookie(String name) {
		javax.servlet.http.Cookie[] cookies = req.getCookies();
		if (cookies != null) {
			for (javax.servlet.http.Cookie cookie : cookies) {
				if (cookie.getName().equals(name)) {
					return ServletSupport.ToThundrCookie.from(cookie);
				}
			}
		}
		return null;
	}

	@Override
	public Map> getAllCookies() {
		javax.servlet.http.Cookie[] servletCookies = req.getCookies();
		List cookies = ServletSupport.ToThundrCookies.from(servletCookies);
		return CookieLookup.from(cookies);
	}

	@Override
	public Reader getReader() {
		try {
			return req.getReader();
		} catch (IOException e) {
			throw new BaseException(e);
		}
	}

	@Override
	public InputStream getInputStream() {
		try {
			return req.getInputStream();
		} catch (IOException e) {
			throw new BaseException(e);
		}
	}

	@Override
	public void putData(String key, Object value) {
		req.setAttribute(key, value);
	}

	@Override
	public void putData(Map values) {
		for (Map.Entry pair : values.entrySet()) {
			req.setAttribute(pair.getKey(), pair.getValue());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map getAllData() {
		Map results = new LinkedHashMap<>();
		Iterable iterable = iterable(req.getAttributeNames());
		for (String name : iterable) {
			results.put(name, req.getAttribute(name));
		}
		return results;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  T getData(String key) {
		return (T) req.getAttribute(key);
	}

	@Override
	public boolean isSecure() {
		return req.isSecure();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy