deleting all images from http response's entity in java - java

I have http body in which I want to remove all the image tags. For example from a response which has the entity:
StringEntity entity = new StringEntity(
"<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<meta charset=\"UTF-8\">\n" +
"<title>Title of the document</title>\n" +
"</head>\n" +
"<body>\n"
+ "Content of the document..\n" +
"<img src=\"image\">\n" +
"</body>\n" +
"</html>");
I tried the following but it didn't change:
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
responseString.replaceAll("<img .*?>","");

Related

Spring cloud contract: using URL as response body parameter

In spring cloud contract (groovy) file I have issues with extracting a segment of request URL to use it as a parameter of in response body, e.g.
package contracts
import org.springframework.cloud.contract.spec.Contract
Contract.make {
description "Should return OK "
request {
url "/discovery.svc/something(\'${value(consumer(regex(".*")),producer('defaultSomething'))}\')"
method GET()
}
response {
status 200
body :
value(
consumer(file("response/defaultSomething.xml").file.write(
String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<entry xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\" xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" xmlns=\"http://www.w3.org/2005/Atom\" metadata:context=\"http://localhost:8082/discovery.svc/\$metadata#Environment/ContentServiceCapability\" xml:base=\"http://localhost:8082/discovery.svc\">\n" +
" <id>http://localhost:8082/discovery.svc/something('%1\$s')</id>\n" +
" <title></title>\n" +
" <summary></summary>\n" +
" <updated>2020-03-23T15:19:53.272668900Z</updated>\n" +
" <author>\n" +
" <name>SDL OData v4 framework</name>\n" +
" </author>\n" +
" <link rel=\"edit\" title=\"ContentServiceCapability\" href=\"something('%1\$s')\"></link>\n" +
" <link rel=\"http://docs.oasis-open.org/odata/ns/related/Environment\" type=\"application/atom+xml;type=entry\" title=\"Environment\" href=\"something('%1\$s')/Environment\"></link>\n" +
" <link rel=\"http://docs.oasis-open.org/odata/ns/relatedlinks/Environment\" type=\"application/xml\" title=\"Environment\" href=\"something('%1\$s')/Environment/\$ref\"></link>\n" +
" <category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\" term=\"#Tridion.WebDelivery.Platform.ContentServiceCapability\"></category>\n" +
" <content type=\"application/xml\">\n" +
" <metadata:properties>\n" +
" <data:id>%1\$s</data:id>\n" +
" <data:LastUpdateTime metadata:type=\"Int64\">1580489088713</data:LastUpdateTime>\n" +
" <data:URI>http://localhost:8081/content.svc</data:URI>\n" +
" <data:ExtensionProperties metadata:type=\"#Collection(Tridion.WebDelivery.Platform.ContentKeyValuePair)\"></data:ExtensionProperties>\n" +
" </metadata:properties>\n" +
" </content>\n" +
"</entry>", fromRequest().url().split('/')[-1].split('\'')[-2]))),
producer(file("response/defaultSomething.xml")))
)
}
}
the problen is that fromRequest().url() doesn't seem to be working and when I try to insert
print fromRequest().url()
in response section (in order to debug) I get as a result:
DslProperty{
clientValue={{{request.url}}},
serverValue={{{request.url}}}}
instead of plain string URL. .toString() doesn't help neither. Do you have any ideas how can I get request.url as a plain string?

GraphQL api consuming with spring boot Resttemplate resulting in {"errors":[{"message":"No query string was present"}]} always

Currently we want to consume a graphQL endpoint in a springboot application using resttemplate
However, when we make a POST request with the below query we are always receiving the same error {"errors":[{"message":"No query string was present"}]}
Below is the snippet, we want to run,
#Test
public void testSwoop(){
RestTemplate restTemplate = restTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer *************");
headers.add("content-type", "application/graphql");
String query1 = "{\n" +
" \"query\": query {\n" +
" \"locationTypes\": {\n" +
" \"edges\": \n" +
" {\n" +
" \"node\": \n" +
" {\n" +
" \"name\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
String URL = "https://staging.joinswoop.com/graphql";
ResponseEntity<String> response = restTemplate.postForEntity(URL, new HttpEntity<>(query1, headers), String.class);
System.out.println("The response================="+response);
}
However from Postman, we dont have any issue in consuming the endpoint, and we get response just fine
Can someone please help us in directing us to the correct resource
you set the content type header to "application/graphql", but yo are sending a JSON as data.
Two solutions that might work:
Sending JSON:
Set the content type to "application/json" and send a JSON formatted query:
#Test
public void testSwoop(){
RestTemplate restTemplate = restTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer *************");
headers.add("content-type", "application/json"); // just modified graphql into json
String query1 = "{\n" +
" \"query\": query {\n" +
" \"locationTypes\": {\n" +
" \"edges\": \n" +
" {\n" +
" \"node\": \n" +
" {\n" +
" \"name\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
String URL = "https://staging.joinswoop.com/graphql";
ResponseEntity<String> response = restTemplate.postForEntity(URL, new HttpEntity<>(query1, headers), String.class);
System.out.println("The response================="+response);
}
Sending GraphQL Query:
If your server support this (it should), set the content type to "application/graphql", and send a real graphql query as a string.
#Test
public void testSwoop(){
RestTemplate restTemplate = restTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer *************");
headers.add("content-type", "application/graphql"); // maintain graphql
// query is a grapql query wrapped into a String
String query1 = "{\n" +
" locationTypes: {\n" +
" edges: \n" +
" {\n" +
" node: \n" +
" {\n" +
" name\n" +
" }\n" +
" }\n" +
" }\n" +
" }";
String URL = "https://staging.joinswoop.com/graphql";
ResponseEntity<String> response = restTemplate.postForEntity(URL, new HttpEntity<>(query1, headers), String.class);
System.out.println("The response================="+response);
}

Android - Added image in html webview but image doesn't show

My Code:
web.loadData("<html><body> " + result.getHtmltext().replaceAll("<span class=\"stl_23 stl_10\" style=\"word-spacing:0em;\">-------------------------------</span>",
"<img height=\" 500\" width=\"500\" src=" + " \" " + "file:///android_asset/logo_transparent.png" + "\" " + "/>") + "</body></html>", "text/html; charset=UTF-8", null);
But doesn't work:
You can give it a try
String htmlData = "<body>" + "<img src=\"logo_transparent.png\"/></body>";
webView.loadDataWithBaseURL("file:///android_asset/",htmlData , "text/html", "utf-8",null);
with this, it will start picking up images from the assets folder directly.

Using javamail how do I send integer value 0

I have a utility which creates daily reports as Excel sheets containing the file processed details the failed ones and then sends out a mail to users and this code is written in Java.
When the value of the failed files is 0 the mail has a blank sent instead of 0.
This is the sample code
//sample piece of code
String bodyText = "<html>"
+ "<body style =\"font-family: Calibri; font-size:11pt; background-color:white \">"
+ "<table style =\"border:1px solid black;background-color:#DBE5F1;width:100%\">"
+ "<tr style = \"background-color:#DBE5F1 ;text-align-left; font-size:11pt\">"
+ "<br>"
+ "&nbsp Hi All,"
+ "</br>"
+ "<br>"
+ statusLine
+ "</br>"
+ " "
+"<br>"
+ "</tr>"
+ "<td style = \"text-align:right;width:10%;border-right:1px solid black\">"
+ caseCreated
+ "</td>"
+ "<td style = \"width:10%;border-right:1px solid black;text-align:right\">"
+ Failure
+ "</td>"
+"</html>";
//and for composing the mail message
message.setContent(bodyText, "text/html");

Drawing piechart in HTML email using Apache Commons Email

I would want to send statistical information to my clients showing the number of transactions processed on every terminal or branch. I am using Apache Commons Email to send HTML emails.
I would like to send a pie-chart data like this one on the site.
My java code is basic extracted from.
It goes like:
public void testHtmlEmailPiechart()
throws UnsupportedEncodingException, EmailException, MalformedURLException {
HtmlEmail email = new HtmlEmail();
email.setHostName(emailServer);
email.setSmtpPort(587);
email.setSSLOnConnect(true);
email.setAuthentication(userName, password);
email.setCharset(emailEncoding);
email.addTo(receiver, "Mwesigye John Bosco");
email.setFrom(userName, "Enovate system emailing alert");
email.setSubject("Conkev aml Engine Statistics");
URL url = new URL("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcROXe8tn1ljtctM53TkLJhLs6gEX56CvL0shvyq1V6wg7tXUDH8KRyVP30");
// URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid2 = email.embed(url, "logo.gif");
email.setHtmlMsg("<html>\n" +
" <head>\n" +
" <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n" +
" <script type=\"text/javascript\">\n" +
" google.charts.load(\"current\", {packages:[\"corechart\"]});\n" +
" google.charts.setOnLoadCallback(drawChart);\n" +
" function drawChart() {\n" +
" var data = google.visualization.arrayToDataTable([\n" +
" ['Task', 'Hours per Day'],\n" +
" ['Work', 11],\n" +
" ['Eat', 2],\n" +
" ['Commute', 2],\n" +
" ['Watch TV', 2],\n" +
" ['Sleep', 7]\n" +
" ]);\n" +
"\n" +
" var options = {\n" +
" title: 'My Daily Activities',\n" +
" is3D: true,\n" +
" };\n" +
"\n" +
" var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));\n" +
" chart.draw(data, options);\n" +
" }\n" +
" </script>\n" +
" </head>\n" +
" <body>\n" +
" <div id=\"piechart_3d\" style=\"width: 900px; height: 500px;\">Piechart Data</div>\n" +
" </body>\n" +
"</html>");
email.setTextMsg("Your email client does not support HTML messages");
email.send();
}
My guess is that the JavaScript is not recognized because the code works like sending images,styling fonts and I have sent to my email address some sample mail. I would like your help or recommendation of any material I can read to achieve this as long as am using Java.The processes is automated running in the background so no user interface is involved.
Thanks.

Categories

Resources