Place detail request failing - reference too long - java

I have a small java app that, given a list of references for google places, must get back the Id's for each of said google places (long story short, we were storing references for places instead of their Id's, and only now realized that references are not unique per place).
My app works perfectly for about 95% of the places in the list, but fails with a "NOT_FOUND" status code for some records. Some investigation reveals that the place reference for these particular places is (when combined with the https://maps.googleapis.com/maps/api/place/details/json?sensor=false&key=myApiKey prefix) about 2 characters too long for a URL. The last couple of characters are getting truncated.
My initial thought was that I would just make a POST request to the google places API, but I'm getting back "REQUEST_DENIED" status code from the google servers when sending the same into as a POST request.
Is there anyway around this, or is this just an emergent bug with the google places API (now that the number of places has pushed the reference too long?).
I should also note that the places that fail are all recently added by our application.
This is what my current (working for 95%) code looks like:
public static JSONObject getPlaceInfo(String reference) throws Exception
{
URL places = new URL("https://maps.googleapis.com/maps/api/place/details/json?sensor=false&key="+apiKey+"&reference="+reference);
URLConnection con = places.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuffer input = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null)
input.append(inputLine);
in.close();
JSONObject response = (JSONObject) JSONSerializer.toJSON(input.toString());
return response;
}
and this is what my "ACCESS_DENIED" post code looks like:
public static JSONObject getPlaceInfo(String reference) throws Exception
{
String data = URLEncoder.encode("sensor", "UTF-8") + "=" + URLEncoder.encode("true", "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(apiKey, "UTF-8");
data += "&" + URLEncoder.encode("reference", "UTF-8") + "=" + URLEncoder.encode(reference, "UTF-8");
URL places = new URL("https://maps.googleapis.com/maps/api/place/details/json");
URLConnection con = places.openConnection();
con.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuffer input = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null)
input.append(inputLine);
in.close();
JSONObject response = (JSONObject) JSONSerializer.toJSON(input.toString());
return response;
}
An example of the reference that fails is:
CnRtAAAAxm0DftH1c5c6-krpWWZTT51uf0rDqCK4jikWV6eGfXlmKxrlsdrhFBOCgWOqChc1Au37inhf8HzjEbRdpMGghYy3dxGt17FEb8ys2CZCLHyC--7Vf1jn-Yn1kfZfzxznTJAbIEg6422q1kRbh0nl1hIQ71tmdOVvhdTfY_LOdbEoahoUnP0SAoOFNkk_KBIvTW30btEwkZs
Thanks in advance!

You're sending your request parameters in the body, which isn't supported by the API. There's a good answer about GET and request params at:
HTTP GET with request body
The following code should work for Place detail requests:
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_DETAILS = "/details";
private static final String OUT_JSON = "/json";
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE);
sb.append(TYPE_DETAILS);
sb.append(OUT_JSON);
sb.append("?sensor=false");
sb.append("&key=" + API_KEY);
sb.append("&reference=" + URLEncoder.encode(reference, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString()).getJSONObject("result");
jsonObj.getString("name");
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON results", e);
}

Related

Cross-origin token redemption is permitted only for the 'Single-Page Application'

I have been trying all day to get a minecraft plugin to allow players on bedrock edition on my geyser sever to (OPTIONALY) sign in to java edition using OAuth2's device code flow. I successfully can get a code and url but when I go to poll the API for a successful login I get "Cross-origin token redemption is permitted only for the 'Single-Page Application'." I've tried adding SPA to my azure app registration but the issue persists. I've tried setting the origin header in my request to "http://localhost" and the issue persist:
here is my code for retrieving the login token:
public static JSONObject pollSignIn(String deviceCode) {
double i = 0;
long previousTime = 0;
while (i <= 60000 /*GeyserFloodgateSkinFix.defaultConfig.requestTimeout*/) {
while (!(System.currentTimeMillis() > previousTime)) {}
previousTime = System.currentTimeMillis();
i++;
if ((i/1000) % 3 == 0) {
try {
URL url = new URL("https://login.microsoftonline.com/common/oauth2/v2.0/token");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Origin", null);
con.setDoOutput(true);
System.out.println(deviceCode);
String body = String.format(
"grant_type=urn:ietf:params:oauth:grant-type:device_code&client_id=%s&device_code=%s",
"[Censored]",
deviceCode
);
byte[] output = body.getBytes(StandardCharsets.UTF_8);
OutputStream os = con.getOutputStream();
os.write(output);
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(br.readLine());
}
JSONObject json = new JSONObject(sb.toString());
if (json.getString("token_type").equalsIgnoreCase("Bearer")) {
return json;
}
}
catch (Exception ignored) {
System.out.println(ignored.getMessage());
}
}
}
return null;
}
if it helps heres the code I use to get the token (This works)
public static JSONObject getAuthCode() {
try {
URL url = new URL("https://login.microsoftonline.com/common/oauth2/v2.0/devicecode");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setDoOutput(true);
String body = String.format(
"scope=XboxLive.signin%%20offline_access&client_id=%s",
"[Censored]"
);
OutputStream os = con.getOutputStream();
byte[] output = body.getBytes(StandardCharsets.UTF_8);
os.write(output, 0, output.length);
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
con.disconnect();
return new JSONObject(sb.toString());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
UPDATE: I managed to fix the above error but now I am getting "The provided value for the input parameter 'scope' is not valid. The scope 'XboxLive.signin offline_access' is not configured for this tenant." Chanfing the tenant to "consumer" throws "The provided value for the input parameter 'device_code' is not valid. Device codes supporting the personal Microsoft Account sign-in audience can only be used for v2 common or consumers tenants"

Get Json / Resonse body from Curl Post Request in Java

This is the method I have written which sends a POST request to send an Email.
I am able to send the email and get the Response Code 200 Ok.
But I don't know how to get the JSON Response and convert it into an Object.
Can someone please tell me how to do this?
public void sendEmail() {
try {
URL url = new URL("https://mandrillapp.com/api/1.0/messages/send");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
String data =
"{\"key\": \"" + mailchimpApiKey + "\", " +
"\"message\": {" +
"\"from_email\": \"from#gmail.com\", " +
"\"subject\": \"Hello World\", " +
"\"text\": \"Welcome to Mailchimp Transactional!\", " +
"\"to\": [{ \"email\": \"to#gmail.com\", \"type\": \"to\" }]}}";
byte[] out = data.getBytes(StandardCharsets.UTF_8);
OutputStream stream = httpURLConnection.getOutputStream();
stream.write(out);
System.out.println(httpURLConnection.getResponseCode() + " " + httpURLConnection.getResponseMessage());
httpURLConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
A basic search reveals: https://www.baeldung.com/httpurlconnection-post#8-read-the-response-from-input-stream
try(BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
If the response is in JSON format, use any third-party JSON parsers such as Jackson library, Gson, or org.json to parse the response.
In addition to the answer by #mdre
I use the org.json library to convert responses into JSON Objects. The following method does exactly this:
import org.json.JSONException;
import org.json.JSONObject;
public static JSONObject convertResponseToJSONObject(String responseString) {
try {
JSONObject jsonObj = new JSONObject(responseString);
return jsonObj;
} catch (JSONException e) {
System.err.println(
"It is not possible to create a JSONObject from the input string, returning null. Exception:");
e.printStackTrace();
}
return null;
}
Note that the response only represents a JSON object if it starts with a {. If it starts with a [ the response represents a JSON array.
You can get errorStream or inputStream based on the response code you receive and get the response from it. Below example creates a BufferedReader from the stream
BufferedReader br = null;
if (100 <= httpURLConnection.getResponseCode() && httpURLConnection.getResponseCode() <= 399) {
br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
} else {
br = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream()));
}
You can then read from the br and store data based on your requirement. Below will store data into StringBuilder
StringBuilder data = new StringBuilder();
String dataLine = null;
while ((dataLine = br.readLine()) != null) {
data.append(dataLine.trim());
}
System.out.println(data.toString());
Instead of printing as String you can also convert it into JSON by using JSON libraries. You may follow this guide

Posting JSON to REST API from Java Servlet

I'm having issues trying to send over a JSON string to a REST API. Long story short, I'm taking user input in a form, sending it over to a java servlet to validate and work with it a bit, and then trying to send it to an endpoint.
I have the following method being called on in my doPost method in my servlet, I am using printwriter pw to be able to read back data being returned in my response in the browser console at this point.
String jsonData = //JSON STRING HERE\\
String username = //USERNAME\\
String password = //PASSWORD\\
String endpointURL = //ENDPOINT URL HERE\\
pw.println(sendJson(jsonData, username, password));
private String sendJSON(String jsonData, String usrname, String usrpass) {
try {
String auth = usrname + ":" + usrpass;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
URL url = new URL(endpointURL);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setConnectTimeout(5000);
http.setReadTimeout(5000);
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "application/json; utf-8");
http.setRequestProperty("Authorization", authHeaderValue);
http.setDoOutput(true);
//POST Json to URL using HttpURLConnection
//try(OutputStream os = http.getOutputStream()) {
OutputStream os = http.getOutputStream();
byte[] input = jsonData.getBytes("utf-8");
os.write(input, 0, input.length);
//}
/*String responseBody;
try(BufferedReader br = new BufferedReader(
new InputStreamReader(http.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
//System.out.println(response.toString());
responseBody = response.toString();
return responseBody;
}
return responseBody;*/
BufferedReader in = new BufferedReader(
new InputStreamReader(http.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
} catch (IOException e) {
return e.toString();
}
}
}
I was having issues with the try's so I rewrote it to try and just get functionality right away first. Right now I'm receiving "java.io.IOException: Server Returned HTTP response code: 500 for URL: //URL HERE\"
Would anybody have any tips to point me in the right direction? I feel like I'm just missing like a small piece of the puzzle at this point, and I'm having a hard time finding any tutorials showing what it is that I'm trying to do. Thank you so much to anyone for any tips/pointers!
Made sure I was able to authenticate and that wasn't the issue by just connecting and returning:
int statusCode = http.getResponseCode();
String statusCodeString = Integer.toString(statusCode);
return statusCodeString;
This worked fine, received 403 response when setting wrong password/username and 400 response when I change to correct.
I attempted using HttpClient as well instead, but was having issues trying to get that to work at all. I also had an error earlier with week trying to do this with a certificate error, but after reimporting the cert to my cacerts file this was resolved (unrelated to this issue I believe).

How do i get a device token of my computer?

I am trying to create a small application that will read the CPU load of computers / laptops and send a push notification to the browser of my main laptop, which will contain the user name and how much the processor is loaded. As a technology for sending notifications, I chose FCM. The code itself is already ready, but I lack one detail. I need to get the device token of my laptop, to which this push notification will be sent (because as I understand it, the device token is the token of the computer where the notification is sent). But I do not know how to get this token. Most of the guidelines are directed to Android, and I need to send it from computer to computer. Maybe someone can tell me a different approach to sending these notifications, or the option that I attached is also suitable for a start? If so, how can I get this token?
public class MetricTesting {
Process p = Runtime.getRuntime().exec("typeperf \"\\238(_Total)\\6\"");
BufferedReader br = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String line;
double pr = 0;
Pattern pattern = Pattern.compile("[\\d]{0,3}\\.\\d{4,}");
while ((line = br.readLine()) != null) {
System.out.println(line);
Matcher m = pattern.matcher(line);
if (!m.find()) {
continue;
}
line = m.group();
pr = Math.round(Double.parseDouble(line) * 10.0) / 10.0;
System.out.println(pr);
if (pr > 5) {
PushNotificationSender.sendPushNotification("??", Double.toString(pr));
System.out.println(System.getProperty("user.name") + ", Processor loaded " + pr + " %");
}
}
String[] g = br.readLine().split("");
System.out.println(Arrays.toString(g));
br.close();
}
}
class PushNotificationSender {
public final static String AUTH_KEY_FCM = "//";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static String sendPushNotification(String deviceToken, String pr)
throws IOException, JSONException {
String result = "";
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM);
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("to", deviceToken.trim());
JSONObject info = new JSONObject();
info.put("title", "CPU is overloaded");
info.put("body", System.getProperty("user.name")+"\n"+pr);
json.put("notification", info);
try {
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
result = "OK";
} catch (Exception e) {
e.printStackTrace();
result = "BAD";
}
return result;
}
}
You must try https://firebase.google.com/docs/cloud-messaging/js/client to create a web client for firebase push notification.
In the above article, generating firebase token is also mentioned.
You can try to push notification thru topic instead of firebase token as well.

Is there a way to permanently remove a tile "programmatically" (Java) from caching in GeoServer?

I'm working with GeoServer and a Java backend. I am able to reset a tile's cache using the Rest API and to prevent any further cahing by removing the tile permanently from GeoServer's GUI (Tile Caching -> Tile Layers -> Check the tile -> Click on "Remove selected cached layers").
I would like to automatize the process and do it from back-end side. I tried truncate operations, dug in the rest api as well as Java objects but haven't been able to figure how.
Is is possible to permanently remove a tile from caching using Java? If yes, how?
Thank you for your help.
If you are trying to avoid caching a layer at all (rather than truncating a layer's cache) then you need to visit its GWC REST endpoint:
http://localhost:8080/geoserver/gwc/rest/layers/zoomstack:airports.xml
which will give you a file like:
<GeoServerLayer>
<id>LayerInfoImpl-36bac688:1666e6c28d4:-7ffd</id>
<enabled>true</enabled>
<inMemoryCached>true</inMemoryCached>
<name>zoomstack:airports</name>
<mimeFormats>
<string>image/png</string>
<string>image/jpeg</string>
</mimeFormats>
<gridSubsets>
<gridSubset>
<gridSetName>osgb</gridSetName>
</gridSubset>
<gridSubset>
<gridSetName>EPSG:900913</gridSetName>
</gridSubset>
<gridSubset>
<gridSetName>EPSG:4326</gridSetName>
</gridSubset>
</gridSubsets>
<metaWidthHeight>
<int>4</int>
<int>4</int>
</metaWidthHeight>
<expireCache>0</expireCache>
<expireClients>0</expireClients>
<parameterFilters>
<styleParameterFilter>
<key>STYLES</key>
<defaultValue/>
</styleParameterFilter>
</parameterFilters>
<gutter>0</gutter>
</GeoServerLayer>
You can then change <enabled>true</enabled> to <enabled>false</enabled> and PUT it back to the server.
As a complement to Ian's answer (works like a charm, thank you Ian), here is a Java snippet (that needs some love, constants, etc.):
private void removeLayerGroupFromCache() throws IOException {
URL url = new URL("http://localhost:8080/geoserver/gwc/rest/layers/layer group name.xml");
String rawLayer = getXMLContent(url);
String updatedLayer = formatXMLContent(rawLayer);
updateLayerInGIS(url, updatedLayer);
}
private String getXMLContent(URL url) throws IOException {
URLConnection urlConnection = setUsernamePassword(url);
InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder result = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
bufferedReader.close();
inputStreamReader.close();
return result.toString();
}
// TODO: This feels akward and should be improved. (Why extra spaces are being added with the urlConnection.getInputStream() call?)
private String formatXMLContent(String originalContent) {
originalContent = originalContent.replaceAll("\\s+", ""); // Removes undesired spaces.
originalContent = originalContent.replaceAll("layergroupname".replaceAll("\\s+", ""), "layer group name"); // Makes sure the name is matching if it contains white spaces.
originalContent = originalContent.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>".replaceAll("\\s+", ""), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // Re-append required spaces to XML declaration.
originalContent = originalContent.replace("<enabled>true</enabled>", "<enabled>false</enabled>"); // Finally, disable the caching for the group.
return originalContent;
}
private URLConnection setUsernamePassword(URL url) throws IOException {
String authStringEncoded = new String(Base64.encodeBase64("username:userpassword".getBytes()));
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEncoded);
return urlConnection;
}
private void updateLayerInGIS(URL url, String updatedLayer) throws IOException {
Authentication auth = authenticationResolver.retrieve();
String encodedCredentials = java.util.Base64.getEncoder().encodeToString("username:userpassword".getBytes());
URL directUrl = new URL("http", url.getHost(), url.getPort(), url.getFile());
HttpURLConnection httpCon = (HttpURLConnection) directUrl.openConnection();
httpCon.setRequestProperty("Authorization", "Basic " + encodedCredentials);
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
out.write(updatedLayer);
out.close();
int statusCode = httpCon.getResponseCode();
if(statusCode >= 200 && statusCode < 400) {
httpCon.getInputStream();
} else {
// Gets a more verbose message on why a non valid code has been returned.
String errorMessage = readStream(httpCon.getErrorStream());
throw new IOException(errorMessage);
}
}
private String readStream(InputStream stream) throws IOException {
StringBuilder builder = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(stream))) {
String line;
while ((line = in.readLine()) != null) {
builder.append(line);
}
}
return builder.toString();
}

Categories

Resources