I've done it through rest_v2 API but, I want to get it done through Java.
2 Answers:
Posted on March 5, 2015 at 8:57am
Have you tried the client availble on Github?
Posted on March 12, 2015 at 11:22pm
I created simplest version...
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class Restv2 {
public static void main(String[] args) {
DefaultHttpClient httpclient = new DefaultHttpClient(); //session
try {
HttpHost target = new HttpHost("localhost", 8080, "http"); // specify the host, protocol, and port
HttpGet getRequest = new HttpGet("/jasperserver/rest/login?j_username=jasperadmin&j_password=jasperadmin"); // specify the get request
HttpResponse objResponse = httpclient.execute(target, getRequest);
if(objResponse.getStatusLine().getStatusCode()==200){ //check status code in web service guide
System.out.println("Successfully logged in to JasperServer");
if(objResponse!=null)
EntityUtils.consumeQuietly(objResponse.getEntity()); //need to complete first response to send another request...
objResponse=null;
String xmlString=""; //set XML String over here.
HttpPut objHttpPut = new HttpPut("/jasperserver/rest_v2/jobs");
objHttpPut.setHeader("Content-Type", "application/xml"); //send put request after login.
objHttpPut.setEntity(new StringEntity(xmlString));
objResponse = httpclient.execute(target, objHttpPut);
System.out.println(objResponse);
BufferedReader rd = new BufferedReader(new InputStreamReader(objResponse.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
else
System.out.println("Failed to login to JasperServer");
} catch (Exception e) {
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
Thanks