private void sendEmail(URL templateUrl, String emailAddress, String subject, String message) { try { String template = IoUtils.toString(templateUrl); String content = template.replace("${message-content}", message); // AwsUtils.sendEmail(subject, content, "Munchie Monster <support@munchiemonster.com>", new MunMonEmailAddress(emailAddress)); } catch (Exception e) { String msg = String.format("Exception sending email to %s", emailAddress); throw ApiException.internalServerError(msg, e); } }
@Override protected UserDetails retrieveUser(String userName, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { try { if (ProviderUtils.isGoogleAuthentication(authentication) == false) { throw new UsernameNotFoundException("Not Goolge Authentication"); } String code = authentication.getCredentials().toString(); GoogleAuthentication googleAuth = getAuthResponse(code, clientId, clientSecret); UserInfo userInfo = getUserInfo(googleAuth); CurrentUserSource source = store.getCurrentUserSourceByEmail(userInfo.getEmail()); if (source == null) { throw new BadCredentialsException(CurrentUser.INVALID_USER_NAME_OR_PASSWORD); } return new CurrentUser(source); } catch (IOException e) { throw ApiException.internalServerError("Exception during Google-Authentication", e); } }
public static DrivingDirections getDrivingDirections(LatLng locationA, LatLng locationB) throws IOException { // https://developers.google.com/maps/documentation/directions/ ExceptionUtils.assertNotNull(locationA, "locationA"); ExceptionUtils.assertNotNull(locationB, "locationB"); String url = "http://maps.googleapis.com/maps/api/directions/json"; url += String.format("?origin=%s,%s", locationA.getLatitude(), locationA.getLongitude()); url += String.format("&destination=%s,%s", locationB.getLatitude(), locationB.getLongitude()); url += "&sensor=false"; URLConnection connection = new URL(url).openConnection(); String json = IoUtils.toString(connection.getInputStream()); DrivingDirections directions = objectMapper.readValue(json, DrivingDirections.class); if (directions.getStatus().isNotOK()) { String msg = String.format("Unable to compute driving directions: %s (%s)", directions.getStatus(), directions.getStatus().getDescription()); throw ApiException.internalServerError(msg); } return directions; }
@Override protected UserDetails retrieveUser(String userName, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { try { if (ProviderUtils.isGoogleAuthentication(authentication) == false) { throw new UsernameNotFoundException("Not Goolge Authentication"); } String code = authentication.getCredentials().toString(); GoogleAuthentication googleAuth = GoogleUsersUtils.getAuthResponse(code, clientId, clientSecret); UserInfo userInfo = GoogleUsersUtils.getUserInfo(googleAuth.getAccessToken()); CurrentUserSource source = store.getCurrentUserSourceByEmail(userInfo.getEmail()); if (source == null) { throw new BadCredentialsException(CurrentUser.INVALID_USER_NAME_OR_PASSWORD); } return new CurrentUser(source); } catch (IOException e) { throw ApiException.internalServerError("Exception during Google-Authentication", e); } } }
throw ApiException.internalServerError(msg);
public static boolean createDatabase(CouchDatabase database, IdGenerator idGenerator, List<String> documentPaths) throws IOException { if (database.exists()) { log.info("The database "+database.getDatabaseName()+" already exists."); return false; } log.warn("Creating the database " + database.getDatabaseName()); WriteResponse response = database.createDatabase(); if (response.isCreated() == false) { String msg = String.format("Unable to create database: %s %s", response.getStatusCode().getCode(), response.getStatusCode().getReason()); throw ApiException.internalServerError(msg); } for (String documentPath : documentPaths) { log.warn("Creating the document "+documentPath); String json = IoUtils.toString(CouchUtils.class.getResourceAsStream(documentPath)); response = database.put().document(idGenerator.newId(), json).execute(); if (response.isCreated() == false) { String msg = String.format("Unable to put document %s: %s %s", documentPath, response.getStatusCode().getCode(), response.getStatusCode().getReason()); throw ApiException.internalServerError(msg); } } return true; }
public static GoogleAuthentication getAuthResponse(String code, String clientId, String clientSecret) throws IOException { Client client = ClientBuilder.newBuilder().build(); Form form = new Form(); form.param("code", code); form.param("client_id", clientId); form.param("client_secret", clientSecret); form.param("grant_type", "authorization_code"); form.param("redirect_uri", "postmessage"); UriBuilder uriBuilder = new JerseyUriBuilder().uri("https://accounts.google.com/o/oauth2/token"); Response jerseyResponse = client.target(uriBuilder) .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE)); int status = jerseyResponse.getStatus(); String json = jerseyResponse.readEntity(String.class); YakJacksonObjectMapper objectMapper = new YakJacksonObjectMapper(); GoogleAuthentication googleAuth = objectMapper.readValue(json, GoogleAuthentication.class); // If there was an error in the token info, abort. if (StringUtils.isNotBlank(googleAuth.getError())) { String msg = String.format("Authentication Error: %s", googleAuth.getError()); throw ApiException.internalServerError(msg); } return googleAuth; }