Skip to content

Commit

Permalink
Finished testing of Public Services.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Vásquez committed Jun 26, 2014
1 parent bf61b5a commit 10db6d7
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 109 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<module>private-services</module>
<module>private-services-client</module>
<module>notification-engine</module>
<module>public-api</module>
<module>public-services</module>
</modules>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public List<FavoriteRoute> findFavoriteRoutesByNotificationTimeRange(LocalTime f

@Override
public FavoriteRoute addFavoriteRoute(FavoriteRoute route) throws PrivateApiException {
String url = getAbsoluteUrl(BASE_URL_FAVORITE_ROUTES + URL_FAVORITE_ROUTES_REMOVE);
String url = getAbsoluteUrl(BASE_URL_FAVORITE_ROUTES + URL_FAVORITE_ROUTES_ADD);

return doPostForObject(url, route, FavoriteRoute.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ExceptionHandlers extends ResponseEntityExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(ExceptionHandlers.class);

@ExceptionHandler(PrivateApiException.class)
public ResponseEntity<Object> handleDataServiceException(PrivateApiException e, WebRequest request) {
public ResponseEntity<Object> handlePrivateApiException(PrivateApiException e, WebRequest request) {
return handleExceptionInternal(e, e.getErrorCode().getDefaultHttpStatus(), e.getErrorCode(), request);
}

Expand Down
94 changes: 0 additions & 94 deletions public-services/src/main/java/org/mytraffic/pub/User.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
package org.mytraffic.pub.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.bson.types.ObjectId;
import org.craftercms.commons.jackson.ObjectIdDeserializer;
import org.craftercms.commons.jackson.ObjectIdSerializer;
import org.mytraffic.utils.jackson.LocalTimeDeserializer;
import org.mytraffic.utils.jackson.LocalTimeSerializer;
import org.mytraffic.utils.jackson.ZonedDateTimeDeserializer;
import org.mytraffic.utils.jackson.ZonedDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.List;

/**
* Spring web application configuration.
Expand All @@ -12,6 +32,46 @@
*/
@Configuration
@EnableWebMvc
@ComponentScan("org.mytraffic.pub.rest.controllers.")
public class WebConfig {
@PropertySource("classpath:server.properties")
@ComponentScan("org.mytraffic.pub.rest.controllers")
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setOrder(Ordered.HIGHEST_PRECEDENCE);
configurer.setIgnoreUnresolvablePlaceholders(true);

return configurer;
}

@Bean
public ObjectMapper objectMapper() {
SimpleModule module = new SimpleModule();
module.addSerializer(new ObjectIdSerializer());
module.addSerializer(new LocalTimeSerializer());
module.addSerializer(new ZonedDateTimeSerializer());
module.addDeserializer(ObjectId.class, new ObjectIdDeserializer());
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer());
module.addDeserializer(ZonedDateTime.class, new ZonedDateTimeDeserializer());

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);

return objectMapper;
}

@Bean
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());

return converter;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.mytraffic.pub.rest.controllers;

import org.craftercms.profile.api.exceptions.ProfileException;
import org.craftercms.profile.exceptions.ProfileRestServiceException;
import org.mytraffic.priv.api.exceptions.PrivateApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
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.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.Collections;

/**
* Advice for the REST controllers that includes response handling for all major exceptions.
*
* @author avasquez
*/
@ControllerAdvice
public class ExceptionHandlers extends ResponseEntityExceptionHandler {

private static final Logger logger = LoggerFactory.getLogger(ExceptionHandlers.class);

@ExceptionHandler(PrivateApiException.class)
public ResponseEntity<Object> handlePrivateApiException(PrivateApiException e, WebRequest request) {
return handleExceptionInternal(e, e.getErrorCode().getDefaultHttpStatus(), request);
}

@ExceptionHandler(ProfileRestServiceException.class)
public ResponseEntity<Object> handleProfileRestServiceException(ProfileRestServiceException e, WebRequest request) {
return handleExceptionInternal(e, e.getStatus(), request);
}

@ExceptionHandler(ProfileException.class)
public ResponseEntity<Object> handleProfileException(ProfileException e, WebRequest request) {
return handleExceptionInternal(e, HttpStatus.INTERNAL_SERVER_ERROR, request);
}

@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
HttpStatus status, WebRequest request) {
return handleExceptionInternal(ex, headers, status, request);
}

protected ResponseEntity<Object> handleExceptionInternal(Exception ex, HttpStatus status, WebRequest request) {
return handleExceptionInternal(ex, new HttpHeaders(), status, request);
}

protected ResponseEntity<Object> handleExceptionInternal(Exception ex, HttpHeaders headers, HttpStatus status,
WebRequest request) {
logger.error("Request for " + ((ServletWebRequest) request).getRequest().getRequestURI() + " failed " +
"with HTTP status " + status, ex);

String message = ex.getMessage();

if (ex instanceof ProfileRestServiceException) {
message = ((ProfileRestServiceException) ex).getDetailMessage();
}

return new ResponseEntity<>(Collections.singletonMap("message", message), headers, status);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalTime;
import java.util.List;
Expand All @@ -22,13 +23,14 @@
* @author mariobarque
*/
@Controller
@RequestMapping("/api/1/route")
@RequestMapping("/api/1/routes")
public class RouteRestController {

@Autowired
private FavoriteRouteService favoriteRouteService;

@RequestMapping(value = "/favorite/register", method = RequestMethod.POST)
@ResponseBody
public FavoriteRoute registerFavoriteRoute(@RequestParam("userId") String userId,
@RequestParam("polyline") String polyline,
@RequestParam("description") String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.ZonedDateTime;

Expand All @@ -28,6 +29,7 @@ public class TrafficRestController {
private TrafficIncidentService trafficIncidentService;

@RequestMapping(value = "/incident/create", method = RequestMethod.POST)
@ResponseBody
public TrafficIncident createIncident(@RequestParam("latitude") double latitude,
@RequestParam("longitude") double longitude,
@RequestParam("type")IncidentType type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.mytraffic.pub.rest.controllers;

import org.craftercms.profile.api.Profile;
import org.craftercms.profile.api.exceptions.ProfileException;
import org.craftercms.profile.api.services.ProfileService;
import org.mytraffic.pub.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -23,22 +24,27 @@
@RequestMapping("/api/1/users")
public class UserRestController {

public static final String FIRST_NAME_ATTRIBUTE = "firstName";
public static final String LAST_NAME_ATTRIBUTE = "lastName";
public static final String PHONE_NUMBER_ATTRIBUTE = "phoneNumber";

@Value("${crafter.security.tenant.default.name}")
private String tenantName;
@Autowired
private ProfileService profileService;

@RequestMapping(value = "/register", method = RequestMethod.POST)
public User register(@RequestParam("username") String username, @RequestParam("password") String password,
@RequestParam("email") String email, @RequestParam("firstName") String firstName,
@RequestParam("lastName") String lastName, @RequestParam("phoneNumber") String phoneNumber)
@ResponseBody
public Profile register(@RequestParam("username") String username, @RequestParam("password") String password,
@RequestParam("email") String email, @RequestParam("firstName") String firstName,
@RequestParam("lastName") String lastName, @RequestParam("phoneNumber") String phoneNumber)
throws ProfileException {
Map<String, Object> attributes = new HashMap<>(3);
attributes.put(User.FIRST_NAME_ATTRIBUTE, firstName);
attributes.put(User.LAST_NAME_ATTRIBUTE, lastName);
attributes.put(User.PHONE_NUMBER_ATTRIBUTE, phoneNumber);
attributes.put(FIRST_NAME_ATTRIBUTE, firstName);
attributes.put(LAST_NAME_ATTRIBUTE, lastName);
attributes.put(PHONE_NUMBER_ATTRIBUTE, phoneNumber);

return new User(profileService.createProfile(tenantName, username, password, email, true, null, attributes, null));
return profileService.createProfile(tenantName, username, password, email, true, null, attributes, null);
}

}
19 changes: 19 additions & 0 deletions public-services/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
</appender>

<logger name="org.springframework" level="INFO"/>
<logger name="org.craftercms" level="INFO"/>
<logger name="org.mytraffic" level="DEBUG"/>

<root level="ERROR">
<appender-ref ref="STDOUT"/>
</root>

</configuration>
Loading

0 comments on commit 10db6d7

Please sign in to comment.