forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Azure#21 from loudej/b1336ad5e8eecb5a0930546341792…
…6c47c3198fe New pull request - initial code review for starting point
- Loading branch information
Showing
60 changed files
with
3,599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.classpath | ||
.project | ||
.settings | ||
target | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.microsoft.azure</groupId> | ||
<artifactId>microsoft-azure-api</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>Microsoft Azure Client API</name> | ||
<description>API for Microsoft Azure Clients</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-client</artifactId> | ||
<version>1.10-b02</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.xml.bind</groupId> | ||
<artifactId>jaxb-api</artifactId> | ||
<version>2.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.8</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<scope>test</scope> | ||
<version>1.9.0-rc1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.inject</groupId> | ||
<artifactId>javax.inject</artifactId> | ||
<version>1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-json</artifactId> | ||
<version>1.10-b02</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jvnet.jaxb2.maven2</groupId> | ||
<artifactId>maven-jaxb2-plugin</artifactId> | ||
<version>0.8.0</version> | ||
<executions> | ||
<execution> | ||
<phase>generate-sources</phase> | ||
<goals> | ||
<goal>generate</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<extension>true</extension> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jvnet.jaxb2_commons</groupId> | ||
<artifactId>jaxb2-basics</artifactId> | ||
<version>0.6.0</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.jvnet.jaxb2_commons</groupId> | ||
<artifactId>jaxb2-basics-annotate</artifactId> | ||
<version>0.6.0</version> | ||
</plugin> | ||
</plugins> | ||
|
||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
85 changes: 85 additions & 0 deletions
85
microsoft-azure-api/src/main/java/com/microsoft/azure/ServiceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.microsoft.azure; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
public class ServiceException extends Exception { | ||
|
||
private static final long serialVersionUID = -4942076377009150131L; | ||
|
||
int httpStatusCode; | ||
String httpReasonPhrase; | ||
String serviceName; | ||
|
||
String errorCode; | ||
String errorMessage; | ||
Map<String, String> errorValues; | ||
|
||
public ServiceException() { | ||
init(); | ||
} | ||
|
||
public ServiceException(String message) { | ||
super(message); | ||
init(); | ||
} | ||
|
||
public ServiceException(String message, Throwable cause) { | ||
super(message, cause); | ||
init(); | ||
} | ||
|
||
public ServiceException(Throwable cause) { | ||
super(cause); | ||
init(); | ||
} | ||
|
||
private void init() { | ||
errorValues = new HashMap<String,String>(); | ||
} | ||
|
||
|
||
public int getHttpStatusCode() { | ||
return httpStatusCode; | ||
} | ||
public void setHttpStatusCode(int httpStatusCode) { | ||
this.httpStatusCode = httpStatusCode; | ||
} | ||
public String getHttpReasonPhrase() { | ||
return httpReasonPhrase; | ||
} | ||
public void setHttpReasonPhrase(String httpReasonPhrase) { | ||
this.httpReasonPhrase = httpReasonPhrase; | ||
} | ||
public String getErrorCode() { | ||
return errorCode; | ||
} | ||
public void setErrorCode(String errorCode) { | ||
this.errorCode = errorCode; | ||
} | ||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
public void setErrorMessage(String errorMessage) { | ||
this.errorMessage = errorMessage; | ||
} | ||
public Map<String, String> getErrorValues() { | ||
return errorValues; | ||
} | ||
public void setErrorValues(Map<String, String> errorValues) { | ||
this.errorValues = errorValues; | ||
} | ||
public String getErrorValue(String name) { | ||
return errorValues.get(name); | ||
} | ||
public void setErrorValue(String name, String value) { | ||
this.errorValues.put(name, value); | ||
} | ||
public String getServiceName() { | ||
return serviceName; | ||
} | ||
public void setServiceName(String serviceName) { | ||
this.serviceName = serviceName; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
microsoft-azure-api/src/main/java/com/microsoft/azure/auth/wrap/Exports.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.microsoft.azure.auth.wrap; | ||
|
||
import com.microsoft.azure.auth.wrap.contract.WrapContract; | ||
import com.microsoft.azure.auth.wrap.contract.WrapContractImpl; | ||
import com.microsoft.azure.configuration.builder.Builder.Registry; | ||
|
||
public class Exports implements | ||
com.microsoft.azure.configuration.builder.Builder.Exports { | ||
|
||
public void register(Registry registry) { | ||
registry.add(WrapContract.class, WrapContractImpl.class); | ||
registry.add(WrapClient.class); | ||
registry.add(WrapFilter.class); | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
microsoft-azure-api/src/main/java/com/microsoft/azure/auth/wrap/WrapClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.microsoft.azure.auth.wrap; | ||
|
||
import java.util.Date; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.management.timer.Timer; | ||
|
||
import com.microsoft.azure.ServiceException; | ||
import com.microsoft.azure.auth.wrap.contract.WrapContract; | ||
import com.microsoft.azure.auth.wrap.contract.WrapResponse; | ||
import com.microsoft.azure.utils.DateFactory; | ||
|
||
public class WrapClient { | ||
|
||
WrapContract contract; | ||
private DateFactory dateFactory; | ||
private String uri; | ||
private String name; | ||
private String password; | ||
private String scope; | ||
|
||
private ActiveToken activeToken; | ||
|
||
|
||
@Inject | ||
public WrapClient( | ||
WrapContract contract, | ||
DateFactory dateFactory, | ||
@Named("wrap.uri") String uri, | ||
@Named("wrap.scope") String scope, | ||
@Named("wrap.name") String name, | ||
@Named("wrap.password") String password) { | ||
this.contract = contract; | ||
this.dateFactory = dateFactory; | ||
this.uri = uri; | ||
this.scope = scope; | ||
this.name = name; | ||
this.password = password; | ||
} | ||
|
||
|
||
/** | ||
* @return the contract | ||
*/ | ||
public WrapContract getContract() { | ||
return contract; | ||
} | ||
|
||
/** | ||
* @param contract the contract to set | ||
*/ | ||
public void setContract(WrapContract contract) { | ||
this.contract = contract; | ||
} | ||
|
||
public String getAccessToken() throws ServiceException { | ||
Date now = dateFactory.getDate(); | ||
ActiveToken active = this.activeToken; | ||
|
||
if (active != null && now.before(active.getExpiresUtc()) ) { | ||
return active.getWrapResponse().getAccessToken(); | ||
} | ||
|
||
WrapResponse wrapResponse = getContract().post(uri, name, password, scope); | ||
Date expiresUtc = new Date(now.getTime() + wrapResponse.getExpiresIn() * Timer.ONE_SECOND / 2); | ||
|
||
ActiveToken acquired = new ActiveToken(); | ||
acquired.setWrapResponse(wrapResponse); | ||
acquired.setExpiresUtc(expiresUtc); | ||
this.activeToken = acquired; | ||
|
||
return wrapResponse.getAccessToken(); | ||
} | ||
|
||
class ActiveToken { | ||
Date expiresUtc; | ||
WrapResponse wrapResponse; | ||
/** | ||
* @return the expiresUtc | ||
*/ | ||
public Date getExpiresUtc() { | ||
return expiresUtc; | ||
} | ||
/** | ||
* @param expiresUtc the expiresUtc to set | ||
*/ | ||
public void setExpiresUtc(Date expiresUtc) { | ||
this.expiresUtc = expiresUtc; | ||
} | ||
/** | ||
* @return the wrapResponse | ||
*/ | ||
public WrapResponse getWrapResponse() { | ||
return wrapResponse; | ||
} | ||
/** | ||
* @param wrapResponse the wrapResponse to set | ||
*/ | ||
public void setWrapResponse(WrapResponse wrapResponse) { | ||
this.wrapResponse = wrapResponse; | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
microsoft-azure-api/src/main/java/com/microsoft/azure/auth/wrap/WrapFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.microsoft.azure.auth.wrap; | ||
|
||
import com.microsoft.azure.ServiceException; | ||
import com.sun.jersey.api.client.ClientHandlerException; | ||
import com.sun.jersey.api.client.ClientRequest; | ||
import com.sun.jersey.api.client.ClientResponse; | ||
import com.sun.jersey.api.client.filter.ClientFilter; | ||
|
||
public class WrapFilter extends ClientFilter { | ||
private WrapClient client; | ||
|
||
public WrapFilter(WrapClient client) { | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public ClientResponse handle(ClientRequest cr) | ||
throws ClientHandlerException { | ||
|
||
String accessToken; | ||
try { | ||
accessToken = client.getAccessToken(); | ||
} catch (ServiceException e) { | ||
// must wrap exception because of base class signature | ||
throw new ClientHandlerException(e); | ||
} | ||
|
||
cr.getHeaders().add("Authorization", | ||
"WRAP access_token=\"" + accessToken + "\""); | ||
|
||
return this.getNext().handle(cr); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
microsoft-azure-api/src/main/java/com/microsoft/azure/auth/wrap/contract/WrapContract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.microsoft.azure.auth.wrap.contract; | ||
|
||
import com.microsoft.azure.ServiceException; | ||
|
||
public interface WrapContract { | ||
WrapResponse post(String uri, String name, String password, String scope) throws ServiceException; | ||
} |
52 changes: 52 additions & 0 deletions
52
...soft-azure-api/src/main/java/com/microsoft/azure/auth/wrap/contract/WrapContractImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.microsoft.azure.auth.wrap.contract; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import com.microsoft.azure.ServiceException; | ||
import com.microsoft.azure.utils.ServiceExceptionFactory; | ||
import com.sun.jersey.api.client.Client; | ||
import com.sun.jersey.api.client.UniformInterfaceException; | ||
import com.sun.jersey.api.representation.Form; | ||
|
||
public class WrapContractImpl implements WrapContract { | ||
Client channel; | ||
|
||
static Log log = LogFactory.getLog(WrapContract.class); | ||
|
||
@Inject | ||
public WrapContractImpl(Client channel) { | ||
this.channel = channel; | ||
} | ||
|
||
public WrapResponse post(String uri, String name, String password, String scope) throws ServiceException { | ||
Form requestForm = new Form(); | ||
requestForm.add("wrap_name", name); | ||
requestForm.add("wrap_password", password); | ||
requestForm.add("wrap_scope", scope); | ||
|
||
Form responseForm; | ||
try { | ||
responseForm = channel.resource(uri) | ||
.accept(MediaType.APPLICATION_FORM_URLENCODED) | ||
.type(MediaType.APPLICATION_FORM_URLENCODED) | ||
.post(Form.class, requestForm); | ||
} | ||
catch (UniformInterfaceException e) { | ||
log.warn("WRAP server returned error acquiring access_token", e); | ||
throw ServiceExceptionFactory.process("WRAP", new ServiceException("WRAP server returned error acquiring access_token", e)); | ||
} | ||
|
||
WrapResponse response = new WrapResponse(); | ||
|
||
response.setAccessToken(responseForm.getFirst("wrap_access_token")); | ||
|
||
String expiresIn = responseForm.getFirst("wrap_access_token_expires_in"); | ||
response.setExpiresIn(Long.parseLong(expiresIn)); | ||
|
||
return response; | ||
} | ||
} |
Oops, something went wrong.