The requirement is to capture the HTML page and attach to the work page by taking the input of web URL.
In simple words, the automation of these two manual processes:
-
Hit the public URL which is not required the user login credentials (eg: www.google.com).
-
Attach the exact HTML page that is visible after hitting the URL to the pega work object.
We can achieve the requirement with the following code :
Firstly, write the Java code or create a java function to establish the URL connection and read the response and make the attach stream out of that.
java.net.URL url = null;
java.net.URLConnection connection = null;
try {
// Establish URL connection and hit the URL
String URL = tools.getParamValue("URL");
url = new java.net.URL(URL);
connection = url.openConnection();
connection.setDoOutput(true);
java.io.BufferedReader in = new java.io.BufferedReader(
new java.io.InputStreamReader(
connection.getInputStream()));
String decodedString;
String htmloutput = "";
while ((decodedString = in .readLine()) != null) {
// Customise according to requirement
// If the CSS URL path is not fully defined in the page source, then replace with full path to view the webpage exactly it looks online ..
//Do the same for all images as per the business requirements
if (decodedString.contains(".css") && !(decodedString.contains("http"))) {
int StartIndex = decodedString.indexOf("href");
int EndIndex = decodedString.indexOf(".css");
String replaceString = decodedString.substring(StartIndex + 6, EndIndex + 4);
boolean StartsWithflag = replaceString.startsWith("/");
if (StartsWithflag) {
decodedString = decodedString.replace(replaceString, URL + replaceString);
} else {
decodedString = decodedString.replace(replaceString, URL + "/" + replaceString);
}
}
// End of customization
htmloutput = htmloutput + decodedString;
}
byte[] buffer = htmloutput.getBytes();
strFileData = Base64Util.encodeToString(buffer);
} catch (java.io.IOException e) {} finally {
try {
} catch (Exception w) {}
}
Once we have the AttachStream, then create the WorkAttachFile and Link the attachment with the work object. That’s it. The html page will be attached to the work page.
Note: This code works for the repository storage type.
Modify the code according to the requirements.