Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>jboss-snapshots-repository</id>
<name>JBoss Snapshots Repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -101,6 +106,12 @@
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>
hibernate-commons-annotations
</artifactId>
<groupId>org.hibernate.common</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down Expand Up @@ -149,6 +160,23 @@
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>

<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.5-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>opensymphony</groupId>
<artifactId>oscache</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.1</version>
</dependency>

<!-- <dependency>
<groupId>org.apache.activemq</groupId>
Expand Down
113 changes: 113 additions & 0 deletions src/main/java/org/telap/util/RequestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.telap.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Convenience class for setting and retrieving cookies.
*/
public final class RequestUtil {
private static final Log log = LogFactory.getLog(RequestUtil.class);

/**
* Checkstyle rule: utility classes should not have public constructor
*/
private RequestUtil() {
}

/**
* Convenience method to set a cookie
*
* @param response the current response
* @param name the name of the cookie
* @param value the value of the cookie
* @param path the path to set it on
*/
public static void setCookie(HttpServletResponse response, String name,
String value, String path) {
if (log.isDebugEnabled()) {
log.debug("Setting cookie '" + name + "' on path '" + path + "'");
}

Cookie cookie = new Cookie(name, value);
cookie.setSecure(false);
cookie.setPath(path);
cookie.setMaxAge(3600 * 24 * 30); // 30 days

response.addCookie(cookie);
}

/**
* Convenience method to get a cookie by name
*
* @param request the current request
* @param name the name of the cookie to find
*
* @return the cookie (if found), null if not found
*/
public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
Cookie returnCookie = null;

if (cookies == null) {
return returnCookie;
}

for (final Cookie thisCookie : cookies) {
if (thisCookie.getName().equals(name) && !"".equals(thisCookie.getValue())) {
returnCookie = thisCookie;
break;
}
}

return returnCookie;
}

/**
* Convenience method for deleting a cookie by name
*
* @param response the current web response
* @param cookie the cookie to delete
* @param path the path on which the cookie was set (i.e. /appfuse)
*/
public static void deleteCookie(HttpServletResponse response,
Cookie cookie, String path) {
if (cookie != null) {
// Delete the cookie by setting its maximum age to zero
cookie.setMaxAge(0);
cookie.setPath(path);
response.addCookie(cookie);
}
}

/**
* Convenience method to get the application's URL based on request
* variables.
*
* @param request the current request
* @return URL to application
*/
public static String getAppURL(HttpServletRequest request) {
if (request == null) return "";

StringBuffer url = new StringBuffer();
int port = request.getServerPort();
if (port < 0) {
port = 80; // Work around java.net.URL bug
}
String scheme = request.getScheme();
url.append(scheme);
url.append("://");
url.append(request.getServerName());
if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
url.append(':');
url.append(port);
}
url.append(request.getContextPath());
return url.toString();
}
}
Loading