提交 196e5bc9 编写于 作者: 智布道's avatar 智布道 👁

📝 change: Delete useless files

上级 63831663
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.fujieid</groupId>
<artifactId>jap</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jap-http-adapter</artifactId>
<name>jap-http-adapter</name>
<description>jap-http-adapter</description>
<dependencies>
<dependency>
<groupId>com.fujieid</groupId>
<artifactId>jap-http</artifactId>
</dependency>
<!-- servlet -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http.adapter.jakarta;
import com.fujieid.jap.http.JapHttpCookie;
import javax.servlet.http.Cookie;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class JakartaCookieAdapter extends JapHttpCookie {
public JakartaCookieAdapter(Object cookie) {
super(cookie);
Cookie jakartaCookie = (Cookie) cookie;
super.setDomain(jakartaCookie.getDomain());
super.setPath(jakartaCookie.getDomain());
super.setName(jakartaCookie.getDomain());
super.setValue(jakartaCookie.getDomain());
super.setMaxAge(jakartaCookie.getMaxAge());
super.setSecure(jakartaCookie.getSecure());
super.setHttpOnly(jakartaCookie.isHttpOnly());
}
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http.adapter.jakarta;
import com.fujieid.jap.http.JapHttpCookie;
import com.fujieid.jap.http.JapHttpRequest;
import com.fujieid.jap.http.JapHttpSession;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class JakartaRequestAdapter implements JapHttpRequest {
private final HttpServletRequest request;
public JakartaRequestAdapter(HttpServletRequest request) {
this.request = request;
}
/**
* Get the actual source object
*
* @return Object
*/
@Override
public Object getSource() {
return request;
}
/**
* Returns the value of a request parameter as a <code>String</code>, or <code>null</code> if the parameter does not
* exist.
*
* @param name a <code>String</code> specifying the name of the parameter
* @return a <code>String</code> representing the single value of the parameter
*/
@Override
public String getParameter(String name) {
return this.request.getParameter(name);
}
/**
* Returns an array of <code>String</code> objects containing all of the values the given request parameter has, or
* <code>null</code> if the parameter does not exist.
*
* @param name a <code>String</code> containing the name of the parameter whose value is requested
* @return an array of <code>String</code> objects containing the parameter's values
*/
@Override
public String[] getParameterValues(String name) {
return this.request.getParameterValues(name);
}
/**
* an immutable java.util.Map containing parameter names as keys and parameter values as map values.
*
* @return Returns a java.util.Map of the parameters of this request.
*/
@Override
public Map<String, String[]> getParameterMap() {
return this.request.getParameterMap();
}
/**
* Returns the value of the specified request header as a <code>String</code>. If the request did not include a
* header of the specified name, this method returns <code>null</code>.
*
* @param name a <code>String</code> specifying the header name
* @return a <code>String</code> containing the value of the requested header, or <code>null</code> if the request
* does not have a header of that name
*/
@Override
public String getHeader(String name) {
return this.request.getHeader(name);
}
/**
* Returns the part of this request's URL from the protocol name up to the query string in the first line of the
* HTTP request.
*
* @return a <code>String</code> containing the part of the URL from the protocol name up to the query string
*/
@Override
public String getRequestUri() {
return this.request.getRequestURI();
}
/**
* Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port
* number, and server path, but it does not include query string parameters.
*
* @return a <code>StringBuffer</code> object containing the reconstructed URL
*/
@Override
public StringBuffer getRequestUrl() {
return this.request.getRequestURL();
}
/**
* Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
*
* @return a <code>String</code> specifying the name of the method with which this request was made
*/
@Override
public String getMethod() {
return this.request.getMethod();
}
/**
* Returns the query string that is contained in the request URL after the path. This method returns
* <code>null</code> if the URL does not have a query string.
*
* @return a <code>String</code> containing the query string or <code>null</code> if the URL contains no query
* string.
*/
@Override
public String getQueryString() {
return this.request.getQueryString();
}
/**
* Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.
*
* @return a <code>String</code> containing the IP address of the client that sent the request
*/
@Override
public String getRemoteAddr() {
return this.request.getRemoteAddr();
}
/**
* Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes
* either the servlet name or a path to the servlet, but does not include any extra path information or a query
* string.
*
* @return a <code>String</code> containing the name or path of the servlet being called, as specified in the
* request URL, decoded, or an empty string if the servlet used to process the request is matched using the
* "/*" pattern.
*/
@Override
public String getServletPath() {
return this.request.getServletPath();
}
/**
* Returns an array containing all of the <code>JapHttpCookie</code> objects the client sent with this request. This method
* returns <code>null</code> if no cookies were sent.
*
* @return an array of all the <code>JapHttpCookie</code> included with this request, or <code>null</code> if the request
* has no cookies
*/
@Override
public JapHttpCookie[] getCookies() {
Cookie[] cookies = this.request.getCookies();
if (null == cookies || cookies.length == 0) {
return null;
}
int cookieLen = cookies.length;
JapHttpCookie[] japHttpCookies = new JakartaCookieAdapter[cookieLen];
for (int i = 0; i < cookieLen; i++) {
japHttpCookies[i] = new JakartaCookieAdapter(cookies[i]);
}
return japHttpCookies;
}
/**
* Returns the current <code>HttpSession</code> associated with this request
*
* @return the <code>HttpSession</code> associated with this request
*/
@Override
public JapHttpSession getSession() {
return new JakartaSessionAdapter(this.request.getSession());
}
/**
* Retrieves the body of the request as character data using a <code>BufferedReader</code>. The reader translates
* the character data according to the character encoding used on the body.
*
* @return a <code>BufferedReader</code> containing the body of the request
* @throws IOException if an input or output exception occurred
*/
@Override
public BufferedReader getReader() throws IOException {
return this.request.getReader();
}
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http.adapter.jakarta;
import com.fujieid.jap.http.JapHttpResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class JakartaResponseAdapter implements JapHttpResponse {
private final HttpServletResponse response;
public JakartaResponseAdapter(HttpServletResponse response) {
this.response = response;
}
/**
* Get the actual source object
*
* @return Object
*/
@Override
public Object getSource() {
return this.response;
}
/**
* Delete cookie
*
* @param name cookie name
* @return current response
*/
@Override
public JapHttpResponse delCookie(String name) {
addCookie(null);
return this;
}
/**
* Add cookie
*
* @param name name of the cookie.
* @param value value of this Cookie.
* @param path the path on the server to which the browser returns this cookie.
* @param domain domain name of this Cookie.
* @param expiry maximum age in seconds of this Cookie.
* @param secure send cookies only over a secure protocol, such as HTTPS or SSL.
* @param isHttpOnly http only.
* @return current response
*/
@Override
public JapHttpResponse addCookie(String name, String value, String path, String domain, int expiry, boolean secure, boolean isHttpOnly) {
Cookie cookie = new Cookie(name, value);
cookie.setPath(path);
cookie.setDomain(domain);
cookie.setMaxAge(expiry);
cookie.setSecure(secure);
cookie.setHttpOnly(isHttpOnly);
this.response.addCookie(cookie);
return this;
}
/**
* Set response status code
*
* @param status Response status code
* @return current response
*/
@Override
public JapHttpResponse setStatus(int status) {
this.response.setStatus(status);
return this;
}
/**
* Add response header
*
* @param name name of the response header
* @param value value of the response header
* @return current response
*/
@Override
public JapHttpResponse addHeader(String name, String value) {
this.response.addHeader(name, value);
return this;
}
@Override
public JapHttpResponse setContentType(String contentType) {
this.response.setContentType(contentType);
return this;
}
@Override
public JapHttpResponse setContentLength(int len) {
this.response.setContentLength(len);
return this;
}
@Override
public PrintWriter getWriter() throws IOException {
return this.response.getWriter();
}
/**
* Returns the name of the character encoding (MIME charset) used for the body sent in this response
* <p>
* See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) for more information about character encoding and MIME.
*
* @return a <code>String</code> specifying the name of the character encoding, for example, <code>UTF-8</code>
*/
@Override
public String getCharacterEncoding() {
return this.response.getCharacterEncoding();
}
/**
* Returns a {@link OutputStream} suitable for writing binary data in the response.
*
* @return a {@link OutputStream} for writing binary data
* @throws IOException if an input or output exception occurred
*/
@Override
public OutputStream getOutputStream() throws IOException {
return this.response.getOutputStream();
}
/**
* Redirect to url
*
* @param url Redirect url
*/
@Override
public void redirect(String url) {
try {
this.response.sendRedirect(url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http.adapter.jakarta;
import com.fujieid.jap.http.JapHttpSession;
import javax.servlet.http.HttpSession;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class JakartaSessionAdapter implements JapHttpSession {
private final HttpSession session;
public JakartaSessionAdapter(HttpSession session) {
this.session = session;
}
/**
* Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
*
* @return a <code>long</code> specifying when this session was created, expressed in milliseconds since 1/1/1970
* GMT
*/
@Override
public long getCreationTime() {
return this.session.getCreationTime();
}
/**
* Returns a string containing the unique identifier assigned to this session. The identifier is assigned by the
* servlet container and is implementation dependent.
*
* @return a string specifying the identifier assigned to this session
*/
@Override
public String getId() {
return this.session.getId();
}
/**
* Returns the object bound with the specified name in this session, or <code>null</code> if no object is bound
* under the name.
*
* @param name a string specifying the name of the object
* @return the object with the specified name
*/
@Override
public Object getAttribute(String name) {
return this.session.getAttribute(name);
}
/**
* Binds an object to this session, using the name specified. If an object of the same name is already bound to the
* session, the object is replaced.
*
* @param name the name to which the object is bound; cannot be null
* @param value the object to be bound; cannot be null
*/
@Override
public void setAttribute(String name, Object value) {
this.session.setAttribute(name, value);
}
/**
* Removes the object bound with the specified name from this session. If the session does not have an object bound
* with the specified name, this method does nothing.
*
* @param name the name of the object to remove from this session
*/
@Override
public void removeAttribute(String name) {
this.session.removeAttribute(name);
}
/**
* Invalidates this session then unbinds any objects bound to it.
*/
@Override
public void invalidate() {
this.session.invalidate();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.fujieid</groupId>
<artifactId>jap</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jap-http</artifactId>
<name>jap-http</name>
<description>jap-http</description>
</project>
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.5
*/
public class JapHttpCookie {
private String name;
private String value;
private String domain;
private String path = "/";
private int maxAge = -1;
private boolean secure;
private boolean httpOnly;
public JapHttpCookie(Object cookie) {
}
public JapHttpCookie(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public JapHttpCookie setName(String name) {
this.name = name;
return this;
}
public String getValue() {
return value;
}
public JapHttpCookie setValue(String value) {
this.value = value;
return this;
}
public String getDomain() {
return domain;
}
public JapHttpCookie setDomain(String domain) {
this.domain = domain;
return this;
}
public String getPath() {
return path;
}
public JapHttpCookie setPath(String path) {
this.path = path;
return this;
}
public int getMaxAge() {
return maxAge;
}
public JapHttpCookie setMaxAge(int maxAge) {
this.maxAge = maxAge;
return this;
}
public boolean isSecure() {
return secure;
}
public JapHttpCookie setSecure(boolean secure) {
this.secure = secure;
return this;
}
public boolean isHttpOnly() {
return httpOnly;
}
public JapHttpCookie setHttpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
}
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
/**
* Define an interface to adapt to request objects of different frameworks,
* such as: <code>javax.servlet.http.HttpServletRequest</code> or <code>com.blade.mvc.http.HttpRequest</code>
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.5
*/
public interface JapHttpRequest {
/**
* Get the actual source object
*
* @return Object
*/
Object getSource();
/**
* Returns the value of a request parameter as a <code>String</code>, or <code>null</code> if the parameter does not
* exist.
*
* @param name a <code>String</code> specifying the name of the parameter
* @return a <code>String</code> representing the single value of the parameter
*/
String getParameter(String name);
/**
* Returns an array of <code>String</code> objects containing all of the values the given request parameter has, or
* <code>null</code> if the parameter does not exist.
*
* @param name a <code>String</code> containing the name of the parameter whose value is requested
* @return an array of <code>String</code> objects containing the parameter's values
*/
String[] getParameterValues(String name);
/**
* an immutable java.util.Map containing parameter names as keys and parameter values as map values.
*
* @return Returns a java.util.Map of the parameters of this request.
*/
Map<String, String[]> getParameterMap();
/**
* Returns the value of the specified request header as a <code>String</code>. If the request did not include a
* header of the specified name, this method returns <code>null</code>.
*
* @param name a <code>String</code> specifying the header name
* @return a <code>String</code> containing the value of the requested header, or <code>null</code> if the request
* does not have a header of that name
*/
String getHeader(String name);
/**
* Returns the part of this request's URL from the protocol name up to the query string in the first line of the
* HTTP request.
*
* @return a <code>String</code> containing the part of the URL from the protocol name up to the query string
*/
String getRequestUri();
/**
* Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port
* number, and server path, but it does not include query string parameters.
*
* @return a <code>StringBuffer</code> object containing the reconstructed URL
*/
StringBuffer getRequestUrl();
/**
* Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
*
* @return a <code>String</code> specifying the name of the method with which this request was made
*/
String getMethod();
/**
* Returns the query string that is contained in the request URL after the path. This method returns
* <code>null</code> if the URL does not have a query string.
*
* @return a <code>String</code> containing the query string or <code>null</code> if the URL contains no query
* string.
*/
String getQueryString();
/**
* Returns an array containing all of the <code>JapHttpCookie</code> objects the client sent with this request. This method
* returns <code>null</code> if no cookies were sent.
*
* @return an array of all the <code>JapHttpCookie</code> included with this request, or <code>null</code> if the request
* has no cookies
*/
JapHttpCookie[] getCookies();
/**
* Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.
*
* @return a <code>String</code> containing the IP address of the client that sent the request
*/
String getRemoteAddr();
/**
* Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes
* either the servlet name or a path to the servlet, but does not include any extra path information or a query
* string.
*
* @return a <code>String</code> containing the name or path of the servlet being called, as specified in the
* request URL, decoded, or an empty string if the servlet used to process the request is matched using the
* "/*" pattern.
*/
String getServletPath();
/**
* Returns the current <code>HttpSession</code> associated with this request
*
* @return the <code>HttpSession</code> associated with this request
*/
JapHttpSession getSession();
/**
* Retrieves the body of the request as character data using a <code>BufferedReader</code>. The reader translates
* the character data according to the character encoding used on the body.
*
* @return a <code>BufferedReader</code> containing the body of the request
* @throws IOException if an input or output exception occurred
*/
BufferedReader getReader() throws IOException;
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.5
*/
public interface JapHttpResponse {
/**
* Get the actual source object
*
* @return Object
*/
Object getSource();
/**
* Delete cookie
*
* @param name cookie name
* @return current response
*/
JapHttpResponse delCookie(String name);
/**
* Add cookie
*
* @param name name of the cookie.
* @param value value of this Cookie.
* @param path the path on the server to which the browser returns this cookie.
* @param domain domain name of this Cookie.
* @param expiry maximum age in seconds of this Cookie.
* @param secure send cookies only over a secure protocol, such as HTTPS or SSL.
* @param isHttpOnly http only.
* @return current response
*/
JapHttpResponse addCookie(String name, String value, String path, String domain, int expiry, boolean secure, boolean isHttpOnly);
/**
* Add cookie
*
* @param cookie JapHttpCookie
* @return current response
*/
default JapHttpResponse addCookie(JapHttpCookie cookie) {
if (null == cookie) {
return this.addCookie(null, null, null, null, 0, false, false);
}
return this.addCookie(cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(),
cookie.getMaxAge(), cookie.isSecure(), cookie.isHttpOnly()
);
}
/**
* Set response status code
*
* @param status Response status code
* @return current response
*/
JapHttpResponse setStatus(int status);
/**
* Add response header
*
* @param name name of the response header
* @param value value of the response header
* @return current response
*/
JapHttpResponse addHeader(String name, String value);
/**
* Sets the content type of the response being sent to the client, if the response has not been committed yet. The
* given content type may include a character encoding specification, for example,
* <code>text/html;charset=UTF-8</code>. The response's character encoding is only set from the given content type
* if this method is called before <code>getWriter</code> is called.
*
* @param contentType a <code>String</code> specifying the MIME type of the content
* @return current response
*/
JapHttpResponse setContentType(String contentType);
/**
* Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length
* header.
*
* @param len an integer specifying the length of the content being returned to the client; sets the Content-Length
* header
* @return current response
*/
JapHttpResponse setContentLength(int len);
/**
* Returns a <code>PrintWriter</code> object that can send character text to the client. The
* <code>PrintWriter</code> uses the character encoding returned by {@link #getCharacterEncoding}. If the response's
* character encoding has not been specified as described in <code>getCharacterEncoding</code> (i.e., the method
* just returns the default value <code>ISO-8859-1</code>), <code>getWriter</code> updates it to
* <code>ISO-8859-1</code>.
*
* @return a <code>PrintWriter</code> object that can return character data to the client
* @throws IOException if an input or output exception occurred
*/
PrintWriter getWriter() throws IOException;
/**
* Returns the name of the character encoding (MIME charset) used for the body sent in this response
* <p>
* See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) for more information about character encoding and MIME.
*
* @return a <code>String</code> specifying the name of the character encoding, for example, <code>UTF-8</code>
*/
String getCharacterEncoding();
/**
* Returns a {@link OutputStream} suitable for writing binary data in the response.
*
* @return a {@link OutputStream} for writing binary data
* @throws IOException if an input or output exception occurred
*/
OutputStream getOutputStream() throws IOException;
/**
* Redirect to url
*
* @param url Redirect url
*/
void redirect(String url);
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http;
/**
* Define an interface to adapt to session objects of different frameworks,
* such as: <code>javax.servlet.http.HttpSession</code> or <code>com.blade.mvc.http.Session</code>
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.5
*/
public interface JapHttpSession {
/**
* Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
*
* @return a <code>long</code> specifying when this session was created, expressed in milliseconds since 1/1/1970
* GMT
*/
long getCreationTime();
/**
* Returns a string containing the unique identifier assigned to this session. The identifier is assigned by the
* servlet container and is implementation dependent.
*
* @return a string specifying the identifier assigned to this session
*/
String getId();
/**
* Returns the object bound with the specified name in this session, or <code>null</code> if no object is bound
* under the name.
*
* @param name a string specifying the name of the object
* @return the object with the specified name
*/
Object getAttribute(String name);
/**
* Binds an object to this session, using the name specified. If an object of the same name is already bound to the
* session, the object is replaced.
*
* @param name the name to which the object is bound; cannot be null
* @param value the object to be bound; cannot be null
*/
void setAttribute(String name, Object value);
/**
* Removes the object bound with the specified name from this session. If the session does not have an object bound
* with the specified name, this method does nothing.
*
* @param name the name of the object to remove from this session
*/
void removeAttribute(String name);
/**
* Invalidates this session then unbinds any objects bound to it.
*/
void invalidate();
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.fujieid.jap.http;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* http servlet request util
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.1
*/
public class RequestUtil {
/**
* Get the url parameter value of the request through {@code request.getParameter(paramName)}
*
* @param paramName parameter name
* @param request current HTTP request
* @return string
*/
public static String getParam(String paramName, JapHttpRequest request) {
if (null == request) {
return null;
}
return request.getParameter(paramName);
}
/**
* Get request header
*
* @param headerName request header name
* @param request current HTTP request
* @return string
*/
public static String getHeader(String headerName, JapHttpRequest request) {
if (null == request) {
return "";
}
return request.getHeader(headerName);
}
/**
* Get the referer of the current HTTP request
*
* @param request current HTTP request
* @return string
*/
public static String getReferer(JapHttpRequest request) {
return getHeader("Referer", request);
}
/**
* Get subdomain name
*
* @param request current HTTP request
* @return string
*/
public static String getFullDomainName(JapHttpRequest request) {
StringBuffer url = request.getRequestUrl();
return url.delete(url.length() - request.getRequestUri().length(), url.length()).toString();
}
/**
* Get the User-Agent of the current HTTP request
*
* @param request current HTTP request
* @return string
*/
public static String getUa(JapHttpRequest request) {
return getHeader("User-Agent", request);
}
/**
* Get the IP of the current HTTP request
*
* @param request current HTTP request
* @return string
*/
public static String getIp(JapHttpRequest request) {
if (null == request) {
return "";
}
String[] headers = {"X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};
String ip;
for (String header : headers) {
ip = request.getHeader(header);
if (isValidIp(ip)) {
return getMultistageReverseProxyIp(ip);
}
}
ip = request.getRemoteAddr();
return getMultistageReverseProxyIp(ip);
}
/**
* Obtain the first non-unknown ip address from the multi-level reverse proxy
*
* @param ip IP
* @return The first non-unknown ip address
*/
private static String getMultistageReverseProxyIp(String ip) {
if (ip != null && ip.indexOf(",") > 0) {
final String[] ips = ip.trim().split(",");
for (String subIp : ips) {
if (isValidIp(subIp)) {
ip = subIp;
break;
}
}
}
return ip;
}
/**
* Verify ip legitimacy
*
* @param ip ip
* @return boolean
*/
private static boolean isValidIp(String ip) {
return isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip);
}
/**
* Get the value of the cookie
*
* @param request current HTTP request
* @param name cookie name
* @return String
*/
public static String getCookieVal(JapHttpRequest request, String name) {
JapHttpCookie cookie = getCookie(request, name);
return cookie != null ? cookie.getValue() : null;
}
private static boolean isNotEmpty(String s) {
return s != null && s.trim().length() != 0;
}
/**
* Get the request url
*
* @param encode Whether to encode url
* @param request current HTTP request
* @return string
*/
public static String getRequestUrl(boolean encode, JapHttpRequest request) {
if (null == request) {
return "";
}
String currentUrl = request.getRequestUrl().toString();
String queryString = request.getQueryString();
if (isNotEmpty(queryString)) {
currentUrl = currentUrl + "?" + queryString;
}
if (encode) {
String result = "";
try {
result = URLEncoder.encode(currentUrl, "UTF-8");
} catch (UnsupportedEncodingException e) {
//ignore
}
return result;
}
return currentUrl;
}
/**
* Get cookie
*
* @param request current HTTP request
* @param name cookie name
* @return Cookie
*/
public static JapHttpCookie getCookie(JapHttpRequest request, String name) {
JapHttpCookie[] cookies = request.getCookies();
if (cookies != null) {
for (JapHttpCookie cookie : cookies) {
if (name.equals(cookie.getName())) {
return cookie;
}
}
}
return null;
}
/**
* Get all the cookies, and use the cookie name as the key to form a map
*
* @param request current HTTP request
* @return Map
*/
public static Map<String, JapHttpCookie> getCookieMap(JapHttpRequest request) {
final JapHttpCookie[] cookies = request.getCookies();
if (null == cookies || cookies.length == 0) {
return new HashMap<>(0);
}
return Arrays.stream(cookies).collect(Collectors.toMap(JapHttpCookie::getName, v -> v, (k1, k2) -> k1));
}
/**
* Set cookie
*
* @param response current HTTP response
* @param name cookie name
* @param value cookie value
* @param maxAge maxAge
* @param path path
* @param domain domain
*/
public static void setCookie(JapHttpResponse response, String name, String value, int maxAge, String path, String domain) {
JapHttpCookie cookie = new JapHttpCookie(name, value);
cookie.setPath(path);
if (null != domain) {
cookie.setDomain(domain);
}
cookie.setMaxAge(maxAge);
cookie.setHttpOnly(false);
response.addCookie(cookie);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册