Download files from bitbucket

I was asked to download a file (raw) from bitbucket repository and wrote an automation script using those files. Bitbucket exposed set of REST Web services for their repository using which we can integrate our application with bitbucket.

Steps to download files from bitbucket:

1. Need a HttpClient library , I opted for Apache CXF Client.

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.0-milestone1</version>
</dependency>

2. REST API documents . File Resource

public class GitClient {
	static String url = "https://bitbucket.org/api/1.0/repositories/myrepository/mr/raw/";
	static String branch = "master";
	static String source = "/MavenWeb/src/main/resources/";
	String test = "/src/test/resources/";
	static String user = "username";
	static String password = "password";

	public static void usingWebClient() throws FileNotFoundException,IOException {
		downloadFileFromBitBucket("provisioner-components-setup.ini");
		downloadFileFromBitBucket("provisioner-properties-setup.csv");
		downloadFileFromBitBucket("provisioner-resources-setup.zip");
		downloadFileFromBitBucket("provisioner-system-setup.ini");
	}

	private static void downloadFileFromBitBucket(String resource)
			throws IOException, FileNotFoundException {
		WebClient client = WebClient.create(url + branch + source + resource,user, password, null);
		Response r = client.accept("text/plain").get();
		System.out.println("reponse" + r.getStatus());
		IOUtils.copyLarge((InputStream) r.getEntity(), new FileOutputStream(new File("C:\\folder" + resource)));
	}

	public static void main(String args[]) throws FileNotFoundException,IOException {
		usingWebClient();
	}
} 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s