You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oltu.apache.org by si...@apache.org on 2013/10/29 13:45:50 UTC

svn commit: r1536690 [1/2] - in /oltu/trunk: ./ commons/ commons/encodedtoken/ commons/encodedtoken/src/ commons/encodedtoken/src/main/ commons/encodedtoken/src/main/java/ commons/encodedtoken/src/main/java/org/ commons/encodedtoken/src/main/java/org/a...

Author: simonetripodi
Date: Tue Oct 29 12:45:49 2013
New Revision: 1536690

URL: http://svn.apache.org/r1536690
Log:
OLTU-125 - Create a 'commons' module for JSON reader/writer

added small commons 'json' and 'encodedtoken' modules in order to drop duplicate codes in JWT/JWS modules

Added:
    oltu/trunk/commons/   (with props)
    oltu/trunk/commons/encodedtoken/   (with props)
    oltu/trunk/commons/encodedtoken/pom.xml   (with props)
    oltu/trunk/commons/encodedtoken/src/
    oltu/trunk/commons/encodedtoken/src/main/
    oltu/trunk/commons/encodedtoken/src/main/java/
    oltu/trunk/commons/encodedtoken/src/main/java/org/
    oltu/trunk/commons/encodedtoken/src/main/java/org/apache/
    oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/
    oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/
    oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/
    oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenDecoder.java   (with props)
    oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenReader.java   (with props)
    oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenWriter.java   (with props)
    oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/package-info.java   (with props)
    oltu/trunk/commons/json/   (with props)
    oltu/trunk/commons/json/pom.xml   (with props)
    oltu/trunk/commons/json/src/
    oltu/trunk/commons/json/src/main/
    oltu/trunk/commons/json/src/main/java/
    oltu/trunk/commons/json/src/main/java/org/
    oltu/trunk/commons/json/src/main/java/org/apache/
    oltu/trunk/commons/json/src/main/java/org/apache/oltu/
    oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/
    oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/
    oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableBuilder.java   (with props)
    oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntity.java   (with props)
    oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java   (with props)
    oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java   (with props)
    oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/package-info.java   (with props)
    oltu/trunk/commons/pom.xml   (with props)
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSConstants.java   (with props)
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderParser.java   (with props)
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderWriter.java   (with props)
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetParser.java   (with props)
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetWriter.java   (with props)
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTConstants.java
      - copied, changed from r1536305, oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTHeaderParser.java   (with props)
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTHeaderWriter.java   (with props)
Removed:
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/AbstractJWSIO.java
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWTEntity.java
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
Modified:
    oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/MainController.java
    oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/TokenController.java
    oltu/trunk/jose/jws/pom.xml
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/Header.java
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/JWS.java
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSReader.java
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSWriter.java
    oltu/trunk/oauth-2.0/jwt/pom.xml
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/ClaimsSet.java
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/Header.java
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWT.java
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java
    oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java
    oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java
    oltu/trunk/pom.xml

Propchange: oltu/trunk/commons/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Oct 29 12:45:49 2013
@@ -0,0 +1 @@
+.settings

Propchange: oltu/trunk/commons/encodedtoken/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Oct 29 12:45:49 2013
@@ -0,0 +1,2 @@
+target
+.settings

Added: oltu/trunk/commons/encodedtoken/pom.xml
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/encodedtoken/pom.xml?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/encodedtoken/pom.xml (added)
+++ oltu/trunk/commons/encodedtoken/pom.xml Tue Oct 29 12:45:49 2013
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+-->
+<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>
+
+  <parent>
+    <groupId>org.apache.oltu.commons</groupId>
+    <artifactId>org.apache.oltu.commons.parent</artifactId>
+    <version>1-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>org.apache.oltu.commons.encodedtoken</artifactId>
+  <version>1.0.0-SNAPSHOT</version>
+
+  <name>Apache Oltu - Commons - Encoded Token</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>commons-codec</groupId>
+      <artifactId>commons-codec</artifactId>
+      <version>1.8</version>
+    </dependency>
+  </dependencies>
+
+</project>

Propchange: oltu/trunk/commons/encodedtoken/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/encodedtoken/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: oltu/trunk/commons/encodedtoken/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenDecoder.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenDecoder.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenDecoder.java (added)
+++ oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenDecoder.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.commons.encodedtoken;
+
+import java.nio.charset.Charset;
+
+import org.apache.commons.codec.binary.Base64;
+
+abstract class TokenDecoder {
+
+    /**
+     * The {@code UTF-8} charset reference.
+     */
+    private static final Charset UTF_8 = Charset.forName("UTF-8");
+
+    /**
+     * The BASE64 encoder/decoder.
+     */
+    private final Base64 base64 = new Base64(true);
+
+    protected final String base64Decode(String base64encoded) {
+        return new String(base64.decode(base64encoded), UTF_8);
+    }
+
+    protected final String base64Encode(String input) {
+        return new String(base64.encode(input.getBytes(UTF_8)), UTF_8);
+    }
+
+}

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenDecoder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenDecoder.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenDecoder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenReader.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenReader.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenReader.java (added)
+++ oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenReader.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.commons.encodedtoken;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public abstract class TokenReader<T> extends TokenDecoder {
+
+    /**
+     * The Base64 JSON string default separator.
+     */
+    private final Pattern base64TokenPattern = Pattern.compile("([a-zA-Z0-9/+=]+)\\.([a-zA-Z0-9/+=]+)\\.(.+)");
+
+    public T read(String base64String) {
+        if (base64String == null || base64String.isEmpty()) {
+            throw new IllegalArgumentException("Impossible to obtain a Token from a null or empty string");
+        }
+
+        // TODO improve multi-line tokens
+        StringBuilder buffer = new StringBuilder();
+        BufferedReader reader = new BufferedReader(new StringReader(base64String));
+        String line = null;
+        try {
+            while ((line = reader.readLine()) != null) {
+                buffer.append(line.trim());
+            }
+        } catch (IOException e) {
+            // it cannot happen
+        } finally {
+            try {
+                reader.close();
+            } catch (IOException e) {
+                // swallow it
+            }
+        }
+
+        Matcher matcher = base64TokenPattern.matcher(buffer.toString());
+        if (!matcher.matches()) {
+            throw new IllegalArgumentException(base64String
+                                               + "is not a valid Token, it does not match with the pattern: "
+                                               + base64TokenPattern.pattern());
+        }
+
+        // HEADER
+        String header = matcher.group(1);
+        String decodedHeader = base64Decode(header);
+
+        // BODY
+        String body = matcher.group(2);
+        String decodedBody = base64Decode(body);
+
+        // SIGNATURE
+        String signature = matcher.group(3);
+        String decodedSignature = base64Decode(signature);
+
+        return build(base64String, decodedHeader, decodedBody, decodedSignature);
+    }
+
+    protected abstract T build(String rawString, String decodedHeader, String decodedBody, String decodedSignature);
+
+}

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenReader.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenReader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenWriter.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenWriter.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenWriter.java (added)
+++ oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenWriter.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.commons.encodedtoken;
+
+public abstract class TokenWriter<T> extends TokenDecoder {
+
+    public final String write(T token) {
+        if (token == null) {
+            throw new IllegalArgumentException("Impossible to build a Token from a null JWS representation.");
+        }
+
+        String header = writeHeader(token);
+        String encodedHeader = base64Encode(header);
+
+        String body = writeBody(token);
+        String encodedBody = base64Encode(body);
+
+        String signature = writeSignature(token);
+        String encodedSignature = base64Encode(signature);
+
+        return new StringBuilder()
+                   .append(encodedHeader)
+                   .append('.')
+                   .append(encodedBody)
+                   .append('.')
+                   .append(encodedSignature)
+                   .toString();
+    }
+
+    protected abstract String writeHeader(T token);
+
+    protected abstract String writeBody(T token);
+
+    protected abstract String writeSignature(T token);
+
+}

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenWriter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/TokenWriter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/package-info.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/package-info.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/package-info.java (added)
+++ oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/package-info.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/**
+ * Common reusable Encoded Token utilities.
+ */
+package org.apache.oltu.commons.encodedtoken;

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/package-info.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/encodedtoken/src/main/java/org/apache/oltu/commons/encodedtoken/package-info.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Propchange: oltu/trunk/commons/json/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Oct 29 12:45:49 2013
@@ -0,0 +1,2 @@
+.settings
+target

Added: oltu/trunk/commons/json/pom.xml
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/json/pom.xml?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/json/pom.xml (added)
+++ oltu/trunk/commons/json/pom.xml Tue Oct 29 12:45:49 2013
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+-->
+<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>
+
+  <parent>
+    <groupId>org.apache.oltu.commons</groupId>
+    <artifactId>org.apache.oltu.commons.parent</artifactId>
+    <version>1-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>org.apache.oltu.commons.json</artifactId>
+  <version>1.0.0-SNAPSHOT</version>
+
+  <name>Apache Oltu - Commons - JSON</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.json</groupId>
+      <artifactId>json</artifactId>
+      <version>20131018</version>
+    </dependency>
+  </dependencies>
+
+</project>

Propchange: oltu/trunk/commons/json/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/json/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: oltu/trunk/commons/json/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableBuilder.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableBuilder.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableBuilder.java (added)
+++ oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableBuilder.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.commons.json;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public abstract class CustomizableBuilder<E> {
+
+    /**
+     * The registry that keeps the custom fields.
+     */
+    private final Map<String, Object> customFields = new LinkedHashMap<String, Object>();
+
+    protected final Map<String, Object> getCustomFields() {
+        return customFields;
+    }
+
+    /**
+     * TODO
+     *
+     * @param key
+     * @param value
+     * @return
+     */
+    public final CustomizableBuilder<E> setCustomField(String key, Object value) {
+        if (key == null) {
+            throw new IllegalArgumentException("Null key not allowed.");
+        }
+        if (value != null) {
+            customFields.put(key, value);
+        }
+        return this;
+    }
+
+    public abstract E build();
+
+}

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntity.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntity.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntity.java (added)
+++ oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntity.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.commons.json;
+
+import java.util.Formatter;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+/**
+ * TODO
+ */
+public abstract class CustomizableEntity {
+
+    /**
+     * The registry that keeps the custom fields.
+     */
+    private final Map<String, Object> customFields;
+
+    /**
+     * TODO
+     *
+     * @param customFields
+     */
+    public CustomizableEntity(Map<String, Object> customFields) {
+        if (customFields == null) {
+            throw new IllegalArgumentException("Null custom field registry not allowed.");
+        }
+        this.customFields = customFields;
+    }
+
+    /**
+     * Return the specified custom field value,
+     * {@code null} if the custom field is not present.
+     *
+     * @param name the custom field name, it cannot be null.
+     * @return the specified custom field value,
+     *         {@code null} if the custom field is not present.
+     */
+    public final <T> T getCustomField(String name, Class<T> type) {
+        if (name == null) {
+            throw new IllegalArgumentException("Null custom field name not present in the registry.");
+        }
+
+        Object value = customFields.get(name);
+
+        if (value != null) {
+            return type.cast(value);
+        }
+
+        return null;
+    }
+
+    /**
+     * Returns the custom fields stored in the entity.
+     *
+     * @return the custom fields stored in the entity.
+     */
+    public final Set<Entry<String, Object>> getCustomFields() {
+        return customFields.entrySet();
+    }
+
+    @Override
+    public String toString() {
+        if (customFields.isEmpty()) {
+            return "";
+        }
+
+        Formatter formatter = new Formatter();
+
+        formatter.format( ", " );
+
+        int counter = 0;
+        for (Entry<String, Object> customField : customFields.entrySet()) {
+            Object value = customField.getValue();
+
+            if (value != null) {
+                if (counter++ > 0) {
+                    formatter.format(", ");
+                }
+
+                String pattern;
+                if (customField.getValue() instanceof Number) {
+                    pattern = "\"%s\": %s";
+                } else {
+                    pattern = "\"%s\": \"%s\"";
+                }
+
+                formatter.format(pattern, customField.getKey(), value);
+            }
+        }
+
+        try {
+            return formatter.toString();
+        } finally {
+            formatter.close();
+        }
+    }
+
+}

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java (added)
+++ oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.commons.json;
+
+import static java.lang.String.format;
+
+import org.json.JSONArray;
+import org.json.JSONTokener;
+
+/**
+ * TODO
+ */
+public abstract class CustomizableEntityReader<E, B extends CustomizableBuilder<E>> {
+
+    private final B builder;
+
+    public CustomizableEntityReader(B builder) {
+        this.builder = builder;
+    }
+
+    protected final B getBuilder() {
+        return builder;
+    }
+
+    /**
+     * Method extracted from {@code org.json.JSONObject#JSONObject(JSONTokener)}
+     *
+     * @param jsonString
+     */
+    public void read(String jsonString) {
+        final JSONTokener x = new JSONTokener(jsonString);
+        char c;
+        String key;
+
+        if (x.nextClean() != '{') {
+            throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'",
+                                                      jsonString));
+        }
+        for (;;) {
+            c = x.nextClean();
+            switch (c) {
+            case 0:
+                throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must end with '}'",
+                                                          jsonString));
+            case '}':
+                return;
+            default:
+                x.back();
+                key = x.nextValue().toString();
+            }
+
+            /*
+             * The key is followed by ':'. We will also tolerate '=' or '=>'.
+             */
+            c = x.nextClean();
+            if (c == '=') {
+                if (x.next() != '>') {
+                    x.back();
+                }
+            } else if (c != ':') {
+                throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'",
+                                                          jsonString, key));
+            }
+            Object value = x.nextValue();
+
+            // guard from null values
+            if (value != null) {
+                if (value instanceof JSONArray) { // only plain simple arrays in this version
+                    JSONArray array = (JSONArray) value;
+                    Object[] values = new Object[array.length()];
+                    for (int i = 0; i < array.length(); i++) {
+                        values[i] = array.get(i);
+                    }
+                    value = values;
+                }
+
+                // if the concrete implementation is not able to handle the property, set the custom field
+                if (!handleProperty(key, value)) {
+                    builder.setCustomField(key, value);
+                }
+            }
+
+            /*
+             * Pairs are separated by ','. We will also tolerate ';'.
+             */
+            switch (x.nextClean()) {
+            case ';':
+            case ',':
+                if (x.nextClean() == '}') {
+                    return;
+                }
+                x.back();
+                break;
+            case '}':
+                return;
+            default:
+                throw new IllegalArgumentException("Expected a ',' or '}'");
+            }
+        }
+    }
+
+    protected abstract <T> boolean handleProperty(String key, T value);
+
+}

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java (added)
+++ oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.commons.json;
+
+import java.util.Map.Entry;
+
+import org.json.JSONStringer;
+
+public abstract class CustomizableEntityWriter<CE extends CustomizableEntity> {
+
+    private final JSONStringer jsonWriter = new JSONStringer();
+
+    public final String write(CE customizableEntity) {
+        jsonWriter.object();
+
+        handleProperties(customizableEntity);
+
+        for (Entry<String, Object> customFields : customizableEntity.getCustomFields()) {
+            set(customFields.getKey(), customFields.getValue());
+        }
+
+        jsonWriter.endObject();
+        return jsonWriter.toString();
+    }
+
+    protected abstract void handleProperties(CE customizableEntity);
+
+    protected final <T> void set(String key, T value) {
+        if (value != null) {
+            jsonWriter.key(key).value(value);
+        }
+    }
+
+    protected final <T> void set(String key, T[] value) {
+        if (value == null) {
+            return;
+        }
+
+        jsonWriter.key(key).array();
+
+        for (T item : value) {
+            if (item != null) {
+                jsonWriter.value(item);
+            }
+        }
+
+        jsonWriter.endArray();
+    }
+
+}

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/package-info.java
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/package-info.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/package-info.java (added)
+++ oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/package-info.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/**
+ * Common reusable JSON utilities.
+ */
+package org.apache.oltu.commons.json;

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/package-info.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/commons/json/src/main/java/org/apache/oltu/commons/json/package-info.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/commons/pom.xml
URL: http://svn.apache.org/viewvc/oltu/trunk/commons/pom.xml?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/commons/pom.xml (added)
+++ oltu/trunk/commons/pom.xml Tue Oct 29 12:45:49 2013
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+-->
+<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>
+
+  <parent>
+    <groupId>org.apache.oltu</groupId>
+    <artifactId>org.apache.oltu.parent</artifactId>
+    <version>1</version>
+    <relativePath>../parent</relativePath>
+  </parent>
+
+  <groupId>org.apache.oltu.commons</groupId>
+  <artifactId>org.apache.oltu.commons.parent</artifactId>
+  <version>1-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <name>Apache Oltu - Commons - Parent</name>
+
+  <modules>
+    <module>encodedtoken</module>
+    <module>json</module>
+  </modules>
+
+</project>

Propchange: oltu/trunk/commons/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/commons/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: oltu/trunk/commons/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/MainController.java
URL: http://svn.apache.org/viewvc/oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/MainController.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/MainController.java (original)
+++ oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/MainController.java Tue Oct 29 12:45:49 2013
@@ -29,6 +29,8 @@ import org.apache.oltu.oauth2.client.dem
 import org.apache.oltu.oauth2.client.demo.model.OAuthRegParams;
 import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
 import org.apache.oltu.oauth2.jwt.JWT;
+import org.apache.oltu.oauth2.jwt.io.JWTClaimsSetWriter;
+import org.apache.oltu.oauth2.jwt.io.JWTHeaderWriter;
 import org.apache.oltu.oauth2.jwt.io.JWTReader;
 import org.apache.oltu.oauth2.jwt.io.JWTWriter;
 import org.slf4j.Logger;
@@ -107,8 +109,8 @@ public class MainController {
         try {
             JWT jwt = jwtReader.read(oauthParams.getJwt());
 
-            oauthParams.setHeader(jwtWriter.write(jwt.getHeader()));
-            oauthParams.setClaimsSet(jwtWriter.write(jwt.getClaimsSet()));
+            oauthParams.setHeader(new JWTHeaderWriter().write(jwt.getHeader()));
+            oauthParams.setClaimsSet(new JWTClaimsSetWriter().write(jwt.getClaimsSet()));
         } catch (Exception e){
             oauthParams.setErrorMessage(
                     "Error while decoding the token: " + e);

Modified: oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/TokenController.java
URL: http://svn.apache.org/viewvc/oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/TokenController.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/TokenController.java (original)
+++ oltu/trunk/demos/client-demo/src/main/java/org/apache/oltu/oauth2/client/demo/controller/TokenController.java Tue Oct 29 12:45:49 2013
@@ -39,6 +39,8 @@ import org.apache.oltu.oauth2.common.exc
 import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
 import org.apache.oltu.oauth2.common.message.types.GrantType;
 import org.apache.oltu.oauth2.jwt.JWT;
+import org.apache.oltu.oauth2.jwt.io.JWTClaimsSetWriter;
+import org.apache.oltu.oauth2.jwt.io.JWTHeaderWriter;
 import org.apache.oltu.oauth2.jwt.io.JWTWriter;
 import org.apache.oltu.openidconnect.client.response.OpenIdConnectResponse;
 import org.springframework.stereotype.Controller;
@@ -101,8 +103,8 @@ public class TokenController {
             	JWT idToken = openIdConnectResponse.getIdToken();
             	oauthParams.setIdToken(idToken.getRawString());
 
-            	oauthParams.setHeader(jwtWriter.write(idToken.getHeader()));
-            	oauthParams.setClaimsSet(jwtWriter.write(idToken.getClaimsSet()));
+            	oauthParams.setHeader(new JWTHeaderWriter().write(idToken.getHeader()));
+                oauthParams.setClaimsSet(new JWTClaimsSetWriter().write(idToken.getClaimsSet()));
 
             	URL url = new URL(oauthParams.getTokenEndpoint());
 

Modified: oltu/trunk/jose/jws/pom.xml
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/pom.xml?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/jose/jws/pom.xml (original)
+++ oltu/trunk/jose/jws/pom.xml Tue Oct 29 12:45:49 2013
@@ -29,21 +29,17 @@
 
   <name>Apache Oltu - JOSE - JWS</name>
 
-  <properties>
-    <oltu.oauth2.version>1.0.0-SNAPSHOT</oltu.oauth2.version>
-  </properties>
-
   <dependencies>
     <dependency>
-      <groupId>org.json</groupId>
-      <artifactId>json</artifactId>
-      <version>20131018</version>
+      <groupId>org.apache.oltu.commons</groupId>
+      <artifactId>org.apache.oltu.commons.encodedtoken</artifactId>
+      <version>1.0.0-SNAPSHOT</version>
     </dependency>
 
     <dependency>
-      <groupId>commons-codec</groupId>
-      <artifactId>commons-codec</artifactId>
-      <version>1.8</version>
+      <groupId>org.apache.oltu.commons</groupId>
+      <artifactId>org.apache.oltu.commons.json</artifactId>
+      <version>1.0.0-SNAPSHOT</version>
     </dependency>
   </dependencies>
 

Modified: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/Header.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/Header.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/Header.java (original)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/Header.java Tue Oct 29 12:45:49 2013
@@ -17,15 +17,15 @@
 package org.apache.oltu.jose.jws;
 
 import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
+
+import org.apache.oltu.commons.json.CustomizableEntity;
 
 /**
  * Represents the Header as defined in the section 4 of the JWS specification.
  *
  * @see http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-15#section-4
  */
-public final class Header {
+public final class Header extends CustomizableEntity {
 
     /**
      * The {@code alg} JWS Header parameter.
@@ -77,11 +77,6 @@ public final class Header {
      */
     private final String[] critical;
 
-    /**
-     * The registry that keeps the custom fields.
-     */
-    private final Map<String, Object> customFields;
-
     Header(String algorithm,
            String jwkSetUrl,
            String jsonWebKey,
@@ -93,6 +88,7 @@ public final class Header {
            String contentType,
            String[] critical,
            Map<String, Object> customFields) {
+        super(customFields);
         this.algorithm = algorithm;
         this.jwkSetUrl = jwkSetUrl;
         this.jsonWebKey = jsonWebKey;
@@ -103,7 +99,6 @@ public final class Header {
         this.type = type;
         this.contentType = contentType;
         this.critical = critical;
-        this.customFields = customFields;
     }
 
     public String getAlgorithm() {
@@ -146,35 +141,4 @@ public final class Header {
         return critical;
     }
 
-    /**
-     * Return the specified custom field value,
-     * {@code null} if the custom field is not present.
-     *
-     * @param name the custom field name, it cannot be null.
-     * @return the specified custom field value,
-     *         {@code null} if the custom field is not present.
-     */
-    public <T> T getCustomField(String name, Class<T> type) {
-        if (name == null) {
-            throw new IllegalArgumentException("Null custom field name not present in the registry.");
-        }
-
-        Object value = customFields.get(name);
-
-        if (value != null) {
-            return type.cast(value);
-        }
-
-        return null;
-    }
-
-    /**
-     * Returns the custom fields stored in the entity.
-     *
-     * @return the custom fields stored in the entity.
-     */
-    public Set<Entry<String, Object>> getCustomFields() {
-        return customFields.entrySet();
-    }
-
 }

Modified: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/JWS.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/JWS.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/JWS.java (original)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/JWS.java Tue Oct 29 12:45:49 2013
@@ -16,9 +16,7 @@
  */
 package org.apache.oltu.jose.jws;
 
-import java.util.LinkedHashMap;
-import java.util.Map;
-
+import org.apache.oltu.commons.json.CustomizableBuilder;
 import org.apache.oltu.jose.jws.signature.SignatureMethod;
 import org.apache.oltu.jose.jws.signature.SigningKey;
 import org.apache.oltu.jose.jws.signature.VerifyingKey;
@@ -93,7 +91,7 @@ public class JWS {
         return method.verify(signature, payload, verifyingKey);
     }
 
-    public static final class Builder {
+    public static final class Builder extends CustomizableBuilder<JWS> {
 
         /**
          * The {@code alg} JWS Header parameter.
@@ -155,11 +153,6 @@ public class JWS {
          */
         private String signature;
 
-        /**
-         * The registry that keeps the custom fields.
-         */
-        private final Map<String, Object> customFields = new LinkedHashMap<String, Object>();
-
         public Builder setAlgorithm(String algorithm) {
             this.algorithm = algorithm;
             return this;
@@ -210,16 +203,6 @@ public class JWS {
             return this;
         }
 
-        public Builder setCustomField(String key, String value) {
-            if (key == null) {
-                throw new IllegalArgumentException("Null key not allowed.");
-            }
-            if (value != null) {
-                customFields.put(key, value);
-            }
-            return this;
-        }
-
         public Builder setPayload(String payload) {
             this.payload = payload;
             return this;
@@ -255,7 +238,7 @@ public class JWS {
                                       keyId, type,
                                       contentType,
                                       critical,
-                                      customFields),
+                                      getCustomFields()),
                            payload,
                            signature);
         }

Added: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSConstants.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSConstants.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSConstants.java (added)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSConstants.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.jose.jws.io;
+
+interface JWSConstants {
+
+    // header defined in the JWT specification
+
+    /**
+     * The {@code alg} JWT Header key.
+     */
+    public static final String ALGORITHM = "alg";
+
+    /**
+     * The {@code jku} JWT Header key.
+     */
+    public static final String JWK_SET_URL = "jku";
+
+    /**
+     * The {@code jwk} JWT Header key.
+     */
+    public static final String JSON_WEB_KEY = "jwk";
+
+    /**
+     * The {@code x5u} JWT Header key.
+     */
+    public static final String X509_URL = "x5u";
+
+    /**
+     * The {@code x5t} JWT Header key.
+     */
+    public static final String X509_CERTIFICATE_THUMBPRINT = "x5t";
+
+    /**
+     * The {@code x5c} JWT Header key.
+     */
+    public static final String X509_CERTIFICATE_CHAIN = "x5c";
+
+    /**
+     * The {@code kid} JWT Header key.
+     */
+    public static final String KEY_ID = "kid";
+
+    /**
+     * The {@code typ} JWT Header key.
+     */
+    public static final String TYPE = "typ";
+
+    /**
+     * The {@code cty} JWT Header key.
+     */
+    public static final String CONTENT_TYPE = "cty";
+
+    /**
+     * The {@code crit} JWT Header key.
+     */
+    public static final String CRITICAL = "crit";
+
+}

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSConstants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSConstants.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSConstants.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderParser.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderParser.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderParser.java (added)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderParser.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.jose.jws.io;
+
+import org.apache.oltu.commons.json.CustomizableEntityReader;
+import org.apache.oltu.jose.jws.JWS;
+
+final class JWSHeaderParser extends CustomizableEntityReader<JWS, JWS.Builder> implements JWSConstants {
+
+    public JWSHeaderParser(JWS.Builder builder) {
+        super(builder);
+    }
+
+    @Override
+    protected <T> boolean handleProperty(String key, T value) {
+        boolean handled = true;
+
+        if (ALGORITHM.equals(key)) {
+            getBuilder().setAlgorithm(String.valueOf(value));
+        } else if (JWK_SET_URL.equals(key)) {
+            getBuilder().setJwkSetUrl(String.valueOf(value));
+        } else if (JSON_WEB_KEY.equals(key)) {
+            getBuilder().setJsonWebKey(String.valueOf(value));
+        }  else if (X509_URL.equals(key)) {
+            getBuilder().setX509url(String.valueOf(value));
+        } else if (X509_CERTIFICATE_THUMBPRINT.equals(key)) {
+            getBuilder().setX509CertificateThumbprint(String.valueOf(value));
+        } else if (X509_CERTIFICATE_CHAIN.equals(key)) {
+            getBuilder().setX509CertificateChain(String.valueOf(value));
+        } else if (KEY_ID.equals(key)) {
+            getBuilder().setKeyId(String.valueOf(value));
+        } else if (TYPE.equals(key)) {
+            getBuilder().setType(String.valueOf(value));
+        } else if (CONTENT_TYPE.equals(key)) {
+            getBuilder().setContentType(String.valueOf(value));
+        } else if (CRITICAL.equals(key)) {
+            Object[] criticalValues = (Object[]) value;
+            String[] critical = new String[criticalValues.length];
+            for (int i = 0; i < critical.length; i++) {
+                critical[i] = String.valueOf(criticalValues[i]);
+            }
+            getBuilder().setCritical(critical);
+        } else {
+            handled = false;
+        }
+
+        return handled;
+    }
+
+
+}

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderParser.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderParser.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderWriter.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderWriter.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderWriter.java (added)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderWriter.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.jose.jws.io;
+
+import org.apache.oltu.commons.json.CustomizableEntityWriter;
+import org.apache.oltu.jose.jws.Header;
+
+final class JWSHeaderWriter extends CustomizableEntityWriter<Header> implements JWSConstants {
+
+    @Override
+    protected void handleProperties(Header header) {
+        set(ALGORITHM, header.getAlgorithm());
+        set(JWK_SET_URL, header.getJwkSetUrl());
+        set(JSON_WEB_KEY, header.getJsonWebKey());
+        set(X509_URL, header.getX509url());
+        set(X509_CERTIFICATE_THUMBPRINT, header.getX509CertificateThumbprint());
+        set(X509_CERTIFICATE_CHAIN, header.getX509CertificateChain());
+        set(KEY_ID, header.getKeyId());
+        set(TYPE, header.getType());
+        set(CONTENT_TYPE, header.getContentType());
+        set(CRITICAL, header.getCritical());
+    }
+
+}

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderWriter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSHeaderWriter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSReader.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSReader.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSReader.java (original)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSReader.java Tue Oct 29 12:45:49 2013
@@ -16,172 +16,26 @@
  */
 package org.apache.oltu.jose.jws.io;
 
-import static java.lang.String.format;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.StringReader;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
+import org.apache.oltu.commons.encodedtoken.TokenReader;
 import org.apache.oltu.jose.jws.JWS;
-import org.json.JSONArray;
-import org.json.JSONObject;
-import org.json.JSONTokener;
 
 /**
  * A {@link JWS} reader.
  *
  * TODO understand if JWT can be reused to avoid code duplication!
  */
-public final class JWSReader extends AbstractJWSIO {
-
-    /**
-     * The Base64 JSON string default separator.
-     */
-    private final Pattern base64JWTPattern = Pattern.compile("([a-zA-Z0-9/+=]+)\\.([a-zA-Z0-9/+=]+)\\.(.+)");
-
-    public JWS read(String base64jsonString) {
-        if (base64jsonString == null || base64jsonString.isEmpty()) {
-            throw new IllegalArgumentException("Impossible to obtain a JWT from a null or empty string");
-        }
-
-        // TODO improve multi-line tokens
-        StringBuilder buffer = new StringBuilder();
-        BufferedReader reader = new BufferedReader(new StringReader(base64jsonString));
-        String line = null;
-        try {
-            while ((line = reader.readLine()) != null) {
-                buffer.append(line.trim());
-            }
-        } catch (IOException e) {
-            // it cannot happen
-        } finally {
-            try {
-                reader.close();
-            } catch (IOException e) {
-                // swallow it
-            }
-        }
-
-        Matcher matcher = base64JWTPattern.matcher(buffer.toString());
-        if (!matcher.matches()) {
-            throw new IllegalArgumentException(base64jsonString
-                                               + "is not a valid JSON Web Signature, it does not match with the pattern: "
-                                               + base64JWTPattern.pattern());
-        }
-
-        JWS.Builder jwsBuilder = new JWS.Builder();
-
-        // HEADER
-
-        String header = matcher.group(1);
-        String decodedHeader = base64Decode(header);
-        parseHeader(jwsBuilder, decodedHeader);
-
-        // PAYLOAD
-        String payload = matcher.group(2);
-        String decodedPayload = base64Decode(payload);
-
-        // SIGNATURE
-        String signature = matcher.group(3);
-        String decodedSignature = base64Decode(signature);
-
-        return jwsBuilder.setPayload(decodedPayload)
-                         .setSignature(decodedSignature)
-                         .build();
-    }
+public final class JWSReader extends TokenReader<JWS> {
 
-    /**
-     * This method has been extracted from {@link JSONObject#JSONObject(JSONTokener)}
-     *
-     * @param jswBuilder the JWS builder reference
-     * @param decodedHeader the BASE64 decoded JSON string
-     */
-    private static void parseHeader(JWS.Builder jwsBuilder, String decodedHeader) {
-        final JSONTokener x = new JSONTokener(decodedHeader);
-        char c;
-        String key;
-
-        if (x.nextClean() != '{') {
-            throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'",
-                                                      decodedHeader));
-        }
-        for (;;) {
-            c = x.nextClean();
-            switch (c) {
-            case 0:
-                throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must end with '}'",
-                                                          decodedHeader));
-            case '}':
-                return;
-            default:
-                x.back();
-                key = x.nextValue().toString();
-            }
-
-            /*
-             * The key is followed by ':'. We will also tolerate '=' or '=>'.
-             */
-            c = x.nextClean();
-            if (c == '=') {
-                if (x.next() != '>') {
-                    x.back();
-                }
-            } else if (c != ':') {
-                throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'",
-                                                          decodedHeader, key));
-            }
-            Object value = x.nextValue();
-
-            if (value != null) {
-                if (ALGORITHM.equals(key)) {
-                    jwsBuilder.setAlgorithm(String.valueOf(value));
-                } else if (JWK_SET_URL.equals(key)) {
-                    jwsBuilder.setJwkSetUrl(String.valueOf(value));
-                } else if (JSON_WEB_KEY.equals(key)) {
-                    jwsBuilder.setJsonWebKey(String.valueOf(value));
-                }  else if (X509_URL.equals(key)) {
-                    jwsBuilder.setX509url(String.valueOf(value));
-                } else if (X509_CERTIFICATE_THUMBPRINT.equals(key)) {
-                    jwsBuilder.setX509CertificateThumbprint(String.valueOf(value));
-                } else if (X509_CERTIFICATE_CHAIN.equals(key)) {
-                    jwsBuilder.setX509CertificateChain(String.valueOf(value));
-                } else if (KEY_ID.equals(key)) {
-                    jwsBuilder.setKeyId(String.valueOf(value));
-                } else if (TYPE.equals(key)) {
-                    jwsBuilder.setType(String.valueOf(value));
-                } else if (CONTENT_TYPE.equals(key)) {
-                    jwsBuilder.setContentType(String.valueOf(value));
-                } else if (CRITICAL.equals(key)) {
-                    JSONArray array = (JSONArray) value;
-                    String[] critical = new String[array.length()];
-                    for (int i = 0; i < array.length(); i++) {
-                        critical[i] = array.getString(i);
-                    }
-                    jwsBuilder.setCritical(critical);
-                } else {
-                    jwsBuilder.setCustomField(key, String.valueOf(value));
-                }
-            }
-
-            /*
-             * Pairs are separated by ','. We will also tolerate ';'.
-             */
-            switch (x.nextClean()) {
-            case ';':
-            case ',':
-                if (x.nextClean() == '}') {
-                    return;
-                }
-                x.back();
-                break;
-            case '}':
-                return;
-            default:
-                throw new IllegalArgumentException("Expected a ',' or '}'");
-            }
-        }
+    @Override
+    protected JWS build(String rawString, String decodedHeader, String decodedBody, String decodedSignature) {
+        final JWS.Builder jwsBuilder = new JWS.Builder();
+
+        new JWSHeaderParser(jwsBuilder).read(decodedHeader);
+
+        return jwsBuilder
+               .setPayload(decodedBody)
+               .setSignature(decodedSignature)
+               .build();
     }
 
 }

Modified: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSWriter.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSWriter.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSWriter.java (original)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/io/JWSWriter.java Tue Oct 29 12:45:49 2013
@@ -16,93 +16,24 @@
  */
 package org.apache.oltu.jose.jws.io;
 
-import java.util.Map.Entry;
-
-import org.apache.oltu.jose.jws.Header;
+import org.apache.oltu.commons.encodedtoken.TokenWriter;
 import org.apache.oltu.jose.jws.JWS;
-import org.json.JSONStringer;
-
-public final class JWSWriter extends AbstractJWSIO {
-
-    public String write(JWS jws) {
-        if (jws == null) {
-            throw new IllegalArgumentException("Impossible to build a Token from a null JWS representation.");
-        }
-
-        String header = write(jws.getHeader());
-        String encodedHeader = base64Encode(header);
-
-        String payload = jws.getPayload();
-        String encodedPayload = base64Encode(payload);
-
-        String signature = jws.getSignature();
-        String encodedSignature = base64Encode(signature);
-
-        return new StringBuilder()
-                   .append(encodedHeader)
-                   .append('.')
-                   .append(encodedPayload)
-                   .append('.')
-                   .append(encodedSignature)
-                   .toString();
-    }
 
-    /**
-     * Serializes the input JWT Header to its correct JSON representation.
-     *
-     * @param header the JWT Header has to be serialized.
-     * @return the JSON string that represents the JWT Header.
-     */
-    public String write(Header header) {
-        if (header == null) {
-            throw new IllegalArgumentException("Null JWT Header cannot be serialized to JSON representation.");
-        }
-
-        JSONStringer jsonWriter = new JSONStringer();
-        jsonWriter.object();
-
-        setString(jsonWriter, ALGORITHM, header.getAlgorithm());
-        setString(jsonWriter, JWK_SET_URL, header.getJwkSetUrl());
-        setString(jsonWriter, JSON_WEB_KEY, header.getJsonWebKey());
-        setString(jsonWriter, X509_URL, header.getX509url());
-        setString(jsonWriter, X509_CERTIFICATE_THUMBPRINT, header.getX509CertificateThumbprint());
-        setString(jsonWriter, X509_CERTIFICATE_CHAIN, header.getX509CertificateChain());
-        setString(jsonWriter, KEY_ID, header.getKeyId());
-        setString(jsonWriter, TYPE, header.getType());
-        setString(jsonWriter, CONTENT_TYPE, header.getContentType());
-        setStringArray(jsonWriter, CRITICAL, header.getCritical());
-
-        for (Entry<String, Object> customField : header.getCustomFields()) {
-            setObject(jsonWriter, customField.getKey(), customField.getValue());
-        }
+public final class JWSWriter extends TokenWriter<JWS> {
 
-        return jsonWriter.endObject().toString();
+    @Override
+    protected String writeHeader(JWS token) {
+        return new JWSHeaderWriter().write(token.getHeader());
     }
 
-    private static void setString(JSONStringer jsonWriter, String key, String value) {
-        if (value != null) {
-            jsonWriter.key(key).value(value);
-        }
-    }
-
-    private static void setStringArray(JSONStringer jsonWriter, String key, String[] value) {
-        if (value != null) {
-            jsonWriter.key(key).array();
-
-            for (Object item : value) {
-                if (item != null) {
-                    jsonWriter.value(item);
-                }
-            }
-
-            jsonWriter.endArray();
-        }
+    @Override
+    protected String writeBody(JWS token) {
+        return token.getPayload();
     }
 
-    private static void setObject(JSONStringer jsonWriter, String key, Object value) {
-        if (value != null) {
-            jsonWriter.key(key).value(value);
-        }
+    @Override
+    protected String writeSignature(JWS token) {
+        return token.getSignature();
     }
 
 }

Modified: oltu/trunk/oauth-2.0/jwt/pom.xml
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/pom.xml?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/pom.xml (original)
+++ oltu/trunk/oauth-2.0/jwt/pom.xml Tue Oct 29 12:45:49 2013
@@ -31,14 +31,15 @@
 
   <dependencies>
     <dependency>
-      <groupId>org.json</groupId>
-      <artifactId>json</artifactId>
-      <version>20131018</version>
+      <groupId>org.apache.oltu.commons</groupId>
+      <artifactId>org.apache.oltu.commons.encodedtoken</artifactId>
+      <version>1.0.0-SNAPSHOT</version>
     </dependency>
 
     <dependency>
-      <groupId>commons-codec</groupId>
-      <artifactId>commons-codec</artifactId>
+      <groupId>org.apache.oltu.commons</groupId>
+      <artifactId>org.apache.oltu.commons.json</artifactId>
+      <version>1.0.0-SNAPSHOT</version>
     </dependency>
   </dependencies>
 

Modified: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/ClaimsSet.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/ClaimsSet.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/ClaimsSet.java (original)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/ClaimsSet.java Tue Oct 29 12:45:49 2013
@@ -20,12 +20,14 @@ import static java.lang.String.format;
 
 import java.util.Map;
 
+import org.apache.oltu.commons.json.CustomizableEntity;
+
 /**
  * Represents the Claims Set as defined in the 6.1 section of the JWT specification.
  *
  * @see http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06#section-6.1
  */
-public final class ClaimsSet extends JWTEntity {
+public final class ClaimsSet extends CustomizableEntity {
 
     /**
      * The {@code iss} JWT Claims Set parameter.

Modified: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/Header.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/Header.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/Header.java (original)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/Header.java Tue Oct 29 12:45:49 2013
@@ -20,12 +20,14 @@ import static java.lang.String.format;
 
 import java.util.Map;
 
+import org.apache.oltu.commons.json.CustomizableEntity;
+
 /**
  * Represents the Header as defined in the 6.1 section of the JWT specification.
  *
  * @see http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06#section-6.1
  */
-public final class Header extends JWTEntity {
+public final class Header extends CustomizableEntity {
 
     /**
      * The {@code typ} JWT Header parameter.

Modified: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWT.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWT.java?rev=1536690&r1=1536689&r2=1536690&view=diff
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWT.java (original)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWT.java Tue Oct 29 12:45:49 2013
@@ -21,6 +21,8 @@ import static java.lang.String.format;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
+import org.apache.oltu.commons.json.CustomizableBuilder;
+
 /**
  * This class contains constants used in the JWT implementation.
  *
@@ -101,7 +103,7 @@ public class JWT {
     /**
      * A simple {@link JWT} builder.
      */
-    public static final class Builder {
+    public static final class Builder extends CustomizableBuilder<JWT> {
 
         /**
          * The JWT raw string
@@ -182,7 +184,7 @@ public class JWT {
             this(null);
         }
 
-        public Builder(String rawString ) {
+        public Builder(String rawString) {
             this.rawString = rawString;
         }
 
@@ -226,7 +228,7 @@ public class JWT {
          * @param value value the custom field value.
          * @return this builder instance.
          */
-        public Builder setHeaderCustomField(String key, String value) {
+        public Builder setHeaderCustomField(String key, Object value) {
             if (key == null) {
                 throw new IllegalArgumentException("Null key not allowed");
             }
@@ -331,7 +333,7 @@ public class JWT {
          * @param value value the custom field value.
          * @return this builder instance.
          */
-        public Builder setClaimsSetCustomField(String key, String value) {
+        public Builder setClaimsSetCustomField(String key, Object value) {
             if (key == null) {
                 throw new IllegalArgumentException("Null key not allowed");
             }

Added: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetParser.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetParser.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetParser.java (added)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetParser.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.oauth2.jwt.io;
+
+import org.apache.oltu.commons.json.CustomizableEntityReader;
+import org.apache.oltu.oauth2.jwt.JWT;
+
+final class JWTClaimsSetParser extends CustomizableEntityReader<JWT, JWT.Builder> implements JWTConstants {
+
+    public JWTClaimsSetParser(JWT.Builder builder) {
+        super(builder);
+    }
+
+    @Override
+    protected <T> boolean handleProperty(String key, T value) {
+        if (AUDIENCE.equals(key)) {
+            getBuilder().setClaimsSetAudience(String.valueOf(value));
+        } else if (EXPIRATION_TIME.equals(key)) {
+            getBuilder().setClaimsSetExpirationTime(((Integer) value).longValue());
+        } else if (ISSUED_AT.equals(key)) {
+            getBuilder().setClaimsSetIssuedAt(((Integer) value).longValue());
+        } else if (ISSUER.equals(key)) {
+            getBuilder().setClaimsSetIssuer(String.valueOf(value));
+        } else if (JWT_ID.equals(key)) {
+            getBuilder().setClaimsSetJwdId(String.valueOf(value));
+        } else if (NOT_BEFORE.equals(key)) {
+            getBuilder().setClaimsSetNotBefore(String.valueOf(value));
+        } else if (SUBJECT.equals(key)) {
+            getBuilder().setClaimsSetSubject(String.valueOf(value));
+        } else if (TYPE.equals(key)) {
+            getBuilder().setClaimsSetType(String.valueOf(value));
+        } else {
+            getBuilder().setClaimsSetCustomField(key, value);
+        }
+
+        return true;
+    }
+
+}

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetParser.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetParser.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetWriter.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetWriter.java?rev=1536690&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetWriter.java (added)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetWriter.java Tue Oct 29 12:45:49 2013
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.oauth2.jwt.io;
+
+import org.apache.oltu.commons.json.CustomizableEntityWriter;
+import org.apache.oltu.oauth2.jwt.ClaimsSet;
+
+public final class JWTClaimsSetWriter extends CustomizableEntityWriter<ClaimsSet> implements JWTConstants {
+
+    @Override
+    protected void handleProperties(ClaimsSet claimsSet) {
+        set(AUDIENCE, claimsSet.getAudience());
+        set(ISSUER, claimsSet.getIssuer());
+        set(JWT_ID, claimsSet.getJwdId());
+        set(NOT_BEFORE, claimsSet.getNotBefore());
+        set(SUBJECT, claimsSet.getSubject());
+        set(TYPE, claimsSet.getType());
+        set(EXPIRATION_TIME, claimsSet.getExpirationTime());
+        set(ISSUED_AT, claimsSet.getIssuedAt());
+    }
+
+}

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetWriter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTClaimsSetWriter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTConstants.java (from r1536305, oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java)
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTConstants.java?p2=oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTConstants.java&p1=oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java&r1=1536305&r2=1536690&rev=1536690&view=diff
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java (original)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTConstants.java Tue Oct 29 12:45:49 2013
@@ -16,23 +16,15 @@
  */
 package org.apache.oltu.oauth2.jwt.io;
 
-import java.nio.charset.Charset;
 
-import org.apache.commons.codec.binary.Base64;
-
-abstract class AbstractJWTIO {
-
-    /**
-     * The {@code UTF-8} charset reference.
-     */
-    protected static final Charset UTF_8 = Charset.forName("UTF-8");
+interface JWTConstants {
 
     // header defined in the JWT specification
 
     /**
      * The {@code typ} JWT Header key.
      */
-    protected static final String TYPE = "typ";
+    public static final String TYPE = "typ";
 
     /**
      * The {@code alg} JWT Header key.
@@ -49,41 +41,36 @@ abstract class AbstractJWTIO {
     /**
      * The {@code iss} JWT Claims Set key.
      */
-    protected static final String ISSUER = "iss";
+    public static final String ISSUER = "iss";
 
     /**
      * The {@code sub} JWT Claims Set key.
      */
-    protected static final String SUBJECT = "sub";
+    public static final String SUBJECT = "sub";
 
     /**
      * The {@code aud} JWT Claims Set key.
      */
-    protected static final String AUDIENCE = "aud";
+    public static final String AUDIENCE = "aud";
 
     /**
      * The {@code exp} JWT Claims Set key.
      */
-    protected static final String EXPIRATION_TIME = "exp";
+    public static final String EXPIRATION_TIME = "exp";
 
     /**
      * The {@code nbf} JWT Claims Set key.
      */
-    protected static final String NOT_BEFORE = "nbf";
+    public static final String NOT_BEFORE = "nbf";
 
     /**
      * The {@code iat} JWT Claims Set key.
      */
-    protected static final String ISSUED_AT = "iat";
+    public static final String ISSUED_AT = "iat";
 
     /**
      * The {@code jti} JWT Claims Set key.
      */
-    protected static final String JWT_ID = "jti";
-
-    /**
-     * The BASE64 encoder/decoder.
-     */
-    protected final Base64 base64 = new Base64(true);
+    public static final String JWT_ID = "jti";
 
 }