master
esensar 2017-04-02 19:58:09 +02:00
parent 45f252b3a3
commit 907e0327df
34 changed files with 181 additions and 102 deletions

View File

@ -2,7 +2,7 @@ server.port=8888
spring.cloud.config.server.git.uri=https://github.com/esensar/steleks_config
spring.cloud.config.server.git.clone-on-start=true
security.user.name=root
security.user.password=skorpion
security.user.password=root
encrypt.key-store.location=classpath:config-server.jks
encrypt.key-store.password=my-s70r3-s3cr3t
encrypt.key-store.alias=config-server-key

View File

@ -1,11 +1,11 @@
package ba.steleks;
package ba.steleks.controller;
import ba.steleks.error.exception.ExternalServiceException;
import ba.steleks.repository.EventsJpaRepository;
import ba.steleks.repository.model.Event;
import ba.steleks.model.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
@ -15,9 +15,7 @@ import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.xml.ws.http.HTTPException;
import java.net.URI;
import java.util.HashMap;
/**
* Created by admin on 01/04/2017.
@ -37,7 +35,7 @@ public class EventController {
}
@RequestMapping(path = "/events", method = RequestMethod.POST)
public ResponseEntity<?> add(@RequestBody Event event) {
public ResponseEntity<?> add(@RequestBody Event event) throws ExternalServiceException {
String oviUseriNeki = "http://localhost:8090/users/{id}";
try {
@ -50,9 +48,9 @@ public class EventController {
return ResponseEntity.created(location).body(result);
} catch (Exception ex) {
if (ex instanceof HttpClientErrorException && ((HttpClientErrorException) ex).getStatusCode() == HttpStatus.NOT_FOUND) {
return ResponseEntity.badRequest().body(HttpStatus.BAD_REQUEST);
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST);
} else {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("Users service is dead! It's dead! There is no hope. It's all gone!");
throw new ExternalServiceException();
}
}
}

View File

@ -0,0 +1,54 @@
package ba.steleks.controller;/**
* Created by ensar on 02/04/17.
*/
import ba.steleks.error.exception.ExternalServiceException;
import ba.steleks.model.Event;
import ba.steleks.model.Media;
import ba.steleks.repository.MediaJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
@RepositoryRestController
public class MediaController {
private MediaJpaRepository repository;
private RestTemplate restTemplate;
@Autowired
public MediaController(RestTemplateBuilder restTemplateBuilder, MediaJpaRepository repository) {
this.repository = repository;
this.restTemplate = restTemplateBuilder.build();
}
@RequestMapping(path = "/medias", method = RequestMethod.POST)
public ResponseEntity<?> add(@RequestBody Media media) throws ExternalServiceException {
String oviUseriNeki = "http://localhost:8090/users/{id}";
try {
String response = restTemplate.getForObject(oviUseriNeki, String.class, media.getCreatedById());
Media result = repository.save(media);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest().path("/{id}")
.buildAndExpand(result.getId()).toUri();
return ResponseEntity.created(location).body(result);
} catch (Exception ex) {
if (ex instanceof HttpClientErrorException && ((HttpClientErrorException) ex).getStatusCode() == HttpStatus.NOT_FOUND) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST);
} else {
throw new ExternalServiceException();
}
}
}
}

View File

@ -0,0 +1,59 @@
package ba.steleks.error;
import ba.steleks.error.exception.ExternalServiceException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.HashMap;
import java.util.Map;
/**
* Created by admin on 01/04/2017.
*/
@ControllerAdvice
public class RestErrorHandler {
@ExceptionHandler(javax.validation.ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Map<String, String> handleCustomException(javax.validation.ConstraintViolationException ex) {
Map<String, String> map= new HashMap<String, String>();
map.put("status", HttpStatus.BAD_REQUEST.toString());
map.put("error", ex.getLocalizedMessage());
return map;
}
@ExceptionHandler(ExternalServiceException.class)
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
@ResponseBody
public Map<String, String> handleExternalServiceException(ExternalServiceException ex) {
Map<String, String> map= new HashMap<String, String>();
map.put("status", HttpStatus.SERVICE_UNAVAILABLE.toString());
map.put("error", HttpStatus.SERVICE_UNAVAILABLE.getReasonPhrase());
return map;
}
@ExceptionHandler(HttpStatusCodeException.class)
@ResponseBody
public ResponseEntity<Map<String, String>> handleAllExceptions(HttpStatusCodeException ex) {
Map<String, String> map= new HashMap<String, String>();
map.put("status", ex.getStatusCode().toString());
map.put("error", ex.getStatusCode().getReasonPhrase());
return ResponseEntity.status(ex.getStatusCode()).body(map);
}
}

View File

@ -0,0 +1,8 @@
package ba.steleks.error.exception;/**
* Created by ensar on 02/04/17.
*/
import javax.naming.ServiceUnavailableException;
public class ExternalServiceException extends ServiceUnavailableException {
}

View File

@ -1,4 +1,4 @@
package ba.steleks.repository.model;
package ba.steleks.model;
import javax.persistence.*;
import java.sql.Timestamp;

View File

@ -1,4 +1,4 @@
package ba.steleks.repository.model;
package ba.steleks.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

View File

@ -1,10 +1,8 @@
package ba.steleks.repository.model;
package ba.steleks.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.sql.Date;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Date;
/**
* Created by admin on 23/03/2017.
@ -18,7 +16,8 @@ public class Media {
private long id;
private String contentUrl;
private Date creationDate;
@Column(updatable = false, insertable = false)
private Timestamp creationDate;
private long createdById;
public Media() {
@ -40,11 +39,11 @@ public class Media {
this.contentUrl = contentUrl;
}
public Date getCreationDate() {
public Timestamp getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
public void setCreationDate(Timestamp creationDate) {
this.creationDate = creationDate;
}
@ -55,4 +54,10 @@ public class Media {
public void setCreatedById(long createdById) {
this.createdById = createdById;
}
@PrePersist
@PreUpdate
protected void onCreate() {
this.creationDate = new Timestamp(new Date().getTime());
}
}

View File

@ -1,6 +1,6 @@
package ba.steleks.repository;
import ba.steleks.repository.model.EventType;
import ba.steleks.model.EventType;
import org.springframework.data.repository.PagingAndSortingRepository;
/**

View File

@ -3,11 +3,9 @@
*/
package ba.steleks.repository;
import ba.steleks.repository.model.Event;
import ba.steleks.model.Event;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
public interface EventsJpaRepository extends PagingAndSortingRepository<Event, Long> {
}

View File

@ -1,6 +1,6 @@
package ba.steleks.repository;
import ba.steleks.repository.model.Media;
import ba.steleks.model.Media;
import org.springframework.data.repository.PagingAndSortingRepository;
/**

View File

@ -1 +0,0 @@
user.password={"description":"No key was installed for encryption service","status":"NO_KEY"}

View File

@ -1,7 +1,7 @@
server.port = 9020
spring.datasource.url = jdbc:mysql://localhost:3306/events
spring.datasource.username = root
spring.datasource.password = skorpion
spring.datasource.password = root
spring.jpa.generate-ddl=true
user.password=dizda
#

View File

@ -2,4 +2,4 @@ spring.application.name=steleks
# N.B. this is the default:
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.username=root
spring.cloud.config.password=skorpion
spring.cloud.config.password=root

View File

@ -1,10 +1,9 @@
package ba.steleks.repository.module;
package ba.steleks.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
/**
* Created by admin on 24/03/2017.

View File

@ -1,7 +1,6 @@
package ba.steleks.repository.module;
package ba.steleks.model;
import javax.persistence.*;
import javax.security.auth.Subject;
import java.util.Set;
/**

View File

@ -1,4 +1,4 @@
package ba.steleks.repository.module;
package ba.steleks.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

View File

@ -1,7 +1,6 @@
package ba.steleks.repository.module;
package ba.steleks.model;
import javax.persistence.*;
import java.util.Set;
/**
* Created by admin on 23/03/2017.

View File

@ -1,6 +1,6 @@
package ba.steleks.repository;
import ba.steleks.repository.module.Participant;
import ba.steleks.model.Participant;
import org.springframework.data.repository.PagingAndSortingRepository;
/**

View File

@ -1,6 +1,6 @@
package ba.steleks.repository;
import ba.steleks.repository.module.TeamCategory;
import ba.steleks.model.TeamCategory;
import org.springframework.data.repository.PagingAndSortingRepository;
/**

View File

@ -1,6 +1,6 @@
package ba.steleks.repository;
import ba.steleks.repository.module.Team;
import ba.steleks.model.Team;
import org.springframework.data.repository.PagingAndSortingRepository;
/**

View File

@ -1,6 +1,6 @@
package ba.steleks.repository;
import ba.steleks.repository.module.TeamMedia;
import ba.steleks.model.TeamMedia;
import org.springframework.data.repository.PagingAndSortingRepository;

View File

@ -1,6 +1,6 @@
server.port=9010
spring.datasource.url = jdbc:mysql://localhost:3306/teams
spring.datasource.username = root
spring.datasource.password = skorpion
spring.datasource.password = root
spring.jpa.generate-ddl=true

View File

@ -1,37 +0,0 @@
package ba.steleks.controller;
import ba.steleks.repository.UsersJpaRepository;
import ba.steleks.repository.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
/**
* Created by admin on 02/04/2017.
*/
@RepositoryRestController
public class UserController {
@Autowired
private UsersJpaRepository repository;
@RequestMapping(path = "/users", method = RequestMethod.POST)
public ResponseEntity<?> add(@RequestBody User user) {
System.out.println("doslo je do poziva");
User result = repository.save(user);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest().path("/{id}")
.buildAndExpand(result.getId()).toUri();
return ResponseEntity.created(location).body(result);
}
}

View File

@ -1,4 +1,4 @@
package ba.steleks.repository.model;/**
package ba.steleks.model;/**
* Created by ensar on 23/03/17.
*/

View File

@ -1,4 +1,4 @@
package ba.steleks.repository.model;/**
package ba.steleks.model;/**
* Created by ensar on 23/03/17.
*/

View File

@ -1,12 +1,9 @@
package ba.steleks.repository.model;/**
package ba.steleks.model;/**
* Created by ensar on 22/03/17.
*/
import org.hibernate.validator.constraints.NotBlank;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Set;
@ -27,8 +24,7 @@ public class User {
private String firstName;
@NotNull
private String lastName;
@NotNull
@Column(updatable = false, insertable = false)
@Column(updatable = false, insertable = false, columnDefinition="DATETIME default NOW()")
private Timestamp registrationDate;
@NotNull
private String email;
@ -161,4 +157,5 @@ public class User {
protected void onCreate() {
this.registrationDate = new Timestamp(new Date().getTime());
}
}

View File

@ -1,4 +1,4 @@
package ba.steleks.repository.model;/**
package ba.steleks.model;/**
* Created by ensar on 22/03/17.
*/

View File

@ -4,7 +4,7 @@ package ba.steleks.repository;
* Created by ensar on 22/03/17.
*/
import ba.steleks.repository.model.Course;
import ba.steleks.model.Course;
import org.springframework.data.repository.PagingAndSortingRepository;

View File

@ -4,7 +4,7 @@ package ba.steleks.repository;
* Created by ensar on 22/03/17.
*/
import ba.steleks.repository.model.MembershipType;
import ba.steleks.model.MembershipType;
import org.springframework.data.repository.PagingAndSortingRepository;

View File

@ -8,6 +8,7 @@ import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.rest.webmvc.support.RepositoryConstraintViolationExceptionMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ -35,13 +37,21 @@ public class RestErrorHandler extends ResponseEntityExceptionHandler {
public Map<String, String> handleCustomException(javax.validation.ConstraintViolationException ex) {
Map<String, String> map= new HashMap<String, String>();
map.put("status", "400");
map.put("error", "Bad request");
map.put("status", HttpStatus.BAD_REQUEST.toString());
map.put("error", ex.getLocalizedMessage());
return map;
}
@ExceptionHandler(HttpStatusCodeException.class)
@ResponseBody
public ResponseEntity<Map<String, String>> handleAllExceptions(HttpStatusCodeException ex) {
Map<String, String> map= new HashMap<String, String>();
map.put("status", ex.getStatusCode().toString());
map.put("error", ex.getLocalizedMessage());
return ResponseEntity.status(ex.getStatusCode()).body(map);
}
}

View File

@ -4,8 +4,7 @@ package ba.steleks.repository;
* Created by ensar on 22/03/17.
*/
import ba.steleks.repository.model.User;
import ba.steleks.repository.model.UserRole;
import ba.steleks.model.UserRole;
import org.springframework.data.repository.PagingAndSortingRepository;

View File

@ -4,17 +4,9 @@ package ba.steleks.repository;
* Created by ensar on 22/03/17.
*/
import ba.steleks.repository.model.User;
import ba.steleks.repository.model.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import ba.steleks.model.User;
import org.springframework.data.repository.PagingAndSortingRepository;
import javax.transaction.Transactional;
public interface UsersJpaRepository extends PagingAndSortingRepository<User, Long> {
}

View File

@ -1,6 +1,6 @@
server.port = 8090
spring.datasource.url = jdbc:mysql://localhost:3306/users
spring.datasource.username = root
spring.datasource.password = skorpion
spring.datasource.password = root
spring.jpa.generate-ddl=true