Redirect HTTP to HTTPS using Servlet Filter

1. Write a filter to redirect from http to https


package vn.nvanhuong.servlet;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HTTPSFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws java.io.IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;

String uri = req.getRequestURI();
String getProtocol = req.getScheme();
String getDomain = req.getServerName();
String getPort = Integer.toString(req.getServerPort());

if (getProtocol.toLowerCase().equals("http")) {

// Set response content type
response.setContentType("text/html");

// New location to be redirected
String httpsPath = "https" + "://" + getDomain + ":" + getPort
+ uri;

String site = new String(httpsPath);
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
res.setHeader("Location", site);
}

// Pass request back down the filter chain
chain.doFilter(req, res);

}

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

@Override
public void destroy() {
// TODO Auto-generated method stub

}

}

2. Compile above filter and create following entries in web.xml


<!-- HTTPS Redirect -->
<filter>
<filter-name>HTTPS</filter-name>
<filter-class>vn.nvanhuong.servlet.HTTPSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HTTPS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

References
[1]. http://www.tutorialspoint.com/servlets/servlets-page-redirect.htm
[2]. http://www.easywayserver.com/blog/jsp-get-url/

[Java] Compare two list

Problem:

I have two classes are Employee and Department


class Department{
private Long deptId;
private String deptName;

public Long getDeptId() {
return deptId;
}
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}

class Employee{
private Long empId;
private String empName;
private Department dept;

public Employee() {
super();
}

public Employee(Long empId, String empName, Department dept) {
super();
this.empId = empId;
this.empName = empName;
this.dept = dept;
}
public Long getEmpId() {
return empId;
}
public void setEmpId(Long empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}

}

And, I write  a class for compare two list with data from Employee class: ListComparisonTest


package tdd.sprint10;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.*;

public class ListComparisonTest {

private Department dept;

public ListComparisonTest() {
}

@Before
public void setUp() throws Exception {
dept=new Department();
}

@After
public void tearDown() throws Exception {
}

@Test
public void testCompareList() {
Employee emp1 = new Employee();
emp1.setEmpId(1L);
emp1.setEmpName(&quot;Van Huong&quot;);
emp1.setDept(dept);

Employee emp2 = new Employee();
emp2.setEmpId(2L);
emp2.setEmpName(&quot;Xuan Vinh&quot;);
emp2.setDept(dept);

List&lt;Employee&gt; firstList = new ArrayList&lt;Employee&gt;();
firstList.add(emp1);
firstList.add(emp2);

List&lt;Employee&gt; secondList = new ArrayList&lt;Employee&gt;();
secondList.add(new Employee(1L,&quot;Van Huong&quot;,dept));
secondList.add(new Employee(2L,&quot;Xuan Vinh&quot;,dept));

assertEquals(firstList.size(), secondList.size());
assertTrue(firstList.containsAll(secondList));
}

}

It’s fail when I  run the test class with JUnit Test!

The solution: create hashCode() and equals() functions on Employee class

On Eclipse IDE, press Alt + Shift + S, chose  generate and hashCode() and equals() functions.


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dept == null) ? 0 : dept.hashCode());
result = prime * result + ((empId == null) ? 0 : empId.hashCode());
result = prime * result + ((empName == null) ? 0 : empName.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (dept == null) {
if (other.dept != null)
return false;
} else if (!dept.equals(other.dept))
return false;
if (empId == null) {
if (other.empId != null)
return false;
} else if (!empId.equals(other.empId))
return false;
if (empName == null) {
if (other.empName != null)
return false;
} else if (!empName.equals(other.empName))
return false;
return true;
}