Sample Code: Install Device Image



public static void installDeviceImage() {
	int responseCode = 0;
	String responseString = null;
	String cType = "application/vnd.net.juniper.space.software-management.package+xml;version=1";

	Credentials credentials = new UsernamePasswordCredentials("super", "juniper123");
	// Setup Credentials
	HttpClient httpClient = new HttpClient(); // Setup HTTPClient
	httpClient.getState().setCredentials(AuthScope.ANY, credentials);
	httpClient.getParams().setAuthenticationPreemptive(true);

	String requestURL = "http://space:8080/api/space/software-management/packages";

	// File Name on the client
	String packageName = "junos-srxsme-10.4R1.9-domestic.tgz";
	// File Location on the Client side. This location points to the client's local directory.
	String packagePath = "C:/MyJunosSwPackageRepository/" + packageName;
	File f = new File(packagePath);

	PostMethod filePost = new PostMethod(requestURL);
	filePost.addRequestHeader("Accept", cType);

	try {
		// fileData and fileName are the reserved words for this SPACE operation.
		Part[] parts = { new FilePart("fileData", f), new StringPart("fileName", packageName) };
		filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
		responseCode = httpClient.executeMethod(filePost);
		responseString = filePost.getResponseBodyAsString();
		filePost.releaseConnection();
		System.out.println("addDeviceImage: responseCode: " + responseCode);
		System.out.println("addDeviceImage: responseString: " + responseString);
	}
	catch (Exception e) {
		e.printStackTrace();
	}
}