Skip to content

Commit 5d938b6

Browse files
committed
added AOP to handle some errors in controllers
1 parent 245b5df commit 5d938b6

14 files changed

+105
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<root>
2+
<facet id="jst.jaxrs">
3+
<node name="libprov">
4+
<attribute name="provider-id" value="jaxrs-no-op-library-provider"/>
5+
</node>
6+
</facet>
7+
</root>

pom.xml

+13-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<plugin.war.warName>${project.build.finalName}</plugin.war.warName>
1515
<maven.test.skip>true</maven.test.skip>
1616
</properties>
17+
<!-- this manages all of spring dependencies and versions -->
1718
<dependencyManagement>
1819
<dependencies>
1920
<dependency>
@@ -69,6 +70,11 @@
6970
<artifactId>spring-aop</artifactId>
7071
<!-- <version>4.3.7.RELEASE</version> -->
7172
</dependency>
73+
<dependency>
74+
<groupId>org.springframework</groupId>
75+
<artifactId>spring-aspects</artifactId>
76+
<!-- <version>${spring.version}</version> -->
77+
</dependency>
7278
<dependency>
7379
<groupId>org.springframework</groupId>
7480
<artifactId>spring-expression</artifactId>
@@ -133,7 +139,13 @@
133139
<artifactId>jackson-dataformat-csv</artifactId>
134140
<!-- <version>${jackson.dataformat.csv.version}</version> -->
135141
</dependency>
136-
142+
<!-- Aspect oriented programming -->
143+
<dependency>
144+
<groupId>org.aspectj</groupId>
145+
<artifactId>aspectjrt</artifactId>
146+
<version>1.8.10</version>
147+
</dependency>
148+
137149
<dependency>
138150
<groupId>org.hibernate</groupId>
139151
<artifactId>hibernate-core</artifactId>
@@ -183,11 +195,6 @@
183195
<artifactId>commons-lang3</artifactId>
184196
<version>3.5</version>
185197
</dependency>
186-
187-
188-
<!-- Support for LocalDateTime and ZonedDateTime data conversion in SQL -->
189-
190-
191198
</dependencies>
192199
<build>
193200
<finalName>FStore</finalName>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.pier.aop;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
import org.aspectj.lang.annotation.Around;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.annotation.Pointcut;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.stereotype.Component;
10+
11+
import io.jsonwebtoken.ExpiredJwtException;
12+
13+
@Aspect
14+
@Component
15+
public class ErrorInterceptor {
16+
17+
@Pointcut("within(com.pier.controllers.user..*)")
18+
public void controller() {}
19+
20+
@Pointcut("execution(public org.springframework.http.ResponseEntity *(..))")// the pointcut expression
21+
private void allMethods() {}// the pointcut signature
22+
23+
@Around("controller() && allMethods()")
24+
public Object errorInterceptor(ProceedingJoinPoint joinPoint) throws Throwable{
25+
26+
try{
27+
return joinPoint.proceed();
28+
}catch (IndexOutOfBoundsException exp){
29+
return new ResponseEntity<String>("error processing request",HttpStatus.UNAUTHORIZED);
30+
}
31+
32+
33+
34+
}
35+
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pier.config;
22

33
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
45
import org.springframework.context.annotation.ImportResource;
56
import org.springframework.context.annotation.PropertySource;
67
import org.springframework.context.annotation.PropertySources;
@@ -9,8 +10,10 @@
910
@ImportResource("classpath:/persistence-beans.xml")
1011
@PropertySources({
1112
@PropertySource("classpath:jwt2.properties"),
12-
@PropertySource("classpath:paths.properties")
13+
@PropertySource("classpath:paths.properties"),
14+
@PropertySource("classpath:payment.properties")
1315
})
16+
@EnableAspectJAutoProxy(proxyTargetClass=true)
1417
public class SpringConfiguration {
1518

1619
}

src/main/java/com/pier/controllers/admin/ManageBrandRestController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public ResponseEntity<?> handleFileUpload( @RequestPart("file") MultipartFile fi
157157
String filePath = uploadsDir + name;
158158
File destination = new File(filePath);
159159
file.transferTo(destination);
160-
return new ResponseEntity<String>("success",HttpStatus.NO_CONTENT);
160+
return new ResponseEntity<String>(""+id,HttpStatus.OK);
161161
} catch (Exception e) {
162162

163163
return new ResponseEntity<String>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);

src/main/java/com/pier/controllers/admin/ManageCategoryRestController.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,19 @@ private Session currentSession(){
4545
}
4646

4747
@RequestMapping(method=RequestMethod.GET)
48-
public List<Category> list(@RequestParam("index") int index){
48+
public List<Category> list(@RequestParam("index") int index, @RequestParam(value="clothing",required=false) Boolean clothing,
49+
@RequestParam(value="all",required=false) Boolean all){
50+
if(clothing==null){
51+
clothing=false;
52+
}
4953
int pageSize=30;
5054
Criteria criteria = currentSession().createCriteria(Category.class);
5155
criteria.addOrder(Order.asc("id"));
5256
criteria.setFirstResult(index).setMaxResults(pageSize);
53-
return criteria.list();
57+
if(all==null || all==false)
58+
criteria.add(Restrictions.eq("clothing", clothing));
59+
List results=criteria.list();
60+
return results;
5461
}
5562

5663
@RequestMapping(params = "word",method=RequestMethod.GET)
@@ -93,6 +100,7 @@ public ResponseEntity<?> updateCategory(@RequestBody Category category, @PathVar
93100
Category currentCategory=dao.find(id);
94101
if(currentCategory!=null){
95102
currentCategory.setName(category.getName());
103+
currentCategory.setClothing(category.getClothing());
96104
dao.update(currentCategory);
97105
return new ResponseEntity<Category>(currentCategory,HttpStatus.OK);
98106
}

src/main/java/com/pier/controllers/admin/ManageProductsRestController.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.web.multipart.MultipartFile;
3333
import org.springframework.web.util.UriComponentsBuilder;
3434

35+
import com.fasterxml.jackson.databind.node.ObjectNode;
3536
import com.pier.business.validation.ProductIntegrityChecker;
3637
import com.pier.rest.model.Flavor;
3738
import com.pier.rest.model.Product;
@@ -175,10 +176,10 @@ public ResponseEntity<?> updateProduct(@PathVariable Long id, @RequestBody Produ
175176

176177
@PreAuthorize("hasRole('ADMIN')")
177178
@RequestMapping(value="setprice/{id}",method=RequestMethod.PUT)
178-
public ResponseEntity<?> updatePrice(@PathVariable Long id, @RequestBody String price){
179+
public ResponseEntity<?> updatePrice(@PathVariable Long id, @RequestBody ObjectNode json){
179180
Product product=this.dao.find(id);
180181
try{
181-
182+
String price=json.get("price").textValue();
182183
product.setPrice(new BigDecimal(price));
183184
dao.update(product);
184185
}catch(NullPointerException e){
@@ -217,7 +218,7 @@ public ResponseEntity<?> handleFileUpload( @RequestPart("file") MultipartFile fi
217218
String filePath = uploadsDir + name;
218219
File destination = new File(filePath);
219220
file.transferTo(destination);
220-
return new ResponseEntity<String>("success",HttpStatus.NO_CONTENT);
221+
return new ResponseEntity<String>(""+id,HttpStatus.OK);
221222
} catch (Exception e) {
222223

223224
return new ResponseEntity<String>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);

src/main/java/com/pier/controllers/admin/ManagePromotionsRestController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public ResponseEntity<?> handleFileUpload( @RequestPart("file") MultipartFile fi
227227
String filePath = uploadsDir + name;
228228
File destination = new File(filePath);
229229
file.transferTo(destination);
230-
return new ResponseEntity<String>("success",HttpStatus.NO_CONTENT);
230+
return new ResponseEntity<String>(""+id,HttpStatus.OK);
231231
} catch (Exception e) {
232232

233233
return new ResponseEntity<String>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);

src/main/java/com/pier/controllers/admin/ManageUserRestController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public ResponseEntity<?> updateUser(@PathVariable("id") long id, @RequestBody Us
173173

174174
@PreAuthorize("hasRole('ADMIN')")
175175
@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
176-
public ResponseEntity<User> deleteUser(@PathVariable("id") long id) {
176+
public ResponseEntity<?> deleteUser(@PathVariable("id") long id) {
177177
User user = userDao.find(id);
178178
if (user == null) {
179179
System.out.println("Unable to delete. User with id " + id + " not found");

src/main/java/com/pier/security/controller/UserRestController.java src/main/java/com/pier/controllers/user/UserRestController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.pier.security.controller;
1+
package com.pier.controllers.user;
22

33
import java.util.Collections;
44
import java.util.HashSet;

src/main/java/com/pier/rest/model/Category.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.pier.rest.model;
22

3+
import java.util.ArrayList;
4+
import java.util.Collection;
35
import java.util.List;
46

57
import javax.persistence.Column;
@@ -30,7 +32,11 @@ public class Category implements ObjectModel<Long> {
3032
@Column(name="NAME", length=50, unique=true)
3133
@NotNull
3234
@Size(min=3, max=20)
33-
private String name;
35+
private String name;
36+
37+
@Column(name="CLOTHING" , columnDefinition="boolean default false")
38+
@NotNull
39+
private Boolean clothing=false;
3440

3541
@JsonIgnore
3642
@ManyToMany(mappedBy="categories", fetch=FetchType.LAZY)
@@ -72,6 +78,15 @@ public List<Product> getProducts() {
7278

7379
public void setProducts(List<Product> products) {
7480
this.products = products;
81+
}
82+
83+
84+
public Boolean getClothing() {
85+
return clothing;
86+
}
87+
88+
public void setClothing(Boolean clothing) {
89+
this.clothing = clothing;
7590
}
7691

7792
@Override

src/main/java/com/pier/security/controller/MethodProtectedRestController.java

-19
This file was deleted.

src/main/java/com/pier/security/util/JwtTokenUtil.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ public class JwtTokenUtil implements Serializable{
4646

4747
private Long expiration;
4848

49-
public String getUsernameFromToken(String token) {
49+
public String getUsernameFromToken(String token) throws ExpiredJwtException {
5050
String username;
5151
try {
5252
final Claims claims = getClaimsFromToken(token);
5353
username = claims.getSubject();
54+
}catch(ExpiredJwtException exp){
55+
username=null;
5456
}catch (Exception e) {
5557
username = null;
5658
}
@@ -98,6 +100,9 @@ private Claims getClaimsFromToken(String token) {
98100
.setSigningKey(secret)
99101
.parseClaimsJws(token)
100102
.getBody();
103+
}catch(ExpiredJwtException exp){
104+
claims=null;
105+
throw new ExpiredJwtException(null, claims, exp.getMessage());
101106
}catch (Exception ex) {
102107
claims = null;
103108
}

src/main/resources/payment.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public_key=TEST-2d66a12c-4a59-47d7-8140-53413c396638
2+
client_id=5085861350610208
3+
client_secret=P4ZWdNNkLWWqizSPGEot19xe5aIPcTEG
4+
access_token=TEST-5085861350610208-092014-71c0af5021dd4838f893ebf9a99375ec__LB_LD__-213055451

0 commit comments

Comments
 (0)