Loading .gitignore 0 → 100644 +9 −0 Original line number Diff line number Diff line # Maven target pom.xml.versionsBackup # Eclipse .project .classpath .settings pom.xml 0 → 100644 +93 −0 Original line number Diff line number Diff line <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.inteligr8</groupId> <artifactId>bitbucket-api</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>BitBucket API & Utilities</name> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.12</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludes> <exclude>**/*IT.class</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <id>integration-tests</id> <phase>integration-test</phase> <goals> <goal>integration-test</goal> </goals> <configuration> <includes> <include>**/*IT.class</include> </includes> </configuration> </execution> </executions> </plugin> </plugins> </build> <distributionManagement> <repository> <id>inteligr8-releases</id> <name>Inteligr8 Releases</name> <url>http://repos.yateslong.us/nexus/repository/inteligr8-public</url> <layout>default</layout> </repository> </distributionManagement> </project> src/main/java/com/inteligr8/bitbucket/ApiGateway.java 0 → 100644 +104 −0 Original line number Diff line number Diff line package com.inteligr8.bitbucket; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpResponse; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; import com.inteligr8.bitbucket.http.BaseResponse; import com.inteligr8.bitbucket.http.PreemptiveAuthInterceptor; public class ApiGateway { private final Logger logger = LoggerFactory.getLogger(ApiGateway.class); private final String baseUrl = "https://api.bitbucket.org/2.0"; private ObjectMapper omapper = new ObjectMapper(); private CredentialsProvider credProvider; public ApiGateway(CredentialsProvider credProvider) { this.credProvider = credProvider; } public <Response extends BaseResponse> Response get(String uriPath, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { return this.execute(HttpGet.METHOD_NAME, uriPath, paramMap, null, responseType); } public <Request, Response extends BaseResponse> Response post(String uriPath, Request requestObject, Class<Response> responseType) throws IOException { return this.execute(HttpPost.METHOD_NAME, uriPath, null, requestObject, responseType); } public <Request, Response extends BaseResponse> Response put(String uriPath, Request requestObject, Class<Response> responseType) throws IOException { return this.execute(HttpPost.METHOD_NAME, uriPath, null, requestObject, responseType); } public <Response extends BaseResponse> Response delete(String uriPath, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { return this.execute(HttpDelete.METHOD_NAME, uriPath, paramMap, null, responseType); } private <Request, Response extends BaseResponse> Response execute(String method, String uriPath, Map<String, Object> paramMap, Request requestObject, Class<Response> responseType) throws IOException { if (this.logger.isTraceEnabled()) this.logger.trace("execute('" + method + "', '" + uriPath + "')"); RequestBuilder builder = RequestBuilder .create(method) .setUri(this.baseUrl + uriPath); if (paramMap != null) { for (Entry<String, Object> param : paramMap.entrySet()) if (param.getValue() != null) builder.addParameter(param.getKey(), param.getValue().toString()); } if (requestObject != null) { String requestJson = this.omapper.writeValueAsString(requestObject); if (this.logger.isTraceEnabled()) this.logger.trace("execute('" + method + "', '" + uriPath + "'): " + requestJson); builder.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON)); } HttpUriRequest request = builder.build(); if (this.logger.isDebugEnabled()) this.logger.debug("Prepared request for " + method + " to: " + uriPath); HttpResponse response = HttpClientBuilder .create() .addInterceptorFirst(new PreemptiveAuthInterceptor()) .setDefaultCredentialsProvider(this.credProvider) .build() .execute(request); if (this.logger.isDebugEnabled()) this.logger.debug("Received response from " + method + ": " + response.getStatusLine().getStatusCode()); Response responseObject = null; if (response.getEntity() != null) { InputStream istream = response.getEntity().getContent(); try { responseObject = this.omapper.readerFor(responseType).readValue(istream); } finally { istream.close(); } } else { responseObject = this.omapper.readerFor(responseType).readValue("{}"); } responseObject.setHttpStatusCode(response.getStatusLine().getStatusCode()); responseObject.setHttpStatusReason(response.getStatusLine().getReasonPhrase()); return responseObject; } } src/main/java/com/inteligr8/bitbucket/http/BaseResponse.java 0 → 100644 +49 −0 Original line number Diff line number Diff line package com.inteligr8.bitbucket.http; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.inteligr8.bitbucket.model.Error; @JsonIgnoreProperties(ignoreUnknown = true) public class BaseResponse { private int httpStatusCode; private String httpStatusReason; private String type; private Error error; public int getHttpStatusCode() { return this.httpStatusCode; } public void setHttpStatusCode(int httpStatusCode) { this.httpStatusCode = httpStatusCode; } public String getHttpStatusReason() { return this.httpStatusReason; } public void setHttpStatusReason(String httpStatusReason) { this.httpStatusReason = httpStatusReason; } public String getType() { return this.type; } public void setType(String type) { this.type = type; } public Error getError() { if (this.error == null) this.error = new Error(); return this.error; } @JsonIgnoreProperties(ignoreUnknown = true) public class Data { } } src/main/java/com/inteligr8/bitbucket/http/PreemptiveAuthInterceptor.java 0 → 100644 +37 −0 Original line number Diff line number Diff line package com.inteligr8.bitbucket.http; import java.io.IOException; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthState; import org.apache.http.auth.Credentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpCoreContext; public class PreemptiveAuthInterceptor implements HttpRequestInterceptor { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); // If no auth scheme available yet, try to initialize it // preemptively if (authState.getAuthScheme() == null) { CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds == null) { throw new HttpException("No credentials for preemptive authentication"); } authState.update(new BasicScheme(), creds); } } } Loading
.gitignore 0 → 100644 +9 −0 Original line number Diff line number Diff line # Maven target pom.xml.versionsBackup # Eclipse .project .classpath .settings
pom.xml 0 → 100644 +93 −0 Original line number Diff line number Diff line <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.inteligr8</groupId> <artifactId>bitbucket-api</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>BitBucket API & Utilities</name> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.12</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludes> <exclude>**/*IT.class</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <id>integration-tests</id> <phase>integration-test</phase> <goals> <goal>integration-test</goal> </goals> <configuration> <includes> <include>**/*IT.class</include> </includes> </configuration> </execution> </executions> </plugin> </plugins> </build> <distributionManagement> <repository> <id>inteligr8-releases</id> <name>Inteligr8 Releases</name> <url>http://repos.yateslong.us/nexus/repository/inteligr8-public</url> <layout>default</layout> </repository> </distributionManagement> </project>
src/main/java/com/inteligr8/bitbucket/ApiGateway.java 0 → 100644 +104 −0 Original line number Diff line number Diff line package com.inteligr8.bitbucket; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpResponse; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; import com.inteligr8.bitbucket.http.BaseResponse; import com.inteligr8.bitbucket.http.PreemptiveAuthInterceptor; public class ApiGateway { private final Logger logger = LoggerFactory.getLogger(ApiGateway.class); private final String baseUrl = "https://api.bitbucket.org/2.0"; private ObjectMapper omapper = new ObjectMapper(); private CredentialsProvider credProvider; public ApiGateway(CredentialsProvider credProvider) { this.credProvider = credProvider; } public <Response extends BaseResponse> Response get(String uriPath, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { return this.execute(HttpGet.METHOD_NAME, uriPath, paramMap, null, responseType); } public <Request, Response extends BaseResponse> Response post(String uriPath, Request requestObject, Class<Response> responseType) throws IOException { return this.execute(HttpPost.METHOD_NAME, uriPath, null, requestObject, responseType); } public <Request, Response extends BaseResponse> Response put(String uriPath, Request requestObject, Class<Response> responseType) throws IOException { return this.execute(HttpPost.METHOD_NAME, uriPath, null, requestObject, responseType); } public <Response extends BaseResponse> Response delete(String uriPath, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { return this.execute(HttpDelete.METHOD_NAME, uriPath, paramMap, null, responseType); } private <Request, Response extends BaseResponse> Response execute(String method, String uriPath, Map<String, Object> paramMap, Request requestObject, Class<Response> responseType) throws IOException { if (this.logger.isTraceEnabled()) this.logger.trace("execute('" + method + "', '" + uriPath + "')"); RequestBuilder builder = RequestBuilder .create(method) .setUri(this.baseUrl + uriPath); if (paramMap != null) { for (Entry<String, Object> param : paramMap.entrySet()) if (param.getValue() != null) builder.addParameter(param.getKey(), param.getValue().toString()); } if (requestObject != null) { String requestJson = this.omapper.writeValueAsString(requestObject); if (this.logger.isTraceEnabled()) this.logger.trace("execute('" + method + "', '" + uriPath + "'): " + requestJson); builder.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON)); } HttpUriRequest request = builder.build(); if (this.logger.isDebugEnabled()) this.logger.debug("Prepared request for " + method + " to: " + uriPath); HttpResponse response = HttpClientBuilder .create() .addInterceptorFirst(new PreemptiveAuthInterceptor()) .setDefaultCredentialsProvider(this.credProvider) .build() .execute(request); if (this.logger.isDebugEnabled()) this.logger.debug("Received response from " + method + ": " + response.getStatusLine().getStatusCode()); Response responseObject = null; if (response.getEntity() != null) { InputStream istream = response.getEntity().getContent(); try { responseObject = this.omapper.readerFor(responseType).readValue(istream); } finally { istream.close(); } } else { responseObject = this.omapper.readerFor(responseType).readValue("{}"); } responseObject.setHttpStatusCode(response.getStatusLine().getStatusCode()); responseObject.setHttpStatusReason(response.getStatusLine().getReasonPhrase()); return responseObject; } }
src/main/java/com/inteligr8/bitbucket/http/BaseResponse.java 0 → 100644 +49 −0 Original line number Diff line number Diff line package com.inteligr8.bitbucket.http; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.inteligr8.bitbucket.model.Error; @JsonIgnoreProperties(ignoreUnknown = true) public class BaseResponse { private int httpStatusCode; private String httpStatusReason; private String type; private Error error; public int getHttpStatusCode() { return this.httpStatusCode; } public void setHttpStatusCode(int httpStatusCode) { this.httpStatusCode = httpStatusCode; } public String getHttpStatusReason() { return this.httpStatusReason; } public void setHttpStatusReason(String httpStatusReason) { this.httpStatusReason = httpStatusReason; } public String getType() { return this.type; } public void setType(String type) { this.type = type; } public Error getError() { if (this.error == null) this.error = new Error(); return this.error; } @JsonIgnoreProperties(ignoreUnknown = true) public class Data { } }
src/main/java/com/inteligr8/bitbucket/http/PreemptiveAuthInterceptor.java 0 → 100644 +37 −0 Original line number Diff line number Diff line package com.inteligr8.bitbucket.http; import java.io.IOException; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthState; import org.apache.http.auth.Credentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpCoreContext; public class PreemptiveAuthInterceptor implements HttpRequestInterceptor { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); // If no auth scheme available yet, try to initialize it // preemptively if (authState.getAuthScheme() == null) { CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds == null) { throw new HttpException("No credentials for preemptive authentication"); } authState.update(new BasicScheme(), creds); } } }