AWS IoT MQTT only works on the example topic? - java

I've just gotten started in AWS and IoT. Using the documentation and the tutorial I managed to get a working publish app ripped from the sample classes:
public static void main(String[] args) throws AWSIotException, InterruptedException {
String clientEndpoint = "<prefix>-ats.iot.us-west-2.amazonaws.com"; // replace <prefix> and <region> with your own
String clientId = "sdk-java-23"; // replace with your own client ID. Use unique client IDs for concurrent connections.
String certificateFile = "athing.cert.pem"; // X.509 based certificate file
String privateKeyFile = "athing.private.key"; // PKCS#1 or PKCS#8 PEM encoded private key file
// SampleUtil.java and its dependency PrivateKeyReader.java can be copied from the sample source code.
// Alternatively, you could load key store directly from a file - see the example included in this README.
SampleUtil.KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(certificateFile, privateKeyFile);
AWSIotMqttClient client = new AWSIotMqttClient(clientEndpoint, clientId, pair.keyStore, pair.keyPassword);
// optional parameters can be set before connect()
client.connect();
String topic = "sdk/test/java";
String payload = "[\n" +
"{\n" +
" \"id\": \"1231231234123\",\n" +
" \"value\": \"25\",\n" +
" \"unit\": \"°C\",\n" +
" \"timestamp\": \"1585954728\"\n" +
"},\n" +
"{\n" +
" \"id\": \"121231231233\",\n" +
" \"value\": \"26\",\n" +
" \"unit\": \"°B\",\n" +
" \"timestamp\": \"1585254728\"\n" +
"}"+
"]";
System.out.println(payload);
while (true) {
client.publish(topic, AWSIotQos.QOS0, payload);
System.out.println("message sent");
Thread.sleep(2000);
}
}
And I can see the messages coming through successfully on the aws console:
But if I change JUST the publish topic from:
String topic = "sdk/test/java";
to:
String topic = "sensors/temperature";
Now it no longer works. I don't see anything appear in the AWS console and the java program is showing some kind of connection error. My first instinct is some kind of security issue where it's not allowed to publish to any topic other than the one that is used in the sample program. I have no experience with IAM, cognito etc. so I would require some guidance (if that is the cause)
Apr 04, 2020 4:29:05 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established
Apr 04, 2020 4:29:05 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: sdk-java
Apr 04, 2020 4:29:05 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Apr 04, 2020 4:29:05 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sdk-java
Apr 04, 2020 4:29:08 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Apr 04, 2020 4:29:11 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established
Apr 04, 2020 4:29:11 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: sdk-java

So it turns out it was just a policy issue, I had no idea you had to define which ClientIDs and which topics are allowed to published/subscribed etc.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Publish",
"iot:Receive"
],
"Resource": [
"arn:aws:iot:us-west-2:<>:topic/sensors/realtime",
"arn:aws:iot:us-west-2:<>:topic/sdk/test/java",
"arn:aws:iot:us-west-2:<>:topic/sdk/test/Python",
"arn:aws:iot:us-west-2:<>:topic/topic_1",
"arn:aws:iot:us-west-2:<>:topic/topic_2"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Subscribe"
],
"Resource": [
"arn:aws:iot:us-west-2:<>:topicfilter/sensors/realtime",
"arn:aws:iot:us-west-2:<>:topicfilter/sdk/test/java",
"arn:aws:iot:us-west-2:<>:topicfilter/sdk/test/Python",
"arn:aws:iot:us-west-2:<>:topicfilter/topic_1",
"arn:aws:iot:us-west-2:<>:topicfilter/topic_2"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"arn:aws:iot:us-west-2:<>:client/JavaClient2",
"arn:aws:iot:us-west-2:<>:client/sdk-java",
"arn:aws:iot:us-west-2:<>:client/basicPubSub",
"arn:aws:iot:us-west-2:<>:client/sdk-nodejs-*"
]
}
]
}

Related

tomcat cannot connect to https site : SNI error

I have tomcat application running on Ubuntu. When it tries to make a https call to google recaptcha site, I get a error "No SNI provided, please fix client". The same code works fine outside tomcat when I run as a standalone application using same Java. Any ideas how to fix this.
URL app is trying to connect : https://www.google.com/recaptcha/api/siteverify
Details :
Java version : openjdk version "1.8.0_352"
Tomcat Details :
Server version: Apache Tomcat/8.5.57
Server built: Jul 16 2020 22:38:29 UTC
Server number: 8.5.57.0
OS Name: Linux
OS Version: 5.15.0-1028-aws
Architecture: amd64
JVM Version: 1.8.0_352-8u352-ga-1~20.04-b08
JVM Vendor: Private Build
Java code :
public String webRequest(String url) {
try {
InputStream res = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(res, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
res.close();
return sb.toString();
}catch (Exception e) {
return "";
}
}
Error:
javax.net.ssl|FINE|19|http-nio-8080-exec-8|2023-01-24 23:19:20.155 UTC|CertificateMessage.java:366|Consuming server Certificate handshake message (
"Certificates": [
"certificate" : {
"version" : "v3",
"serial number" : "00 90 76 89 18 E9 33 93 A0",
"signature algorithm": "SHA256withRSA",
"issuer" : "CN=invalid2.invalid, OU="No SNI provided; please fix your client."",
"not before" : "2015-01-01 24:00:00.000 UTC",
"not after" : "2030-01-01 24:00:00.000 UTC",
"subject" : "CN=invalid2.invalid, OU="No SNI provided; please fix your client."",
"subject public key" : "RSA",
"extensions" : [

How to do a GET request with params with dates ranges

im using karate framework to test some site, but one call to the endpoints is like this: https://urlbase/index?id=60&filters[start_date]=Fri%20Nov%2018%202022%2010:14:59%20GMT-0300&filters[end_date]=Sat%20Nov%2019%202022%2023:59:59%20GMT-0300
And i have problems with the filters[...] i don't know how to do that :(
I have this query def:
* def query = {id: 60, filters[start_date]:'Fri%20Nov%2018%202022%2010:14:59%20GMT-0300', filters[end_date]:'Sat%20Nov%2019%202022%2023:59:59%20GMT-0300'}
But the request url shows: GET https://urlbase/index?id=60&filters=%7Bstart_date%3D%2C+end_date%3D%7D
`
with no brackets.
And i want my GET url looks like this: https://urlbase/index?id=60&filters[start_date]=Fri%20Nov%2018%202022%2010:14:59%20GMT-0300&filters[end_date]=Sat%20Nov%2019%202022%2023:59:59%20GMT-0300
I tried this and it seems to work fine:
* url 'https://httpbin.org/anything'
* param id = 60
* param filters[start_date] = 'Fri Nov 18 2022 10:14:59 GMT-0300'
* param filters[end_date] = 'Sat Nov 19 2022 23:59:59 GMT-0300'
* method get
You can look at the response and confirm the server handles the request correctly:
{
"args": {
"filters[end_date]": "Sat Nov 19 2022 23:59:59 GMT-0300",
"filters[start_date]": "Fri Nov 18 2022 10:14:59 GMT-0300",
"id": "60"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.13 (Java/17.0.4.1)",
"X-Amzn-Trace-Id": "Root=1-6377b983-6bfc4696374f7d08239de823"
},
"json": null,
"method": "GET",
"origin": "X.X.X.X",
"url": "https://httpbin.org/anything?filters[start_date]=Fri+Nov+18+2022+10%3A14%3A59+GMT-0300&filters[end_date]=Sat+Nov+19+2022+23%3A59%3A59+GMT-0300&id=60"
}

Getting JSON names from String

I have a JSON String:
{
06-2020={
ma01={
0={duur=Dag, aantalVoertuigen=2, tijd=14: 06, naamHuurder=K, prijs=70 },
1={duur=Dag, aantalVoertuigen=2, tijd=14: 08, naamHuurder=H, prijs=70 },
2={duur=Dag, aantalVoertuigen=2, tijd=14: 08, naamHuurder=H, prijs=70 },
3={duur=Dag, aantalVoertuigen=2, tijd=14: 08, naamHuurder=H, prijs=70 },
Aantal=4
}
},
05-2020={
hallo={auto=ja, naam=ja },
za30={hallo=ja, naam=ja }
}
}
I need to get the 06-2020 and the 05-2020 out of it.
I tried using JSONobject. But that only works with one layer.
Thanks in advance

Solrj to query solr : dataimport.properties file

How can I use Solrj to query Solr using the following api:
http://localhost:8983/solr/admin/zookeeper?detail=true&path=%2Fconfigs%2Fmy-search%2Fdataimport.properties
the above api gives the content of the dataimport.properties file.
{
"znode": {
"path": "/configs/my-search/dataimport.properties",
"prop": {
"version": 186,
"aversion": 0,
"children_count": 0,
"ctime": "Sun Oct 16 10:24:04 UTC 2016 (1476613444895)",
"cversion": 0,
"czxid": 479,
"ephemeralOwner": 0,
"mtime": "Fri Mar 24 09:48:50 UTC 2017 (1490348930211)",
"mzxid": 31451,
"pzxid": 479,
"dataLength": 111
},
"data": "#Fri Mar 24 09:48:50 UTC 2017\nname.last_index_time=2017-03-24 09\\:48\\:49\nlast_index_time=2017-03-24 09\\:48\\:49\n"
},
"tree": [
{
"data": {
"title": "dataimport.properties",
"attr": {
"href": "admin/zookeeper?detail=true&path=%2Fconfigs%2Fmy-search%2Fdataimport.properties"
}
}
}
]
}
btw, my Solr is configured in cloud mode.
Given that your Solr is running in Cloud mode the file /configs/my-search/dataimport.properties is into Zookeeper.
SolrJ does not have native API to easily read from Zookeeper.
To read into the Zookeeper I suggest to use use Apache Curator Framework
String dataPath = "/configs/my-search/dataimport.properties";
String connectionString = "zookeeper-ensemble:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, new ExponentialBackoffRetry(1000, 3));
client.start();
byte[] barray = client.getData().forPath(dataPath);
if (barray != null) {
String data = new String(barray);
System.out.println(data);
}

Consuming Json from yahoo finance using RestTemplate

I'm trying to get some financial data from yahoo finance using java / spring RestTemplate.
Suppose I need the data for IBM, the link would be something like this:
https://query1.finance.yahoo.com/v10/finance/quoteSummary/IBM?formatted=true&modules=incomeStatementHistory%2CcashflowStatementHistory%2CbalanceSheetHistory%2CincomeStatementHistoryQuarterly%2CcashflowStatementHistoryQuarterly%2CbalanceSheetHistoryQuarterly%2Cearnings
which is a nice formatted JSON with all the data I need:
{
"quoteSummary": {
"result": [
{
"cashflowStatementHistoryQuarterly": {
"cashflowStatements": [
{
"maxAge": 1,
"endDate": {
"raw": 1459382400,
"fmt": "2016-03-31"
},
"netIncome": {
"raw": -5490000,
"fmt": "-5.49M",
"longFmt": "-5,490,000"
},
"depreciation": {
"raw": 16849000,
"fmt": "16.85M", ... etc
But when I try to open the same link using RestTemplate:
using a code like:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String>response = restTemplate.getForEntity(url, String.class);
The result I'm getting back in response is very different, it looks like this:
<200 OK,{"quoteSummary":{"result":[{}],"error":null}},{X-YQL-Host=[pprd3-node7511-lh2.manhattan.ir2.yahoo.com], X-Content-Type-Options=[nosniff], X-Yahoo-Request-Id=[6s0s3nlbqhlls], Cache-Control=[max-age=1, stale-while-revalidate=15], Content-Type=[application/json; charset=utf-8], Vary=[Origin], Content-Length=[45], Date=[Mon, 08 Aug 2016 19:00:12 GMT], Age=[2], Server=[ATS], Via=[http/1.1 media-ncache-api14.prod.media.ir2.yahoo.com (ApacheTrafficServer [cMsSfW]), http/1.1 media-ncache-api15.prod.media.ir2.yahoo.com (ApacheTrafficServer [cMsSf ]), http/1.1 media-router-api3.prod.media.ir2.yahoo.com (ApacheTrafficServer [cMsSfW]), https/1.1 r07.ycpi.loa.yahoo.net (ApacheTrafficServer [cMsSfW])], Strict-Transport-Security=[max-age=0], Connection=[keep-alive], Y-Trace=[BAEAQAAAAACPmeCQTRIz3QAAAAAAAAAAOEHjlsK9d9kAAAAAAAAAAAAFOZQGmKa_AAU5lAap7rIUoHdnAAAAAA--]}>
Any idea how can I get the same result I'm getting in the browser?
Thank you

Categories

Resources