@Override public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> map, InputStream stream) throws IOException, WebApplicationException { String json = IoUtils.toString(stream); return objectMapper.readValue(json, type); }
public byte[] getBytes(String subUrl, Map<String, Object> queryMap, String...acceptedResponseTypes) throws IOException { Invocation.Builder builder = builder(subUrl, queryMap, acceptedResponseTypes); Response response = builder.get(Response.class); if (response.getStatus() == 404 && notFoundToNull) return null; assertResponse(response.getStatus()); InputStream in = (InputStream)response.getEntity(); return IoUtils.toBytes(in); }
IoUtils.createPath(new File(args.getWorkingDirectory()));
@Override public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> map, InputStream stream) throws IOException, WebApplicationException { String json = IoUtils.toString(stream); return objectMapper.readValue(json, type); }
@Override public void writeTo(LocalResource localResource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { String view = localResource.getView(); String resource = "/push-server-app/view" + (view.startsWith("/") ? "" : "/") + view; InputStream is = getClass().getResourceAsStream(resource); if (is == null) { throw new NotFoundException("View: " + view); } byte[] bytes = IoUtils.toBytes(is); entityStream.write(bytes); } }
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); } }
public static UserAgentModel lookupUserAgent(String userAgentString) throws IOException { String encodedUserAgent = StringUtils.encodeUrl(userAgentString); String url = "http://www.useragentstring.com/?getJSON=all&uas=" + encodedUserAgent; URLConnection connection = new URL(url).openConnection(); String json = IoUtils.toString(connection.getInputStream()); return objectMapper.readValue(json, UserAgentModel.class); }
public static LatLng getLatLng(String address, String city, String state, String zip) throws IOException { String url = "https://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address="; url += encodeUrl(String.format("%s, %s, %s %s", address, city, state, zip)); URLConnection connection = new URL(url).openConnection(); String data = IoUtils.toString(connection.getInputStream()); data = getTagContents(data, "location", 0); return new LatLng( getTagContents(data, "lat", 0), getTagContents(data, "lng", 0) ); } }
@Override public String getAdminUi(PluginContext context, Domain domain) throws IOException { LqNotificationsConfig config = getConfig(context.getCouchServer(), domain); InputStream stream = getClass().getResourceAsStream("/com/cosmicpush/plugins/notifier/admin.html"); String content = IoUtils.toString(stream); content = content.replace("${legend-class}", nullToString(config == null ? "no-config" : "")); content = content.replace("${push-type}", nullToString(getPushType().getCode())); content = content.replace("${plugin-name}", nullToString(getPluginName())); content = content.replace("${domain-key}", nullToString(domain.getDomainKey())); content = content.replace("${push-server-base}", nullToString(context.getBaseURI())); content = content.replace("${config-user-name}", nullToString(config == null ? null : config.getUserName())); if (content.contains("${")) { String msg = String.format("The Notification admin UI still contains un-parsed elements."); throw new IllegalStateException(msg); } return content; } }
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 public String getAdminUi(PluginContext context, Domain domain) throws IOException { GoogleTalkConfig config = getConfig(context.getCouchServer(), domain); InputStream stream = getClass().getResourceAsStream("/com/cosmicpush/plugins/gtalk/admin.html"); String content = IoUtils.toString(stream); content = content.replace("${legend-class}", nullToString(config == null ? "no-config" : "")); content = content.replace("${push-type}", nullToString(getPushType().getCode())); content = content.replace("${plugin-name}", nullToString(getPluginName())); content = content.replace("${domain-key}", nullToString(domain.getDomainKey())); content = content.replace("${push-server-base}", nullToString(context.getBaseURI())); content = content.replace("${config-user-name}", nullToString(config == null ? null : config.getUserName())); content = content.replace("${config-password}", nullToString(config == null ? null : config.getPassword())); content = content.replace("${config-test-address}", nullToString(config == null ? null : config.getTestAddress())); content = content.replace("${config-recipient-override}", nullToString(config == null ? null : config.getRecipientOverride())); if (content.contains("${")) { String msg = String.format("The Google Talk admin UI still contains un-parsed elements."); throw new IllegalStateException(msg); } return content; } }
@Override public String getAdminUi(PluginContext context, Domain domain) throws IOException { SesEmailConfig config = getConfig(context.getCouchServer(), domain); InputStream stream = getClass().getResourceAsStream("/com/cosmicpush/plugins/ses/admin.html"); String content = IoUtils.toString(stream); content = content.replace("${legend-class}", nullToString(config == null ? "no-config" : "")); content = content.replace("${push-type}", nullToString(getPushType().getCode())); content = content.replace("${plugin-name}", nullToString(getPluginName())); content = content.replace("${domain-key}", nullToString(domain.getDomainKey())); content = content.replace("${push-server-base}", nullToString(context.getBaseURI())); content = content.replace("${config-access-key-id}", nullToString(config == null ? null : config.getAccessKeyId())); content = content.replace("${config-secret-key}", nullToString(config == null ? null : config.getSecretKey())); content = content.replace("${config-endpoint}", nullToString(config == null ? null : config.getEndpoint())); content = content.replace("${config-test-to-address}", nullToString(config == null ? null : config.getTestToAddress())); content = content.replace("${config-test-from-address}", nullToString(config == null ? null : config.getTestFromAddress())); content = content.replace("${config-recipient-override}", nullToString(config == null ? null : config.getRecipientOverride())); if (content.contains("${")) { String msg = String.format("The SES admin UI still contains un-parsed elements."); throw new IllegalStateException(msg); } return content; } }
String expectedDoc = IoUtils.toString(in);
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; }