Skip to content

Commit

Permalink
Merge pull request Azure#21 from loudej/b1336ad5e8eecb5a0930546341792…
Browse files Browse the repository at this point in the history
…6c47c3198fe

New pull request - initial code review for starting point
  • Loading branch information
rpaquay committed Nov 4, 2011
2 parents dc09ae2 + b1336ad commit 06f9ad1
Show file tree
Hide file tree
Showing 60 changed files with 3,599 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.classpath
.project
.settings
target
node_modules
83 changes: 83 additions & 0 deletions microsoft-azure-api/pom.xml
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>
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;
}

}
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);
}

}
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;
}
}

}
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);
}
}
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;
}
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;
}
}
Loading

0 comments on commit 06f9ad1

Please sign in to comment.