December 10, 2020
Estimated Post Reading Time ~

cURL execution from Java program

cURL is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNETand TFTP). The command is designed to work without user interaction.

With AEM, you can use cURL commands to modify repository, download json, access OSGi information etc.

Snippet to download a json file via cURL:
While majority of the AEM’s content information can be downloaded via HTTP connection, cURL command can be used to download information from AEM’s OSGi console.

In the shared example, we have used cURL to get information about all the bundles installed in AEM.

package blog.techrevel.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.codec.binary.Base64;

public class CurlExecutor {

public static void main(String[] args) {

String stringUrl = "http://localhost:4502/system/console/bundles.json";
URL url;
try {
url = new URL(stringUrl);
URLConnection uc;
uc = url.openConnection();

uc.setRequestProperty("X-Requested-With", "Curl");

String userpass = "admin" + ":" + "admin";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);

BufferedReader reader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));

}
String result = builder.toString();
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Snippet to execute a cURL command:
The following snippet contains acURL command that has been converted in a format to be used in Java snippet.

The cURL command used here is meant to lock page “/content/geometrixx/en/toolbar/contacts” :

curl -u admin:admin -X POST -F cmd=”lockPage” -F path=”/content/geometrixx/en/toolbar/contacts” -F “_charset_”=”utf-8” http://localhost:4502/bin/wcmcommand

package blog.techrevel.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CurlExecutor2 {

public static void main(String[] args) {

String username = "admin";
String password = "admin";
String url = "http://localhost:4502/bin/wcmcommand";
String path = "/content/geometrixx/en/toolbar/contacts";

//cURL Command: curl -u admin:admin -X POST -F cmd="lockPage" -F path="/content/geometrixx/en/toolbar/contacts" -F "_charset_"="utf-8" http://localhost:4502/bin/wcmcommand

//Equivalent command conversion for Java execution
String[] command = { "curl", "-u", username + ":" + password, "-X", "POST", "-F", "cmd=unlockPage", "-F",
"path=" + path, "-F", "_charset_=utf-8", url };

ProcessBuilder process = new ProcessBuilder(command);
Process p;
try {
p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.print(result);

} catch (IOException e) {
System.out.print("error");
e.printStackTrace();
}
}
}

For more samples of cURL commands, please refer to:


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.