Skip to content

Commit 7ffac0e

Browse files
committed
2 parents 4116810 + 43f9085 commit 7ffac0e

17 files changed

Lines changed: 7404 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# grants

pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.uts.grants</groupId>
5+
<artifactId>grants</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0.0</version>
8+
<name>gratsweb Maven Webapp</name>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<springframework.version>4.3.1.RELEASE</springframework.version>
13+
<jackson.version>2.7.5</jackson.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework</groupId>
19+
<artifactId>spring-webmvc</artifactId>
20+
<version>${springframework.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.fasterxml.jackson.core</groupId>
24+
<artifactId>jackson-databind</artifactId>
25+
<version>${jackson.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>javax.servlet</groupId>
29+
<artifactId>javax.servlet-api</artifactId>
30+
<version>3.1.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>javax.servlet</groupId>
34+
<artifactId>jstl</artifactId>
35+
<version>1.2</version>
36+
</dependency>
37+
</dependencies>
38+
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.2</version>
46+
<configuration>
47+
<source>1.7</source>
48+
<target>1.7</target>
49+
</configuration>
50+
</plugin>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-war-plugin</artifactId>
54+
<version>2.4</version>
55+
<configuration>
56+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
57+
<warName>grants</warName>
58+
<failOnMissingWebXml>false</failOnMissingWebXml>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
<finalName>grants</finalName>
63+
</build>
64+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.websystique.springmvc.configuration;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.Filter;
6+
import javax.servlet.FilterChain;
7+
import javax.servlet.FilterConfig;
8+
import javax.servlet.ServletException;
9+
import javax.servlet.ServletRequest;
10+
import javax.servlet.ServletResponse;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
14+
public class CORSFilter implements Filter {
15+
16+
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
17+
HttpServletResponse response = (HttpServletResponse) res;
18+
response.setHeader("Access-Control-Allow-Origin", "*");
19+
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
20+
response.setHeader("Access-Control-Max-Age", "3600");
21+
response.setHeader("Access-Control-Allow-Headers", "X-requested-with, Content-Type");
22+
chain.doFilter(req, res);
23+
}
24+
25+
public void init(FilterConfig filterConfig) {}
26+
27+
public void destroy() {}
28+
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.websystique.springmvc.configuration;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
7+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
10+
import org.springframework.web.servlet.view.JstlView;
11+
12+
@Configuration
13+
@EnableWebMvc
14+
@ComponentScan(basePackages = "com.websystique.springmvc")
15+
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter{
16+
17+
@Override
18+
public void configureViewResolvers(ViewResolverRegistry registry) {
19+
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
20+
viewResolver.setViewClass(JstlView.class);
21+
viewResolver.setPrefix("/WEB-INF/views/");
22+
viewResolver.setSuffix(".jsp");
23+
registry.viewResolver(viewResolver);
24+
}
25+
26+
@Override
27+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
28+
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
29+
}
30+
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.websystique.springmvc.configuration;
2+
// class file
3+
import javax.servlet.Filter;
4+
5+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
6+
7+
public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
8+
9+
@Override
10+
protected Class<?>[] getRootConfigClasses() {
11+
return new Class[] { HelloWorldConfiguration.class };
12+
}
13+
14+
@Override
15+
protected Class<?>[] getServletConfigClasses() {
16+
return null;
17+
}
18+
19+
@Override
20+
protected String[] getServletMappings() {
21+
return new String[] { "/" };
22+
}
23+
24+
@Override
25+
protected Filter[] getServletFilters() {
26+
Filter [] singleton = { new CORSFilter() };
27+
return singleton;
28+
}
29+
30+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.websystique.springmvc.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.RestController;
15+
import org.springframework.web.util.UriComponentsBuilder;
16+
17+
import com.websystique.springmvc.model.User;
18+
import com.websystique.springmvc.service.UserService;
19+
20+
@RestController
21+
public class HelloWorldRestController {
22+
23+
@Autowired
24+
UserService userService; //Service which will do all data retrieval/manipulation work
25+
26+
27+
//-------------------Retrieve All Users--------------------------------------------------------
28+
29+
@RequestMapping(value = "/user/", method = RequestMethod.GET)
30+
public ResponseEntity<List<User>> listAllUsers() {
31+
List<User> users = userService.findAllUsers();
32+
if(users.isEmpty()){
33+
return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
34+
}
35+
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
36+
}
37+
38+
39+
40+
//-------------------Retrieve Single User--------------------------------------------------------
41+
42+
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
43+
public ResponseEntity<User> getUser(@PathVariable("id") long id) {
44+
System.out.println("Fetching User with id " + id);
45+
User user = userService.findById(id);
46+
if (user == null) {
47+
System.out.println("User with id " + id + " not found");
48+
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
49+
}
50+
return new ResponseEntity<User>(user, HttpStatus.OK);
51+
}
52+
53+
54+
55+
//-------------------Create a User--------------------------------------------------------
56+
57+
@RequestMapping(value = "/user/", method = RequestMethod.POST)
58+
public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
59+
System.out.println("Creating User " + user.getUsername());
60+
61+
if (userService.isUserExist(user)) {
62+
System.out.println("A User with name " + user.getUsername() + " already exist");
63+
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
64+
}
65+
66+
userService.saveUser(user);
67+
68+
HttpHeaders headers = new HttpHeaders();
69+
headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
70+
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
71+
}
72+
73+
74+
75+
//------------------- Update a User --------------------------------------------------------
76+
77+
@RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
78+
public ResponseEntity<User> updateUser(@PathVariable("id") long id, @RequestBody User user) {
79+
System.out.println("Updating User " + id);
80+
81+
User currentUser = userService.findById(id);
82+
83+
if (currentUser==null) {
84+
System.out.println("User with id " + id + " not found");
85+
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
86+
}
87+
88+
currentUser.setUsername(user.getUsername());
89+
currentUser.setAddress(user.getAddress());
90+
currentUser.setEmail(user.getEmail());
91+
92+
userService.updateUser(currentUser);
93+
return new ResponseEntity<User>(currentUser, HttpStatus.OK);
94+
}
95+
96+
97+
98+
//------------------- Delete a User --------------------------------------------------------
99+
100+
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
101+
public ResponseEntity<User> deleteUser(@PathVariable("id") long id) {
102+
System.out.println("Fetching & Deleting User with id " + id);
103+
104+
User user = userService.findById(id);
105+
if (user == null) {
106+
System.out.println("Unable to delete. User with id " + id + " not found");
107+
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
108+
}
109+
110+
userService.deleteUserById(id);
111+
return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
112+
}
113+
114+
115+
116+
//------------------- Delete All Users --------------------------------------------------------
117+
118+
@RequestMapping(value = "/user/", method = RequestMethod.DELETE)
119+
public ResponseEntity<User> deleteAllUsers() {
120+
System.out.println("Deleting All Users");
121+
122+
userService.deleteAllUsers();
123+
return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
124+
}
125+
126+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.websystique.springmvc.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
7+
@Controller
8+
@RequestMapping("/")
9+
public class IndexController {
10+
11+
@RequestMapping(method = RequestMethod.GET)
12+
public String getIndexPage() {
13+
return "UserManagement";
14+
}
15+
16+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.websystique.springmvc.model;
2+
3+
public class User {
4+
5+
private long id;
6+
7+
private String username;
8+
9+
private String address;
10+
11+
private String email;
12+
13+
public User(){
14+
id=0;
15+
}
16+
17+
public User(long id, String username, String address, String email){
18+
this.id = id;
19+
this.username = username;
20+
this.address = address;
21+
this.email = email;
22+
}
23+
24+
public long getId() {
25+
return id;
26+
}
27+
28+
public void setId(long id) {
29+
this.id = id;
30+
}
31+
32+
public String getUsername() {
33+
return username;
34+
}
35+
36+
public void setUsername(String username) {
37+
this.username = username;
38+
}
39+
40+
public String getAddress() {
41+
return address;
42+
}
43+
44+
public void setAddress(String address) {
45+
this.address = address;
46+
}
47+
48+
public String getEmail() {
49+
return email;
50+
}
51+
52+
public void setEmail(String email) {
53+
this.email = email;
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
final int prime = 31;
59+
int result = 1;
60+
result = prime * result + (int) (id ^ (id >>> 32));
61+
return result;
62+
}
63+
64+
@Override
65+
public boolean equals(Object obj) {
66+
if (this == obj)
67+
return true;
68+
if (obj == null)
69+
return false;
70+
if (!(obj instanceof User))
71+
return false;
72+
User other = (User) obj;
73+
if (id != other.id)
74+
return false;
75+
return true;
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return "User [id=" + id + ", username=" + username + ", address=" + address
81+
+ ", email=" + email + "]";
82+
}
83+
84+
85+
86+
}

0 commit comments

Comments
 (0)