how to send HTTP Post request using jersey with a complex parameter - java

I need to send a HTTP Post to a REST API with the following complex type as parameters. I looked at the documentation of jersey and it helps only to send a key value pair. How can i send a HTTP Post request with the below parameters using jersey.
{
"key": "example key",
"message": {
"html": "<p>Example HTML content</p>",
"text": "Example text content",
"subject": "example subject",
"from_email": "message.from_email#example.com",
"from_name": "Example Name",
"to": [
{
"email": "recipient.email#example.com",
"name": "Recipient Name"
}
],
"headers": {
"Reply-To": "message.reply#example.com"
},
"important": false,
"track_opens": null,
"track_clicks": null,
"auto_text": null,
"auto_html": null,
"inline_css": null,
"url_strip_qs": null,
"preserve_recipients": null,
"view_content_link": null,
"bcc_address": "message.bcc_address#example.com",
"tracking_domain": null,
"signing_domain": null,
"return_path_domain": null,
"merge": true,
"global_merge_vars": [
{
"name": "merge1",
"content": "merge1 content"
}
],
"merge_vars": [
{
"rcpt": "recipient.email#example.com",
"vars": [
{
"name": "merge2",
"content": "merge2 content"
}
]
}
],
"tags": [
"password-resets"
],
"subaccount": "customer-123",
"google_analytics_domains": [
"example.com"
],
"google_analytics_campaign": "message.from_email#example.com",
"metadata": {
"website": "www.example.com"
},
"recipient_metadata": [
{
"rcpt": "recipient.email#example.com",
"values": {
"user_id": 123456
}
}
],
"attachments": [
{
"type": "text/plain",
"name": "myfile.txt",
"content": "ZXhhbXBsZSBmaWxl"
}
],
"images": [
{
"type": "image/png",
"name": "IMAGECID",
"content": "ZXhhbXBsZSBmaWxl"
}
]
},
"async": false,
"ip_pool": "Main Pool",
"send_at": "example send_at"
}
I looked at the other questions of sending HTTP Post using Jersey and all I could find was a way to only send a key\value pairs as parameters and not complex string types like above.

You should look at JAXB, it allows you to "automatically" build "complex parameters" out of "objects". Basically the procedure would be to define a class that represents the data structure you present as request message the REST API resource accepts, then populate it with the data your want to POST and send it. In this question you can find more details on how to exactly do that: Can jersey clients POST a JAXB object to the server using JSON?

Related

Slack SDK block builder in spring boot

I'm constructing a message to be sent through my spring boot application.
I was testing out the templates and I have created one where most of the elements are static except a link that needs to be generated by the code and added to the Json.
Currently the Json message looks like this:
{
"blocks": [
{
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://api.slack.com/img/blocks/bkb_template_images/highpriority.png",
"alt_text": "High Priority"
},
{
"type": "mrkdwn",
"text": "*High Priority*"
}
]
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Hercules Platform Status Response failed Messages*"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Please click the link to download the file*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*<LINK|SOME_LINK>*"
}
}
]
}
I'm not sure how to construct this Json in my spring boot application. Since most of it is static, should I just load this template as a string and append the last link section?
I'm not able to figure out the slack classes in java to build such a message.

Sending field metadata to client in Spring

I want my client to be able to request metadata about a #RequestMapping, like the #Size and #Length values of each field. Does Spring have a way of doing this?
Closest thing might be Spring HATEOAS Affordances for HAL forms.
Spring HATEOAS Affordances
Responses produce output which includes all the metadata about the request model along with validation rules and method.
{
"_embedded": {
"employees": [...]
},
"_links": {
"self": {
"href": "http://localhost:8080/employees"
}
},
"_templates": {
"default": {
"title": null,
"method": "post",
"contentType": "",
"properties":[
{
"name": "firstName",
"required": true
},
{
"name": "id",
"required": true
},
{
"name": "lastName",
"required": true
},
{
"name": "role",
"required": true
}
]
}
}
}

like body option in php what is used in java?

The body option is used to control the body of an entity enclosing request (e.g., PUT, POST, PATCH). in PHP it is used like this
$client->request('POST', '/post', ['body' => $stream]);
But I want to use in java like http://crawler.ankiti.com having json
{
"data": [
{
"url": "http:\/\/crawler.ankiti.com\/?module=server&do=test",
"method": "post",
"options": {
"body": "post body test 1"
},
"target": "wo"
},
{
"url": "http:\/\/crawler.ankiti.com\/?module=server&do=test",
"method": "post",
"target": "wo",
"options": {
"body": "post body test 1",
"form_params": {
"name": "abhay",
"surname": "aggarwal"
},
"headers": {
"login": "abhay",
"password": "patparganj405"
}
}
},
{
"url": "http:\/\/crawler.ankiti.com\/?module=server&do=test",
"method": "post",
"target": "wo",
"options": {
"multipart": [
{
"name": "file text",
"path": "\/test\/file.txt"
},
{
"name": "file image",
"path": "\/test\/file.jpg"
}
]
}
}
],
"errors": "",
"meta": ""
}
I want to post a string in a body request which is present in json of this url.
You can use httpServlet and follow a procedure like this.
response.setContentType("application/json");
// Get the printwriter object from response to write the required json object to the output stream
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object
out.print(jsonObject);
out.flush();
Hope this helps

It's possible to check if an user has an active/valid session through REST API in Keycloak?

It's possible to check if an user have an active/valid session through REST API?
I'm using the Java REST Admin client. I saw the UserSessionRepresentation returned by
List<UserSessionRepresentation> usr = Keycloak.realm("realmId").users().get("userId").getUserSessions();
But UserSessionRepresentation do not have the information I want.
It's possible?
you can check your session with this address:
http://keycloakAddressAndPort/auth/realms/develop/protocol/openid-connect/userinfo
if your session is not ok you see this response (http status code 401):
{
"error": "invalid_request",
"error_description": "User session not found or doesn't have client attached on it"
}
and if that is ok you see something like this (http status code 200):
{
"sub": "c0c25095-63e7-471d-a39e-f3b157c91fd7",
"email_verified": true,
"name": "Amir Azizkhani",
"preferred_username": "a.azizkhani#...",
"given_name": "Amir",
"family_name": "Azizkhani",
"email": "a.azizkhani#...."
}
complete postman json:
{
"info": {
"_postman_id": "77ce65c3-948a-4b3d-a97b-b11cd00c593b",
"name": "Spring Keycloak",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "openid-connect/userinfo",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "eyJhbGciOiJSUzI1NiIsInR5cC...",
"type": "string"
}
]
},
"method": "GET",
"header": [
{
"key": "accept",
"value": "application/json"
}
],
"url": {
"raw": "http://192.168.131.33:8080/auth/realms/develop/protocol/openid-connect/userinfo",
"protocol": "http",
"host": [
"192",
"168",
"131",
"33"
],
"port": "8080",
"path": [
"auth",
"realms",
"develop",
"protocol",
"openid-connect",
"userinfo"
]
},
"description": "Dont forget to set Bearer value obtains from token request"
},
"response": []
}
]
}
I think you want that one to get the list of the active client sessions:
GET /admin/realms/{realm}/client-session-stats
Description
The key is the client id, the (returned) value is the number of sessions that
currently are active with that client. Only clients that actually have
a session associated with them will be in this map.
The specific API endpoint that you've referenced here (interface, implementation) only returns active/valid sessions for that user. You could match on session id (or a variety of other things). If a session is invalid, it is marked for removal and not returned for that endpoint. So that seems like what you want.
But also realize that there are other sessions (like offline-sessions) if you're looking for that. Check the linked source for more information or other endpoints if desired.

Modifying katharsis json response

I am using katharsis with spring boot.
I want to change the 'id' key in json response with my table PK column name, following is the katharsis response
{
"type": "table-name",
"id": "A",
"attributes": {
"description": "AAA"
},
"relationships": {
},
"links": {
"self": "http://localhost/table-name/A"
}
}
I want to change the above "id": "A" with "coulmnName":"A".
Answer on katharsis github repository
https://github.com/katharsis-project/katharsis-core/issues/203

Categories

Resources