Skip to content

Commit

Permalink
handle authentication in spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoo committed Dec 11, 2011
1 parent af1fa62 commit 5afa2f5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/main/java/net/linkfluence/jspore/Method.java
Expand Up @@ -42,12 +42,15 @@ public class Method {
public final ImmutableSet<String> optionalParams;
public final ImmutableMap<String, String> headers;
public final ImmutableList<String> pathVarNames;
public final boolean authentication;

protected Method(String name, String path, String httpMethod,
Iterable<Integer> expectedStatus,
Iterable<String> requiredParams,
Iterable<String> optionalParams,
Map<String, String> headers) {
Map<String, String> headers,
boolean authentication) {
this.authentication = authentication;
this.name = name;
this.path = path;
this.httpMethod = httpMethod.toUpperCase();
Expand Down Expand Up @@ -151,7 +154,9 @@ public static class Builder {
private final Set<String> requiredParams;
private final Set<String> optionalParams;
private final Map<String, String> headers;


private boolean authentication = false;

public Builder(String name, String path, String httpMethod) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
Preconditions.checkArgument(!Strings.isNullOrEmpty(path));
Expand Down Expand Up @@ -221,10 +226,15 @@ public Builder addHeaders(Map<String, String> headers) {
return this;
}

public Builder setAuthentication(boolean auth) {
this.authentication = auth;
return this;
}

public Method build(){
return new Method(this.name, this.path, this.httpMethod,
this.expectedStatus, this.requiredParams, this.optionalParams,
this.headers);
this.headers, this.authentication);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/net/linkfluence/jspore/SpecParser.java
Expand Up @@ -46,6 +46,7 @@ class SpecParser {
public static final String OPTIONAL_PARAMS = "optional_params";
public static final String EXPECTED_STATUS = "expected_status";
public static final String HEADERS = "headers";
public static final String AUTH = "authentication";

private static final Set<String> METHOD_KEYS = new ImmutableSet.Builder<String>()
.add(HTTP_METHOD)
Expand All @@ -54,6 +55,7 @@ class SpecParser {
.add(OPTIONAL_PARAMS)
.add(EXPECTED_STATUS)
.add(HEADERS)
.add(AUTH)
.build();

private final ObjectMapper mapper = new ObjectMapper();
Expand Down Expand Up @@ -89,12 +91,16 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
Collection<Integer> expectedStatuses = readIntegerCollection(n.get(EXPECTED_STATUS));
Collection<String> optionalParams = readStringCollection(n.get(OPTIONAL_PARAMS));
Map<String, String> headers = readStringMap(n.get(HEADERS));

boolean authentication = false;
if(n.get(AUTH) != null) {
authentication = n.get(AUTH).asBoolean(false);
}
Method method = new Method.Builder(methodName, path, httpMethod)
.addHeaders(headers)
.addExpectedStatuses(expectedStatuses)
.addOptionalParams(optionalParams)
.addRequiredParams(requiredParams)
.setAuthentication(authentication)
.build();
m.addMethod(method);
}
Expand Down
Expand Up @@ -25,7 +25,9 @@ public Basic(String user, String password){

@Override
public void sendRequest(RequestBuilder request, Object body, Method context) throws SporeException {
request.setHeader("Authorization", "Basic " + b64Token);
if(context.authentication){
request.setHeader("Authorization", "Basic " + b64Token);
}
next(request, body, context);
}

Expand Down

0 comments on commit 5afa2f5

Please sign in to comment.