Skip to content
This repository has been archived by the owner on Oct 19, 2018. It is now read-only.

Commit

Permalink
finish implementations of AX store request and response message
Browse files Browse the repository at this point in the history
git-svn-id: file:///Users/willnorris/Projects/svn-import/java-openid/trunk@85 eae3f9c0-6542-46b9-8785-326aab784c2f
  • Loading branch information
willnorris committed Oct 27, 2009
1 parent 39b583c commit 7098cc3
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 37 deletions.
Expand Up @@ -83,7 +83,7 @@ public AttributeExchangeMessage unmarshall(ParameterMap parameters) throws Unmar
unmarshaller = unmarshallers.get(StoreRequest.class);
} else if (mode.equals(StoreResponse.MODE_SUCCESS) || mode.equals(StoreResponse.MODE_FAILURE)) {
builder = builders.getBuilder(StoreResponse.class);
unmarshaller = unmarshallers.get(FetchRequest.class);
unmarshaller = unmarshallers.get(StoreResponse.class);
}

if (builder == null) {
Expand Down
Expand Up @@ -31,6 +31,20 @@ public interface StoreResponse extends AttributeExchangeMessage {
*/
public static final String MODE_FAILURE = "store_response_failure";

/**
* Get whether this response indicates success.
*
* @return if success
*/
public boolean getSuccess();

/**
* Set whether this response indicates success.
*
* @param newSuccess if success
*/
public void setSuccess(boolean newSuccess);

/**
* A human-readable message indicating why the store request failed.
*
Expand All @@ -44,4 +58,5 @@ public interface StoreResponse extends AttributeExchangeMessage {
* @param newError the error message
*/
public void setError(String newError);

}
Expand Up @@ -16,7 +16,7 @@

package edu.internet2.middleware.openid.extensions.ax.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -36,7 +36,7 @@ public class StoreRequestImpl extends BaseAttributeExchangeMessage implements St
* Constructor.
*/
public StoreRequestImpl() {
attributes = new HashMap<String, List<String>>();
attributes = new LinkedHashMap<String, List<String>>();
}

/** {@inheritDoc} */
Expand Down
Expand Up @@ -19,10 +19,14 @@
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import edu.internet2.middleware.openid.common.NamespaceMap;
import edu.internet2.middleware.openid.common.ParameterMap;
import edu.internet2.middleware.openid.extensions.ax.AttributeExchange;
import edu.internet2.middleware.openid.extensions.ax.AttributeExchangeMarshaller;
import edu.internet2.middleware.openid.extensions.ax.StoreRequest;
import edu.internet2.middleware.openid.extensions.ax.AttributeExchange.Parameter;

/**
* StoreRequestMarshaller.
Expand All @@ -31,34 +35,37 @@ public class StoreRequestMarshaller implements AttributeExchangeMarshaller<Store

/** {@inheritDoc} */
public void marshall(StoreRequest request, ParameterMap parameters) {
int aliasCount = 0;
int valueCount;
String aliasName;

NamespaceMap types = new NamespaceMap();
types.setAliasPrefix(AttributeExchange.ALIAS_PREFIX);

// attributes
Map<String, List<String>> attributes = request.getAttributes();
for (String name : attributes.keySet()) {
List<String> values = attributes.get(name);
if (values.size() <= 0) {
continue;
}
for (String typeURI : attributes.keySet()) {
List<String> values = attributes.get(typeURI);
String alias = types.add(typeURI);

aliasName = AttributeExchange.ALIAS_PREFIX + (++aliasCount);
valueCount = 0;
QName typeQName = new QName(AttributeExchange.AX_10_NS, Parameter.type.toString() + "." + alias);
parameters.put(typeQName, typeURI);

// add type parameter
// parameters.put(Parameter.type.toString() + "." + aliasName, name);

if (values.size() == 1) {
// parameters.put(Parameter.value.toString() + "." + aliasName, values.get(0));
if (values.isEmpty()) {
continue;
} else if (values.size() == 1) {
QName valueQName = new QName(AttributeExchange.AX_10_NS, Parameter.value.toString() + "." + alias);
parameters.put(valueQName, values.get(0));
} else {
// parameters.put(Parameter.count.toString() + "." + aliasName, values.size() + "");
for (String value : values) {
// parameters.put(Parameter.value.toString() + "." + aliasName + "." + (++valueCount), value);
QName countQName = new QName(AttributeExchange.AX_10_NS, Parameter.count.toString() + "." + alias);
parameters.put(countQName, Integer.toString(values.size()));

for (int i = 0; i < values.size(); i++) {
QName valueQName = new QName(AttributeExchange.AX_10_NS, Parameter.value.toString() + "." + alias
+ "." + (i + 1));
parameters.put(valueQName, values.get(i));
}
}

}

}

}
Expand Up @@ -16,9 +16,17 @@

package edu.internet2.middleware.openid.extensions.ax.impl;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import edu.internet2.middleware.openid.common.NamespaceMap;
import edu.internet2.middleware.openid.common.ParameterMap;
import edu.internet2.middleware.openid.extensions.ax.AttributeExchange;
import edu.internet2.middleware.openid.extensions.ax.AttributeExchangeUnmarshaller;
import edu.internet2.middleware.openid.extensions.ax.StoreRequest;
import edu.internet2.middleware.openid.extensions.ax.AttributeExchange.Parameter;

/**
* StoreRequestUnmarshaller.
Expand All @@ -27,6 +35,42 @@ public class StoreRequestUnmarshaller implements AttributeExchangeUnmarshaller<S

/** {@inheritDoc} */
public void unmarshall(StoreRequest request, ParameterMap parameters) {

// get all the attribute types
NamespaceMap types = new NamespaceMap();
for (QName qname : parameters.keySet()) {
String[] parts = qname.getLocalPart().split("\\.", 2);
if (Parameter.type.toString().equals(parts[0])) {
types.add(parameters.get(qname), parts[1]);
}
}

// get the attribute values and add to message
for (String typeURI : types.getURIs()) {
String alias = types.getAlias(typeURI);
List<String> values = new ArrayList<String>();
int count = 1;

QName countQName = new QName(AttributeExchange.AX_10_NS, Parameter.count.toString() + "." + alias);
String countString = parameters.get(countQName);
if (countString != null) {
count = Integer.parseInt(countString);
}

if (count == 1) {
QName valueQName = new QName(AttributeExchange.AX_10_NS, Parameter.value.toString() + "." + alias);
values.add(parameters.get(valueQName));
} else {
for (int i = 0; i < count; i++) {
QName valueQName = new QName(AttributeExchange.AX_10_NS, Parameter.value.toString() + "." + alias
+ "." + (i + 1));
values.add(parameters.get(valueQName));
}
}

request.getAttributes().put(typeURI, values);
}

}

}
Expand Up @@ -56,20 +56,12 @@ public void setError(String newError) {
error = newError;
}

/**
* Get whether this response indicates success.
*
* @return if success
*/
/** {@inheritDoc} */
public boolean getSuccess() {
return success;
}

/**
* Set whether this response indicates success.
*
* @param newSuccess if success
*/
/** {@inheritDoc} */
public void setSuccess(boolean newSuccess) {
success = newSuccess;
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
import edu.internet2.middleware.openid.common.ParameterMap;
import edu.internet2.middleware.openid.extensions.ax.AttributeExchangeMarshaller;
import edu.internet2.middleware.openid.extensions.ax.StoreResponse;
import edu.internet2.middleware.openid.extensions.ax.AttributeExchange.Parameter;

/**
* StoreResponseMarshaller.
Expand All @@ -27,8 +28,8 @@ public class StoreResponseMarshaller implements AttributeExchangeMarshaller<Stor

/** {@inheritDoc} */
public void marshall(StoreResponse response, ParameterMap parameters) {
if (response.getError() != null) {
// parameters.put(Parameter.error.toString(), response.getError());
if (!response.getSuccess() && response.getError() != null) {
parameters.put(Parameter.error.QNAME, response.getError());
}
}

Expand Down
Expand Up @@ -28,7 +28,13 @@ public class StoreResponseUnmarshaller implements AttributeExchangeUnmarshaller<

/** {@inheritDoc} */
public void unmarshall(StoreResponse response, ParameterMap parameters) {
response.setError(parameters.get(Parameter.error.QNAME));
String mode = parameters.get(Parameter.mode.QNAME);
if (StoreResponse.MODE_SUCCESS.equals(mode)) {
response.setSuccess(true);
} else if (StoreResponse.MODE_FAILURE.equals(mode)) {
response.setSuccess(false);
response.setError(parameters.get(Parameter.error.QNAME));
}
}

}
Expand Up @@ -66,9 +66,6 @@ public void testMessageMarshall() {
response.getAttributes().putAll(expectedAttributes);
response.setUpdateURL(expectedUpdateURL);

logMessageParameters(expectedParameters);
logMessageParameters(response);

assertEquals(expectedParameters, response);
}

Expand Down
@@ -0,0 +1,74 @@
/*
* Copyright 2009 University Corporation for Advanced Internet Development, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package edu.internet2.middleware.openid.extensions.ax;

import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import edu.internet2.middleware.openid.extensions.BaseMessageExtensionTestCase;

/**
* Test case for creating, marshalling, and unmarshalling {@link FetchRequest}.
*/
public class StoreRequestTest extends BaseMessageExtensionTestCase {

/** Expected mode. */
private String expectedMode;

/** Expected attributes. */
private Map<String, List<String>> expectedAttributes;

/**
* Constructor.
*
* @throws MalformedURLException if URL is malformed
*/
public StoreRequestTest() throws MalformedURLException {
messageFile = "/data/edu/internet2/middleware/openid/extensions/ax/StoreRequest.txt";

expectedMode = StoreRequest.MODE;
expectedAttributes = new LinkedHashMap<String, List<String>>();
expectedAttributes.put("http://example.com/schema/fullname", Arrays.asList(new String[] { "Bob Smith" }));
expectedAttributes.put("http://example.com/schema/favourite_movie", Arrays.asList(new String[] { "Movie1",
"Movie2", }));
}

/** {@inheritDoc} */
public void testMessageMarshall() {
StoreRequest request = (StoreRequest) buildMessageExtension(StoreRequest.class);

request.getAttributes().putAll(expectedAttributes);

assertEquals(expectedParameters, request);
}

/** {@inheritDoc} */
public void testMessageUnmarshall() {
StoreRequest request = (StoreRequest) unmarshallMessageExtension(messageFile);

String mode = request.getMode();
assertEquals("StoreRequest mode was " + mode + ", expected " + expectedMode, expectedMode, mode);

Map<String, List<String>> attributes = request.getAttributes();
assertEquals("StoreRequest attributes were " + attributes + ", expected " + expectedAttributes,
expectedAttributes, attributes);
}

}

0 comments on commit 7098cc3

Please sign in to comment.