├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── actions ├── pom.xml └── src │ ├── examples │ └── com │ │ └── ibm │ │ └── common │ │ └── activitystreams │ │ └── actions │ │ └── example │ │ └── Actions.java │ ├── main │ └── java │ │ └── com │ │ └── ibm │ │ └── common │ │ └── activitystreams │ │ └── actions │ │ ├── ActionHandler.java │ │ ├── ActionMakers.java │ │ ├── ActionObjectAdapter.java │ │ ├── ActionsModule.java │ │ ├── Adapters.java │ │ ├── Authentication.java │ │ ├── EmbedActionHandler.java │ │ ├── HtmlForm.java │ │ ├── HttpActionHandler.java │ │ ├── IntentActionHandler.java │ │ ├── Parameter.java │ │ ├── ParameterAdapter.java │ │ ├── ParameterValue.java │ │ ├── ParametersAdapter.java │ │ ├── ParametersValue.java │ │ ├── StylesValue.java │ │ ├── TypedPayload.java │ │ └── UrlTemplate.java │ └── test │ └── java │ └── com │ └── ibm │ └── common │ └── activitystreams │ └── actions │ └── test │ └── ActionsTest.java ├── assembly ├── assembly.xml └── pom.xml ├── core ├── TODO.md ├── pom.xml └── src │ ├── examples │ └── com │ │ └── ibm │ │ └── common │ │ └── activitystreams │ │ └── examples │ │ ├── Simple.java │ │ ├── Simple2.java │ │ ├── Simple3.java │ │ ├── Simple4.java │ │ └── Simple5.java │ ├── main │ └── java │ │ └── com │ │ └── ibm │ │ └── common │ │ └── activitystreams │ │ ├── ASObject.java │ │ ├── ActionsValue.java │ │ ├── Activity.java │ │ ├── Collection.java │ │ ├── IO.java │ │ ├── LinkValue.java │ │ ├── Makers.java │ │ ├── NLV.java │ │ ├── TypeValue.java │ │ ├── ValueType.java │ │ ├── Writable.java │ │ ├── internal │ │ ├── ASObjectAdapter.java │ │ ├── Adapter.java │ │ ├── Adapters.java │ │ ├── EnumAdapter.java │ │ ├── GsonWrapper.java │ │ ├── LazilyParsedNumberComparable.java │ │ ├── LinkValueAdapter.java │ │ ├── Model.java │ │ ├── MultimapAdapter.java │ │ ├── NaturalLanguageValueAdapter.java │ │ ├── Schema.java │ │ ├── SimpleAdapter.java │ │ └── TypeValueAdapter.java │ │ └── util │ │ ├── AbstractDictionaryObject.java │ │ ├── AbstractDictionaryObjectAdapter.java │ │ ├── AbstractWritable.java │ │ ├── Converters.java │ │ ├── Module.java │ │ └── Util.java │ └── test │ ├── java │ └── com │ │ └── ibm │ │ └── common │ │ └── activitystreams │ │ └── test │ │ ├── TestBasics.java │ │ └── TestIO.java │ └── resources │ └── data │ └── 1.json ├── geo ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── ibm │ └── common │ └── geojson │ ├── AS1Position.java │ ├── Address.java │ ├── BoundingBox.java │ ├── CRS.java │ ├── Feature.java │ ├── FeatureCollection.java │ ├── GeoMakers.java │ ├── GeoObject.java │ ├── Geometry.java │ ├── GeometryCollection.java │ ├── LineString.java │ ├── MultiLineString.java │ ├── MultiPoint.java │ ├── MultiPolygon.java │ ├── Place.java │ ├── Point.java │ ├── Polygon.java │ ├── Position.java │ └── as2 │ ├── GeoAdapter.java │ ├── GeoModule.java │ └── GeoObjectAdapter.java ├── legacy ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── ibm │ └── common │ └── activitystreams │ └── legacy │ ├── AudioVisual.java │ ├── Binary.java │ ├── Bookmark.java │ ├── Compression.java │ ├── Event.java │ ├── File.java │ ├── Issue.java │ ├── LegacyMakers.java │ ├── LegacyModule.java │ ├── LegacyObjectAdapter.java │ ├── MediaLink.java │ ├── MediaLinkAdapter.java │ ├── Membership.java │ ├── Question.java │ ├── Task.java │ └── WithImage.java ├── pom.xml └── typext ├── EXPERIMENTAL ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── ibm │ └── common │ └── activitystreams │ ├── ext │ ├── ExtAdapter.java │ ├── ExtMakers.java │ ├── ExtModule.java │ ├── ObjectType.java │ └── Verb.java │ └── registry │ ├── CachingResolutionStrategy.java │ ├── ClasspathPreloader.java │ ├── DefaultResolutionStrategy.java │ ├── HttpFetch.java │ ├── NonOpResolutionStrategy.java │ ├── PreloadStrategy.java │ ├── Receiver.java │ ├── ResolutionStrategy.java │ ├── TypeValueRegistry.java │ └── UncacheableResponseException.java └── test ├── java └── com │ └── ibm │ └── common │ └── activitystreams │ └── ext │ └── test │ └── ExtTest.java └── resources └── typeValues.json /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .settings 4 | target 5 | */target 6 | */.classpath 7 | */.project 8 | */.settings 9 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | CONTRIBUTING 2 | ============ 3 | 4 | To contribute, 5 | 6 | 1. Electronically sign the OpenSocial Incubator Contributor License Agreement (https://docs.google.com/forms/d/1b9tfsUJAs1xF7uyFwpT8yoVH7scOj8uXwQnZdTHHCTM/viewform) 7 | 1. Fork this repository. 8 | 1. Work on your changes within a topic branch. 9 | 1. Create a pull request 10 | 11 | Pull requests are accepted under the discretion of the Incubator maintainer(s). Improve the chances your PR will be merged by following the requirements and guidelines below. 12 | 13 | **REQUIREMENTS** 14 | - You must have signed the OpenSocial Incubator CLA and included your Github ID. 15 | 16 | **GUIDELINES** 17 | - Make sure your pull contains quality code. We will certainly provide constructive feedback on works in progress but we will not merge incomplete pull requests. 18 | - Make sure your pull has automated tests. For Java, we have been using JUnit 4.11. 19 | - Make sure your pull is fully documented. 20 | - Reference related Github Issues within commit comments and pull request comment where appropriate. 21 | - Squash out minor or "typo" commits in your pull using `git commit -a` or `git rebase -i`. 22 | 23 | **BUGS** 24 | 25 | File bugs or feature requests using Github Issue Tracker. 26 | 27 | https://github.com/OpenSocial/activitystreams/issues 28 | 29 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/ActionObjectAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import java.lang.reflect.Type; 25 | 26 | import com.google.common.collect.ImmutableSet; 27 | import com.ibm.common.activitystreams.ASObject.AbstractBuilder; 28 | import com.ibm.common.activitystreams.internal.ASObjectAdapter; 29 | import com.ibm.common.activitystreams.internal.Model; 30 | import com.ibm.common.activitystreams.internal.Schema; 31 | 32 | public final class ActionObjectAdapter 33 | extends ASObjectAdapter { 34 | 35 | ActionObjectAdapter(Schema schema) { 36 | super(schema); 37 | } 38 | 39 | private static final ImmutableSet> knownTypes = 40 | ImmutableSet.>of( 41 | HttpActionHandler.class, 42 | IntentActionHandler.class, 43 | EmbedActionHandler.class, 44 | HtmlForm.class, 45 | UrlTemplate.class, 46 | TypedPayload.class 47 | ); 48 | 49 | @Override 50 | protected boolean knowsType(Type type) { 51 | if (super.knowsType(type)) 52 | return true; 53 | return knownTypes.contains(type); 54 | } 55 | 56 | @Override 57 | protected AbstractBuilder builderFor(Type type) { 58 | if (super.knowsType(type)) 59 | return super.builderFor(type); 60 | if (type == HttpActionHandler.class) 61 | return ActionMakers.httpAction(); 62 | else if (type == IntentActionHandler.class) 63 | return ActionMakers.intentAction(); 64 | else if (type == EmbedActionHandler.class) 65 | return ActionMakers.embedAction(); 66 | else if (type == HtmlForm.class) 67 | return ActionMakers.htmlForm(); 68 | else if (type == UrlTemplate.class) 69 | return ActionMakers.urlTemplate(); 70 | else if (type == TypedPayload.class) 71 | return ActionMakers.typedPayload(); 72 | else return null; 73 | } 74 | 75 | @Override 76 | protected Model modelFor(Type type) { 77 | if (super.knowsType(type)) 78 | return super.modelFor(type); 79 | if (type == HttpActionHandler.class) 80 | return schema().forObjectClassOrType( 81 | HttpActionHandler.Builder.class, 82 | "HttpActionHandler"); 83 | else if (type == IntentActionHandler.class) 84 | return schema().forObjectClassOrType( 85 | IntentActionHandler.Builder.class, 86 | "IntentActionHandler"); 87 | else if (type == EmbedActionHandler.class) 88 | return schema().forObjectClassOrType( 89 | EmbedActionHandler.Builder.class, 90 | "EmbedActionHandler"); 91 | else if (type == HtmlForm.class) 92 | return schema().forObjectClassOrType( 93 | HtmlForm.Builder.class, 94 | "HtmlForm"); 95 | else if (type == UrlTemplate.class) 96 | return schema().forObjectClassOrType( 97 | UrlTemplate.Builder.class, 98 | "UrlTemplate"); 99 | else if (type == TypedPayload.class) 100 | return schema().forObjectClassOrType( 101 | TypedPayload.Builder.class, 102 | "TypedPayload"); 103 | else return null; 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/ActionsModule.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import static com.ibm.common.activitystreams.actions.Adapters.AUTH; 25 | import static com.ibm.common.activitystreams.actions.Adapters.STYLES; 26 | 27 | import com.ibm.common.activitystreams.IO.Builder; 28 | import com.ibm.common.activitystreams.internal.Model; 29 | import com.ibm.common.activitystreams.internal.Schema; 30 | import com.ibm.common.activitystreams.util.Module; 31 | 32 | public final class ActionsModule 33 | implements Module { 34 | 35 | public static final Module instance = new ActionsModule(); 36 | 37 | @Override 38 | public void apply(Builder builder, Schema schema) { 39 | 40 | ActionObjectAdapter base = 41 | new ActionObjectAdapter(schema); 42 | 43 | // Register Adapters 44 | builder.hierarchicalAdapter(ActionHandler.class,base) 45 | .hierarchicalAdapter(HttpActionHandler.class,base) 46 | .hierarchicalAdapter(IntentActionHandler.class,base) 47 | .hierarchicalAdapter(EmbedActionHandler.class,base) 48 | .hierarchicalAdapter(HtmlForm.class,base) 49 | .hierarchicalAdapter(ParametersValue.class, ParametersAdapter.instance) 50 | .hierarchicalAdapter(ParameterValue.class, ParameterAdapter.instance) 51 | .hierarchicalAdapter(UrlTemplate.class,base) 52 | .hierarchicalAdapter(TypedPayload.class,base) 53 | .hierarchicalAdapter(Authentication.class, AUTH) 54 | .hierarchicalAdapter(StylesValue.class, STYLES); 55 | } 56 | 57 | @Override 58 | public void apply(Schema.Builder builder) { 59 | builder.map("HtmlForm", withParameters.template(HtmlForm.class, HtmlForm.Builder.class)) 60 | .map("TypedPayload", typedPayload) 61 | .map("UrlTemplate", withParameters.template(UrlTemplate.class, UrlTemplate.Builder.class)) 62 | .map("HttpActionHandler", actionHandler.template(HttpActionHandler.class, HttpActionHandler.Builder.class)) 63 | .map("IntentActionHandler", actionHandler.template(IntentActionHandler.class, IntentActionHandler.Builder.class)) 64 | .map("EmbedActionHandler", actionHandler.template(EmbedActionHandler.class, EmbedActionHandler.Builder.class)); 65 | } 66 | 67 | public final static Model actionHandler = 68 | Model 69 | .make("object") 70 | .linkValue( 71 | "expects", 72 | "returns", 73 | "requires", 74 | "prefers") 75 | .object("context") 76 | .as("auth", Authentication.class) 77 | .as("style", StylesValue.class) 78 | .get(); 79 | 80 | 81 | public final static Model withParameters = 82 | Model 83 | .make("object") 84 | .as("parameters", ParametersValue.class) 85 | .get(); 86 | 87 | public final static Model typedPayload = 88 | Model 89 | .make("object") 90 | .type(TypedPayload.class, TypedPayload.Builder.class) 91 | .linkValue("schema") 92 | .typeValue("type") 93 | .get(); 94 | 95 | 96 | 97 | } 98 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import com.ibm.common.activitystreams.ASObject; 25 | import com.ibm.common.activitystreams.actions.StylesValue.Builder; 26 | import com.ibm.common.activitystreams.internal.Adapter; 27 | import com.ibm.common.activitystreams.util.AbstractDictionaryObjectAdapter; 28 | 29 | final class Adapters { 30 | 31 | private Adapters() {} 32 | 33 | static final Adapter AUTH = 34 | new AbstractDictionaryObjectAdapter 35 | (ASObject.class) { 38 | @Override 39 | protected Authentication.Builder builder() { 40 | return Authentication.make(); 41 | } 42 | }; 43 | 44 | static final Adapter STYLES = 45 | new AbstractDictionaryObjectAdapter 46 | (String.class) { 49 | @Override 50 | protected Builder builder() { 51 | return StylesValue.make(); 52 | } 53 | }; 54 | 55 | } 56 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/Authentication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import java.io.Serializable; 25 | import java.util.Map; 26 | 27 | import com.google.common.collect.ImmutableMap; 28 | import com.ibm.common.activitystreams.ASObject; 29 | import com.ibm.common.activitystreams.util.AbstractDictionaryObject; 30 | 31 | /** 32 | */ 33 | public final class Authentication 34 | extends AbstractDictionaryObject { 35 | 36 | /** 37 | * Method make. 38 | * @return Builder 39 | */ 40 | public static Builder make() { 41 | return new Builder(); 42 | } 43 | 44 | /** 45 | */ 46 | public static final class Builder 47 | extends AbstractDictionaryObject.AbstractBuilder< 48 | ASObject,Authentication,Builder> { 49 | 50 | public Builder() { 51 | writeUsing(ActionMakers.io); 52 | } 53 | 54 | /** 55 | * Method get. 56 | * @return Authentication 57 | * @see com.google.common.base.Supplier#get() 58 | */ 59 | public Authentication get() { 60 | return new Authentication(this); 61 | } 62 | 63 | } 64 | 65 | /** 66 | * Constructor for Authentication. 67 | * @param builder Builder 68 | */ 69 | protected Authentication(Builder builder) { 70 | super(builder); 71 | } 72 | 73 | /** 74 | * Method get. 75 | * @param key String 76 | * @return A 77 | */ 78 | @SuppressWarnings("unchecked") 79 | public A get(String key) { 80 | return (A)this.getSingle(key); 81 | } 82 | 83 | Object writeReplace() throws java.io.ObjectStreamException { 84 | return new SerializedForm(this); 85 | } 86 | 87 | private static class SerializedForm 88 | implements Serializable { 89 | private static final long serialVersionUID = -1975376657749952999L; 90 | private ImmutableMap map; 91 | SerializedForm(Authentication obj) { 92 | ImmutableMap.Builder builder = 93 | ImmutableMap.builder(); 94 | for (String key : obj) 95 | builder.put(key, obj.get(key)); 96 | this.map = builder.build(); 97 | } 98 | 99 | Object readResolve() 100 | throws java.io.ObjectStreamException { 101 | Authentication.Builder builder = 102 | Authentication.make(); 103 | for (Map.Entry entry : map.entrySet()) 104 | builder.set(entry.getKey(), (ASObject) entry.getValue()); 105 | return builder.get(); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.base.Supplier; 27 | import com.ibm.common.activitystreams.ASObject; 28 | 29 | /** 30 | */ 31 | public final class HtmlForm 32 | extends ASObject { 33 | 34 | /** 35 | * Method makeHtmlForm. 36 | * @return Builder 37 | */ 38 | public static Builder makeHtmlForm() { 39 | return new Builder(); 40 | } 41 | 42 | /** 43 | */ 44 | public static final class Builder 45 | extends ASObject.AbstractBuilder { 46 | 47 | private final ParametersValue.Builder params = 48 | ParametersValue.make(); 49 | 50 | public Builder() { 51 | writeUsing(ActionMakers.io); 52 | objectType("HtmlForm"); 53 | mediaType("application/x-www-form-urlencoded"); 54 | } 55 | 56 | public Builder parameter(String name, String iri) { 57 | params.param(name, iri); 58 | return this; 59 | } 60 | 61 | public Builder parameter( 62 | String name, 63 | ParameterValue parameter) { 64 | params.param(name, parameter); 65 | return this; 66 | } 67 | 68 | public Builder parameter( 69 | String name, 70 | Supplier parameter) { 71 | return parameter(name, parameter.get()); 72 | } 73 | 74 | /** 75 | * Method get. 76 | * @return HtmlForm 77 | * @see com.google.common.base.Supplier#get() 78 | */ 79 | public HtmlForm get() { 80 | if (params.notEmpty()) 81 | set("parameters", params.get()); 82 | return new HtmlForm(this); 83 | } 84 | 85 | } 86 | 87 | /** 88 | * Constructor for HtmlForm. 89 | * @param builder Builder 90 | */ 91 | private HtmlForm(Builder builder) { 92 | super(builder); 93 | } 94 | 95 | /** 96 | * Method parameters. 97 | * @return ParametersValue 98 | */ 99 | public ParametersValue parameters() { 100 | return this.get("parameters"); 101 | } 102 | 103 | Object writeReplace() throws java.io.ObjectStreamException { 104 | return new SerializedForm(this); 105 | } 106 | 107 | private static class SerializedForm 108 | extends AbstractSerializedForm { 109 | private static final long serialVersionUID = -2060301713159936285L; 110 | protected SerializedForm(HtmlForm obj) { 111 | super(obj); 112 | } 113 | Object readResolve() throws ObjectStreamException { 114 | return super.doReadResolve(); 115 | } 116 | protected HtmlForm.Builder builder() { 117 | return ActionMakers.htmlForm(); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/HttpActionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | 27 | /** 28 | */ 29 | public class HttpActionHandler 30 | extends ActionHandler { 31 | 32 | /** 33 | * Method makeHttpActionHandler. 34 | * @return Builder 35 | */ 36 | public static Builder makeHttpActionHandler() { 37 | return new Builder(); 38 | } 39 | 40 | /** 41 | */ 42 | public static final class Builder 43 | extends AbstractBuilder { 44 | 45 | public Builder() { 46 | super("HttpActionHandler"); 47 | } 48 | 49 | /** 50 | * Method actualGet. 51 | * @return HttpActionHandler 52 | */ 53 | protected HttpActionHandler actualGet() { 54 | return new HttpActionHandler(this); 55 | } 56 | 57 | } 58 | 59 | /** 60 | */ 61 | @SuppressWarnings("unchecked") 62 | public static abstract class AbstractBuilder 63 | > 64 | extends ActionHandler.Builder { 65 | /** 66 | * Constructor for AbstractBuilder. 67 | * @param objectType String 68 | */ 69 | protected AbstractBuilder(String objectType) { 70 | objectType(objectType); 71 | } 72 | 73 | /** 74 | * Method method. 75 | * @param method String 76 | * @return B 77 | */ 78 | public B method(String method) { 79 | set("method", method); 80 | return (B)this; 81 | } 82 | 83 | /** 84 | * Method target. 85 | * @param target String 86 | * @return B 87 | */ 88 | public B target(String target) { 89 | set("target", target); 90 | return (B)this; 91 | } 92 | } 93 | 94 | /** 95 | * Constructor for HttpActionHandler. 96 | * @param builder AbstractBuilder 97 | */ 98 | protected HttpActionHandler(AbstractBuilder builder) { 99 | super(builder); 100 | } 101 | 102 | /** 103 | * Method method. 104 | * @return String 105 | */ 106 | public String method() { 107 | return this.getString("method"); 108 | } 109 | 110 | /** 111 | * Method target. 112 | * @return String 113 | */ 114 | public String target() { 115 | return has("target") ? 116 | this.get("target") : 117 | ActionMakers.TARGET_DEFAULT; 118 | } 119 | 120 | Object writeReplace() throws java.io.ObjectStreamException { 121 | return new SerializedForm(this); 122 | } 123 | 124 | private static class SerializedForm 125 | extends AbstractSerializedForm { 126 | private static final long serialVersionUID = -2060301713159936285L; 127 | protected SerializedForm(HttpActionHandler obj) { 128 | super(obj); 129 | } 130 | Object readResolve() throws ObjectStreamException { 131 | return super.doReadResolve(); 132 | } 133 | protected HttpActionHandler.Builder builder() { 134 | return ActionMakers.httpAction(); 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/IntentActionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | 27 | /** 28 | */ 29 | public class IntentActionHandler 30 | extends ActionHandler { 31 | 32 | /** 33 | * Method makeIntentActionHandler. 34 | * @return Builder 35 | */ 36 | public static Builder makeIntentActionHandler() { 37 | return new Builder(); 38 | } 39 | 40 | /** 41 | */ 42 | public static final class Builder 43 | extends AbstractBuilder { 44 | 45 | public Builder() { 46 | super("IntentActionHandler"); 47 | } 48 | 49 | /** 50 | * Method actualGet. 51 | * @return IntentActionHandler 52 | */ 53 | protected IntentActionHandler actualGet() { 54 | return new IntentActionHandler(this); 55 | } 56 | 57 | } 58 | 59 | /** 60 | */ 61 | public static abstract class AbstractBuilder 62 | > 63 | extends ActionHandler.Builder { 64 | /** 65 | * Constructor for AbstractBuilder. 66 | * @param objectType String 67 | */ 68 | protected AbstractBuilder(String objectType) { 69 | objectType(objectType); 70 | } 71 | } 72 | 73 | /** 74 | * Constructor for IntentActionHandler. 75 | * @param builder AbstractBuilder 76 | */ 77 | protected IntentActionHandler(AbstractBuilder builder) { 78 | super(builder); 79 | } 80 | 81 | Object writeReplace() throws java.io.ObjectStreamException { 82 | return new SerializedForm(this); 83 | } 84 | 85 | private static class SerializedForm 86 | extends AbstractSerializedForm { 87 | private static final long serialVersionUID = -2060301713159936285L; 88 | protected SerializedForm(IntentActionHandler obj) { 89 | super(obj); 90 | } 91 | Object readResolve() throws ObjectStreamException { 92 | return super.doReadResolve(); 93 | } 94 | protected IntentActionHandler.Builder builder() { 95 | return ActionMakers.intentAction(); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/ParameterValue.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.actions; 2 | 3 | import java.io.Serializable; 4 | 5 | import com.google.common.base.Objects; 6 | import com.ibm.common.activitystreams.IO; 7 | import com.ibm.common.activitystreams.ValueType; 8 | import com.ibm.common.activitystreams.Writable; 9 | import com.ibm.common.activitystreams.util.AbstractWritable; 10 | 11 | public interface ParameterValue 12 | extends Writable, Serializable { 13 | 14 | ValueType valueType(); 15 | 16 | String toString(IO io); 17 | 18 | public static final class SimpleParameterValue 19 | extends AbstractWritable 20 | implements ParameterValue { 21 | 22 | static final class Builder 23 | extends AbstractWritable.AbstractWritableBuilder { 24 | 25 | private String type; 26 | 27 | public Builder() { 28 | writeUsing(ActionMakers.io); 29 | } 30 | 31 | public Builder type(String type) { 32 | this.type = type; 33 | return this; 34 | } 35 | 36 | @Override 37 | public SimpleParameterValue get() { 38 | return new SimpleParameterValue(this); 39 | } 40 | 41 | } 42 | 43 | private final String type; 44 | 45 | private SimpleParameterValue(Builder b) { 46 | super(b); 47 | this.type = b.type; 48 | } 49 | 50 | public ValueType valueType() { 51 | return ValueType.SIMPLE; 52 | } 53 | 54 | public String type() { 55 | return type; 56 | } 57 | 58 | @Override 59 | public int hashCode() { 60 | return Objects.hashCode(type); 61 | } 62 | 63 | @Override 64 | public boolean equals(Object obj) { 65 | if (this == obj) 66 | return true; 67 | if (obj == null) 68 | return false; 69 | if (getClass() != obj.getClass()) 70 | return false; 71 | SimpleParameterValue other = (SimpleParameterValue) obj; 72 | return Objects.equal(type,other.type); 73 | } 74 | 75 | public String toString(IO io) { 76 | return super.toString(io); 77 | } 78 | 79 | Object writeReplace() throws java.io.ObjectStreamException { 80 | return new SerializedForm(this); 81 | } 82 | 83 | private static class SerializedForm 84 | implements Serializable { 85 | private static final long serialVersionUID = -1975376657749952999L; 86 | private String type; 87 | SerializedForm(SimpleParameterValue obj) { 88 | this.type = obj.type; 89 | } 90 | 91 | Object readResolve() 92 | throws java.io.ObjectStreamException { 93 | return new SimpleParameterValue.Builder().type(type).get(); 94 | } 95 | } 96 | 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersAdapter.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.actions; 2 | 3 | import java.lang.reflect.Type; 4 | import java.util.Map; 5 | 6 | import static com.google.common.base.Preconditions.checkArgument; 7 | 8 | import com.google.gson.JsonDeserializationContext; 9 | import com.google.gson.JsonElement; 10 | import com.google.gson.JsonObject; 11 | import com.google.gson.JsonParseException; 12 | import com.google.gson.JsonSerializationContext; 13 | import com.ibm.common.activitystreams.internal.Adapter; 14 | 15 | public final class ParametersAdapter 16 | extends Adapter { 17 | 18 | public static final ParametersAdapter instance = 19 | new ParametersAdapter(); 20 | 21 | @Override 22 | public JsonElement serialize( 23 | ParametersValue params, 24 | Type type, 25 | JsonSerializationContext context) { 26 | if (params == null) return null; 27 | JsonObject obj = new JsonObject(); 28 | for (String param : params) { 29 | obj.add( 30 | param, 31 | context.serialize( 32 | params.get(param), 33 | ParameterValue.class)); 34 | } 35 | return obj; 36 | } 37 | 38 | @Override 39 | public ParametersValue deserialize( 40 | JsonElement json, 41 | Type type, 42 | JsonDeserializationContext context) 43 | throws JsonParseException { 44 | checkArgument(json.isJsonObject()); 45 | JsonObject obj = json.getAsJsonObject(); 46 | ParametersValue.Builder builder = 47 | ParametersValue.make(); 48 | for (Map.Entry entry : obj.entrySet()) 49 | builder.param( 50 | entry.getKey(), 51 | context.deserialize( 52 | entry.getValue(), 53 | ParameterValue.class)); 54 | return builder.get(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersValue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import java.io.Serializable; 25 | import java.util.Iterator; 26 | import java.util.Map; 27 | 28 | import com.google.common.base.Supplier; 29 | import com.google.common.collect.ImmutableMap; 30 | import com.google.common.collect.Maps; 31 | import com.ibm.common.activitystreams.util.AbstractWritable; 32 | 33 | /** 34 | * The value of the "parameters" property... 35 | * @author james 36 | * @version $Revision: 1.0 $ 37 | */ 38 | public final class ParametersValue 39 | extends AbstractWritable 40 | implements Serializable, Iterable { 41 | 42 | public static Builder make() { 43 | return new Builder(); 44 | } 45 | 46 | /** 47 | * @author james 48 | * @version $Revision: 1.0 $ 49 | */ 50 | public static final class Builder 51 | extends AbstractWritable.AbstractWritableBuilder 52 | { 53 | 54 | private final Map params = 55 | Maps.newHashMap(); 56 | 57 | public Builder() { 58 | writeUsing(ActionMakers.io); 59 | } 60 | 61 | public boolean notEmpty() { 62 | return !params.isEmpty(); 63 | } 64 | 65 | public Builder param(String param, String iri) { 66 | params.put(param, ActionMakers.parameter(iri)); 67 | return this; 68 | } 69 | 70 | public Builder param( 71 | String param, 72 | ParameterValue parameter) { 73 | params.put(param, parameter); 74 | return this; 75 | } 76 | 77 | public Builder param( 78 | String param, 79 | Supplier parameter) { 80 | params.put(param, parameter); 81 | return this; 82 | } 83 | 84 | public ParametersValue get() { 85 | return new ParametersValue(this); 86 | } 87 | 88 | } 89 | 90 | private final ImmutableMap params; 91 | 92 | /** 93 | * Constructor for ParametersValue. 94 | * @param builder Builder 95 | */ 96 | ParametersValue(Builder builder) { 97 | super(builder); 98 | this.params = ImmutableMap.copyOf(builder.params); 99 | } 100 | 101 | public Iterator iterator() { 102 | return params.keySet().iterator(); 103 | } 104 | 105 | @SuppressWarnings("unchecked") 106 | public O get(String param) { 107 | return (O)params.get(param); 108 | } 109 | 110 | public boolean has(String param) { 111 | return params.containsKey(param); 112 | } 113 | 114 | Object writeReplace() throws java.io.ObjectStreamException { 115 | return new SerializedForm(this); 116 | } 117 | 118 | private static class SerializedForm 119 | implements Serializable { 120 | private static final long serialVersionUID = -1975376657749952999L; 121 | private ImmutableMap map; 122 | SerializedForm(ParametersValue obj) { 123 | map = obj.params; 124 | } 125 | 126 | Object readResolve() 127 | throws java.io.ObjectStreamException { 128 | ParametersValue.Builder builder = 129 | ParametersValue.make(); 130 | builder.params.putAll(map); 131 | return builder.get(); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import static com.google.common.base.Preconditions.checkArgument; 25 | 26 | import java.io.Serializable; 27 | import java.util.Map; 28 | 29 | import com.google.common.collect.ImmutableMap; 30 | import com.ibm.common.activitystreams.util.AbstractDictionaryObject; 31 | 32 | /** 33 | * The value of the "parameters" property... 34 | * @author james 35 | * @version $Revision: 1.0 $ 36 | */ 37 | public final class StylesValue 38 | extends AbstractDictionaryObject 39 | implements Serializable { 40 | 41 | /** 42 | * Method make. 43 | 44 | * @return Builder */ 45 | public static Builder make() { 46 | return new Builder(); 47 | } 48 | 49 | 50 | /** 51 | * @author james 52 | * @version $Revision: 1.0 $ 53 | */ 54 | public static final class Builder 55 | extends AbstractDictionaryObject.AbstractBuilder 56 | { 57 | 58 | public Builder() { 59 | writeUsing(ActionMakers.io); 60 | } 61 | 62 | /** 63 | * Method media. 64 | * @param query String 65 | * @return Builder 66 | */ 67 | public Builder media(String query) { 68 | return super.set("media", query); 69 | } 70 | 71 | /** 72 | * Method set. 73 | 74 | 75 | 76 | * @param name String 77 | * @param value String 78 | * @return Builder */ 79 | public Builder set(String name, String value) { 80 | checkArgument(!name.equalsIgnoreCase("query")); 81 | return super.set(name, value); 82 | } 83 | 84 | /** 85 | * Method get. 86 | 87 | 88 | * @return ParametersValue * @see com.google.common.base.Supplier#get() */ 89 | public StylesValue get() { 90 | return new StylesValue(this); 91 | } 92 | 93 | } 94 | 95 | /** 96 | * Constructor for ParametersValue. 97 | * @param builder Builder 98 | */ 99 | StylesValue(Builder builder) { 100 | super(builder); 101 | } 102 | 103 | /** 104 | * Method media. 105 | * @return String 106 | */ 107 | public String media() { 108 | return getSingle("media"); 109 | } 110 | 111 | /** 112 | * Method get. 113 | * @param key String 114 | * @return String 115 | */ 116 | public String get(String key) { 117 | return super.getSingle(key); 118 | } 119 | 120 | /** 121 | * Method get. 122 | * @param key String 123 | * @param defaultValue String 124 | * @return String 125 | */ 126 | public String get(String key, String defaultValue) { 127 | return super.getSingle(key, defaultValue); 128 | } 129 | 130 | Object writeReplace() throws java.io.ObjectStreamException { 131 | return new SerializedForm(this); 132 | } 133 | 134 | private static class SerializedForm 135 | implements Serializable { 136 | private static final long serialVersionUID = -1975376657749952999L; 137 | private ImmutableMap map; 138 | SerializedForm(StylesValue obj) { 139 | ImmutableMap.Builder builder = 140 | ImmutableMap.builder(); 141 | for (String key : obj) 142 | builder.put(key, obj.get(key)); 143 | this.map = builder.build(); 144 | } 145 | 146 | Object readResolve() 147 | throws java.io.ObjectStreamException { 148 | StylesValue.Builder builder = 149 | StylesValue.make(); 150 | for (Map.Entry entry : map.entrySet()) 151 | builder.set(entry.getKey(), entry.getValue()); 152 | return builder.get(); 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /actions/src/main/java/com/ibm/common/activitystreams/actions/UrlTemplate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.actions; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.base.Supplier; 27 | import com.ibm.common.activitystreams.ASObject; 28 | 29 | /** 30 | */ 31 | public final class UrlTemplate 32 | extends ASObject 33 | implements ParameterValue { 34 | 35 | /** 36 | * Method makeUrlTemplate. 37 | * @return Builder 38 | */ 39 | public static Builder makeUrlTemplate() { 40 | return new Builder(); 41 | } 42 | 43 | /** 44 | */ 45 | public static final class Builder 46 | extends ASObject.AbstractBuilder { 47 | 48 | private final ParametersValue.Builder params = 49 | ParametersValue.make(); 50 | 51 | public Builder() { 52 | writeUsing(ActionMakers.io); 53 | objectType("UrlTemplate"); 54 | } 55 | 56 | public Builder template(String template) { 57 | set("template", template); 58 | return this; 59 | } 60 | 61 | public Builder parameter(String name, String iri) { 62 | params.param(name, iri); 63 | return this; 64 | } 65 | 66 | public Builder parameter( 67 | String name, 68 | ParameterValue parameter) { 69 | params.param(name, parameter); 70 | return this; 71 | } 72 | 73 | public Builder parameter( 74 | String name, 75 | Supplier parameter) { 76 | return parameter(name, parameter.get()); 77 | } 78 | 79 | /** 80 | * Method get. 81 | * @return UrlTemplate 82 | * @see com.google.common.base.Supplier#get() 83 | */ 84 | public UrlTemplate get() { 85 | if (params.notEmpty()) 86 | set("parameters", params.get()); 87 | return new UrlTemplate(this); 88 | } 89 | 90 | } 91 | 92 | /** 93 | * Constructor for UrlTemplate. 94 | * @param builder Builder 95 | */ 96 | private UrlTemplate(Builder builder) { 97 | super(builder); 98 | } 99 | 100 | /** 101 | * Method parameters. 102 | * @return ParametersValue 103 | */ 104 | public ParametersValue parameters() { 105 | return this.get("parameters"); 106 | } 107 | 108 | /** 109 | * Method template. 110 | * @return String 111 | */ 112 | public String template() { 113 | return this.getString("template"); 114 | } 115 | 116 | Object writeReplace() throws java.io.ObjectStreamException { 117 | return new SerializedForm(this); 118 | } 119 | 120 | private static class SerializedForm 121 | extends AbstractSerializedForm { 122 | private static final long serialVersionUID = -2060301713159936285L; 123 | protected SerializedForm(UrlTemplate obj) { 124 | super(obj); 125 | } 126 | Object readResolve() throws ObjectStreamException { 127 | return super.doReadResolve(); 128 | } 129 | protected UrlTemplate.Builder builder() { 130 | return ActionMakers.urlTemplate(); 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /assembly/assembly.xml: -------------------------------------------------------------------------------- 1 | 2 | bin 3 | 4 | tar.gz 5 | zip 6 | 7 | false 8 | 9 | 10 | 11 | 12 | * 13 | 14 | false 15 | 16 | 17 | -------------------------------------------------------------------------------- /assembly/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.ibm.common 5 | activitystreams 6 | 0.0.1-SNAPSHOT 7 | 8 | assembly 9 | Activity Streams 2.0 - Assembly 10 | pom 11 | 12 | 13 | com.ibm.common 14 | activitystreams-core 15 | 0.0.1-SNAPSHOT 16 | 17 | 18 | com.ibm.common 19 | activitystreams-actions 20 | 0.0.1-SNAPSHOT 21 | 22 | 23 | com.ibm.common 24 | activitystreams-geo 25 | 0.0.1-SNAPSHOT 26 | 27 | 28 | com.ibm.common 29 | activitystreams-legacy 30 | 0.0.1-SNAPSHOT 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-assembly-plugin 39 | 2.2.2 40 | 41 | 42 | assembly.xml 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /core/TODO.md: -------------------------------------------------------------------------------- 1 | # Development Todo's 2 | 3 | 1. Migrate from GSON to Jackson for JSON parsing 4 | 2. Improve Schema/PropertyMap implementation 5 | -------------------------------------------------------------------------------- /core/pom.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | com.ibm.common 8 | activitystreams 9 | 0.0.1-SNAPSHOT 10 | 11 | 12 | 4.0.0 13 | activitystreams-core 14 | jar 15 | 16 | Activity Streams 2.0 - Core 17 | http://github.org/opensocial/activitystreams 18 | 19 | 20 | 21 | Apache License, Version 2.0 22 | http://www.apache.org/licenses/LICENSE-2.0.txt 23 | repo 24 | 25 | 26 | 27 | 28 | UTF-8 29 | 30 | 31 | 32 | 33 | com.google.code.gson 34 | gson 35 | 36 | 37 | com.google.guava 38 | guava 39 | 40 | 41 | joda-time 42 | joda-time 43 | 44 | 45 | 46 | junit 47 | junit 48 | test 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-javadoc-plugin 57 | 2.9.1 58 | 59 | UTF-8 60 | UTF-8 61 | UTF-8 62 | -XDignore.symbol.file 63 | public 64 | 65 | http://www.joda.org/joda-time/apidocs 66 | http://docs.guava-libraries.googlecode.com/git-history/v16.0.1/javadoc/ 67 | 68 | 69 | 70 | 71 | 72 | maven-compiler-plugin 73 | 2.3.2 74 | 75 | 1.7 76 | 1.7 77 | 78 | 79 | 80 | 81 | maven-jar-plugin 82 | 2.3.1 83 | 84 | 85 | ${project.build.outputDirectory}/META-INF/MANIFEST.MF 86 | 87 | 88 | 89 | 90 | 91 | org.apache.felix 92 | maven-bundle-plugin 93 | 2.3.7 94 | true 95 | 96 | 97 | bundle-manifest 98 | process-classes 99 | 100 | manifest 101 | 102 | 103 | 104 | 105 | 106 | com.ibm.common.activitystreams.* 107 | 108 | com.google.gson.*, 109 | com.google.common.*, 110 | org.joda.time.* 111 | 112 | 113 | 114 | 115 | 116 | 117 | org.apache.maven.plugins 118 | maven-assembly-plugin 119 | 2.2.2 120 | 121 | 122 | assembly.xml 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /core/src/examples/com/ibm/common/activitystreams/examples/Simple.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.examples; 23 | 24 | import static com.ibm.common.activitystreams.IO.makeDefaultPrettyPrint; 25 | import static com.ibm.common.activitystreams.Makers.activity; 26 | 27 | import java.io.ByteArrayInputStream; 28 | import java.io.ByteArrayOutputStream; 29 | 30 | import com.ibm.common.activitystreams.Activity; 31 | import com.ibm.common.activitystreams.IO; 32 | 33 | /** 34 | * @author james 35 | * @version $Revision: 1.0 $ 36 | */ 37 | public final class Simple { 38 | 39 | // The IO object handles all of the reading and writing of the object 40 | private static final IO io = makeDefaultPrettyPrint(); 41 | 42 | private Simple() {} 43 | 44 | /** 45 | * Method main. 46 | * @param args String[] 47 | 48 | * @throws Exception */ 49 | public static void main(String... args) throws Exception { 50 | 51 | // Demonstrates the creation and parsing of a simple Activity Object 52 | 53 | 54 | // Create the Activity... The API uses a Fluent Generator pattern 55 | Activity activity = 56 | activity() 57 | .verb("post") 58 | .actor("acct:joe@example.org") 59 | .object("http://example.net/posts/1") 60 | .get(); 61 | 62 | 63 | // The Activity object is immutable... 64 | System.out.println(activity.verb()); 65 | System.out.println(activity.actor()); 66 | System.out.println(activity.object()); 67 | 68 | // let's write it out to our outputstream 69 | ByteArrayOutputStream out = 70 | new ByteArrayOutputStream(); 71 | activity.writeTo(out, io); 72 | 73 | // now let's parse it back in 74 | ByteArrayInputStream in = 75 | new ByteArrayInputStream( 76 | out.toByteArray()); 77 | 78 | activity = io.readAsActivity(in); 79 | 80 | // We get back the same thing... 81 | System.out.println(activity.verb()); 82 | System.out.println(activity.actor()); 83 | System.out.println(activity.object()); 84 | 85 | // If you want to see what was serialized, 86 | // simply write out to stdout... 87 | activity.writeTo(System.out, io); 88 | 89 | 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /core/src/examples/com/ibm/common/activitystreams/examples/Simple2.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.examples; 23 | 24 | /** 25 | * The Makers class includes a bunch of static generator 26 | * methods that are easiest to use when imported statically 27 | */ 28 | import static com.ibm.common.activitystreams.IO.makeDefaultPrettyPrint; 29 | import static com.ibm.common.activitystreams.Makers.activity; 30 | import static com.ibm.common.activitystreams.Makers.object; 31 | 32 | import java.io.ByteArrayInputStream; 33 | import java.io.ByteArrayOutputStream; 34 | 35 | import com.ibm.common.activitystreams.Activity; 36 | import com.ibm.common.activitystreams.IO; 37 | 38 | /** 39 | * @author james 40 | * @version $Revision: 1.0 $ 41 | */ 42 | public final class Simple2 { 43 | 44 | 45 | // The IO object handles all of the reading and writing of the object 46 | private static final IO io = makeDefaultPrettyPrint(); 47 | 48 | 49 | private Simple2() {} 50 | 51 | /** 52 | * Method main. 53 | * @param args String[] 54 | 55 | * @throws Exception */ 56 | public static void main(String... args) throws Exception { 57 | 58 | // Demonstrates the creation and parsing of a simple Activity Object 59 | 60 | // (we'll use this to store the output...) 61 | ByteArrayOutputStream out = 62 | new ByteArrayOutputStream(); 63 | 64 | // Create the Activity... The API uses a Fluent Generator pattern 65 | Activity activity = 66 | activity() 67 | .verb("post") 68 | .actor( 69 | object("person") 70 | .id("acct:joe@example.com") 71 | .displayName("Joe Smith") 72 | ) 73 | .object( 74 | object("note") 75 | .id("http://example.net/posts/1") 76 | .title("This is the title")) 77 | .get(); 78 | 79 | // The Activity object is immutable... 80 | System.out.println(activity.verb()); 81 | System.out.println(activity.actor()); 82 | System.out.println(activity.object()); 83 | 84 | // let's write it out to our outputstream 85 | activity.writeTo(out, io); 86 | 87 | // now let's parse it back in 88 | ByteArrayInputStream in = 89 | new ByteArrayInputStream( 90 | out.toByteArray()); 91 | 92 | activity = io.readAsActivity(in); 93 | 94 | // We get back the same thing... 95 | System.out.println(activity.verb()); 96 | System.out.println(activity.actor()); 97 | System.out.println(activity.object()); 98 | 99 | // If you want to see what was serialized, 100 | // simply write out to stdout... 101 | activity.writeTo(System.out, io); 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /core/src/examples/com/ibm/common/activitystreams/examples/Simple3.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.examples; 23 | 24 | /** 25 | * The Makers class includes a bunch of static generator 26 | * methods that are easiest to use when imported statically 27 | */ 28 | import static com.ibm.common.activitystreams.IO.makeDefaultPrettyPrint; 29 | import static com.ibm.common.activitystreams.Makers.*; 30 | 31 | import java.io.ByteArrayInputStream; 32 | import java.io.ByteArrayOutputStream; 33 | 34 | import com.ibm.common.activitystreams.Activity; 35 | import com.ibm.common.activitystreams.IO; 36 | 37 | /** 38 | * @author james 39 | * @version $Revision: 1.0 $ 40 | */ 41 | public final class Simple3 { 42 | 43 | // The IO object handles all of the reading and writing of the object 44 | private static final IO io = makeDefaultPrettyPrint(); 45 | 46 | private Simple3() {} 47 | 48 | /** 49 | * Method main. 50 | * @param args String[] 51 | 52 | * @throws Exception */ 53 | public static void main(String... args) throws Exception { 54 | 55 | // Demonstrates the creation and parsing of a simple Activity Object 56 | 57 | // (we'll use this to store the output...) 58 | ByteArrayOutputStream out = 59 | new ByteArrayOutputStream(); 60 | 61 | // Create the Activity... The API uses a Fluent Generator pattern 62 | Activity activity = 63 | activity() 64 | .verb("post") 65 | .actor( 66 | object() 67 | .objectType( 68 | object() 69 | .id("http://schema.example.net/Person") 70 | .displayName("Person") 71 | .alias("person")) 72 | .id("acct:joe@example.com") 73 | .displayName("Joe Smith") 74 | ) 75 | .object( 76 | object("note") 77 | .id("http://example.net/posts/1") 78 | .title( 79 | nlv() 80 | .set("en", "This is the title") 81 | .set("fr", "C'est le titre")) 82 | ) 83 | .get(); 84 | 85 | // The Activity object is immutable... 86 | System.out.println(activity.verb()); 87 | System.out.println(activity.actor()); 88 | System.out.println(activity.object()); 89 | 90 | // let's write it out to our outputstream 91 | activity.writeTo(out, io); 92 | 93 | // now let's parse it back in 94 | ByteArrayInputStream in = 95 | new ByteArrayInputStream( 96 | out.toByteArray()); 97 | 98 | activity = io.readAsActivity(in); 99 | 100 | // We get back the same thing... 101 | System.out.println(activity.verb()); 102 | System.out.println(activity.actor()); 103 | System.out.println(activity.object()); 104 | 105 | // If you want to see what was serialized, 106 | // simply write out to stdout... 107 | activity.writeTo(System.out, io); 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /core/src/examples/com/ibm/common/activitystreams/examples/Simple4.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.examples; 23 | 24 | /** 25 | * The Makers class includes a bunch of static generator 26 | * methods that are easiest to use when imported statically 27 | */ 28 | import static com.ibm.common.activitystreams.Activity.Audience.CC; 29 | import static com.ibm.common.activitystreams.Activity.Audience.TO; 30 | import static com.ibm.common.activitystreams.Makers.activity; 31 | import static com.ibm.common.activitystreams.Makers.nlv; 32 | import static com.ibm.common.activitystreams.Makers.object; 33 | 34 | import java.io.ByteArrayInputStream; 35 | import java.io.ByteArrayOutputStream; 36 | 37 | import com.ibm.common.activitystreams.Activity; 38 | import com.ibm.common.activitystreams.IO; 39 | 40 | /** 41 | * @author james 42 | * @version $Revision: 1.0 $ 43 | */ 44 | public final class Simple4 { 45 | 46 | private Simple4() {} 47 | 48 | /** 49 | * Method main. 50 | * @param args String[] 51 | 52 | * @throws Exception */ 53 | public static void main(String... args) throws Exception { 54 | 55 | // Demonstrates the creation and parsing of a simple Activity Object 56 | 57 | // (we'll use this to store the output...) 58 | ByteArrayOutputStream out = 59 | new ByteArrayOutputStream(); 60 | 61 | // The IO object handles all of the reading and writing of the object 62 | IO io = IO.make().prettyPrint().get(); 63 | 64 | // Create the Activity... The API uses a Fluent Generator pattern 65 | Activity activity = 66 | activity() 67 | .verb("post") 68 | .actor( 69 | object() 70 | .objectType( 71 | object() 72 | .id("http://schema.example.net/Person") 73 | .displayName("Person") 74 | .alias("person")) 75 | .id("acct:joe@example.com") 76 | .displayName("Joe Smith") 77 | ) 78 | .object( 79 | object("note") 80 | .id("http://example.net/posts/1") 81 | .title( 82 | nlv() 83 | .set("en", "This is the title") 84 | .set("fr", "C'est le titre")) 85 | ) 86 | .audience(TO, 87 | "urn:social:everyone", 88 | "acct:mary@example.net") 89 | .audience(CC, 90 | "urn:social:extended") 91 | .action("embed", "http://xml.example.org/foo") 92 | .pending() 93 | .get(); 94 | 95 | // The Activity object is immutable... 96 | System.out.println(activity.verb()); 97 | System.out.println(activity.actor()); 98 | System.out.println(activity.object()); 99 | System.out.println(activity.status()); 100 | 101 | // let's write it out to our outputstream 102 | activity.writeTo(out, io); 103 | 104 | // now let's parse it back in 105 | ByteArrayInputStream in = 106 | new ByteArrayInputStream( 107 | out.toByteArray()); 108 | 109 | activity = io.readAsActivity(in); 110 | 111 | // We get back the same thing... 112 | System.out.println(activity.verb()); 113 | System.out.println(activity.actor()); 114 | System.out.println(activity.object()); 115 | System.out.println(activity.status()); 116 | 117 | // If you want to see what was serialized, 118 | // simply write out to stdout... 119 | activity.writeTo(System.out, io); 120 | } 121 | 122 | } 123 | -------------------------------------------------------------------------------- /core/src/examples/com/ibm/common/activitystreams/examples/Simple5.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.examples; 23 | 24 | import static com.ibm.common.activitystreams.Makers.activity; 25 | 26 | import java.io.ByteArrayInputStream; 27 | import java.io.ByteArrayOutputStream; 28 | import java.io.ObjectInputStream; 29 | import java.io.ObjectOutputStream; 30 | 31 | import com.ibm.common.activitystreams.Activity; 32 | 33 | /** 34 | * @author james 35 | * @version $Revision: 1.0 $ 36 | */ 37 | public final class Simple5 { 38 | 39 | private Simple5() {} 40 | 41 | /** 42 | * Method main. 43 | * @param args String[] 44 | 45 | * @throws Exception */ 46 | public static void main(String... args) throws Exception { 47 | 48 | // Create the Activity... The API uses a Fluent Generator pattern 49 | Activity activity = 50 | activity() 51 | .verb("post") 52 | .actor("acct:joe@example.org") 53 | .object("http://example.net/posts/1") 54 | .action("like", "http://example.org/actions/like") 55 | .updatedNow() 56 | .get(); 57 | 58 | 59 | // The Activity object is immutable... 60 | System.out.println(activity.verb()); 61 | System.out.println(activity.actor()); 62 | System.out.println(activity.object()); 63 | 64 | // Activity objects are serializable 65 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 66 | ObjectOutputStream oos = new ObjectOutputStream(out); 67 | oos.writeObject(activity); 68 | 69 | ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 70 | ObjectInputStream ois = new ObjectInputStream(in); 71 | 72 | activity = (Activity) ois.readObject(); 73 | 74 | System.out.println(activity.verb()); 75 | System.out.println(activity.actor()); 76 | System.out.println(activity.object()); 77 | System.out.println(activity.actions().get("like")); 78 | System.out.println(activity.updated()); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/ValueType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams; 23 | 24 | /** 25 | * Used for TypeValue, LinkValue and NLV interfaces to distinguish 26 | * between the possible value options. 27 | * 28 | * LinkValue.valueType() can return SIMPLE, OBJECT or ARRAY 29 | * TypeValue.valueType() can return SIMPLE or OBJECT 30 | * NLV.valueType() can return SIMPLE or OBJECT 31 | */ 32 | public enum ValueType { 33 | SIMPLE, 34 | OBJECT, 35 | ARRAY; 36 | } 37 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/Writable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams; 23 | 24 | import java.io.OutputStream; 25 | import java.io.Writer; 26 | import java.util.concurrent.ExecutorService; 27 | import java.util.concurrent.Future; 28 | 29 | /** 30 | * Base interface for all objects that serialize to IO object instances 31 | * @author james 32 | * @version $Revision: 1.0 $ 33 | */ 34 | public interface Writable { 35 | 36 | /** 37 | * Write the object to the output stream using the default IO instance 38 | * @param out OutputStream 39 | */ 40 | void writeTo(OutputStream out); 41 | 42 | /** 43 | * Asynchronously write the object to the output stream using the 44 | * default IO instance 45 | * @param out 46 | * @param executor 47 | * @return Future<?> 48 | */ 49 | Future writeTo(OutputStream out, ExecutorService executor); 50 | 51 | /** 52 | * Write the object to the Writer using the default IO instance 53 | * @param out Writer 54 | */ 55 | void writeTo(Writer out); 56 | 57 | /** 58 | * Asynchronously write the object to the writer using the default IO instance 59 | * @param out 60 | * @param executor 61 | * @return Future<?> 62 | */ 63 | Future writeTo(Writer out, ExecutorService executor); 64 | 65 | /** 66 | * Write the object to the output stream using the given IO instance 67 | * @param out OutputStream 68 | * @param io IO 69 | */ 70 | void writeTo(OutputStream out, IO io); 71 | 72 | /** 73 | * Asynchronously write the object to the output stream using the given 74 | * IO instance. 75 | * @param out 76 | * @param io 77 | * @param executor 78 | * @return Future<?> 79 | */ 80 | Future writeTo(OutputStream out, IO io, ExecutorService executor); 81 | 82 | /** 83 | * Write the object to the writer using the given IO instance 84 | * @param out Writer 85 | * @param io IO 86 | */ 87 | void writeTo(Writer out, IO io); 88 | 89 | /** 90 | * Asynchronously write the object to the writer using the given IO instance 91 | * @param out 92 | * @param io 93 | * @param executor 94 | * @return Future<?> 95 | */ 96 | Future writeTo(Writer out, IO io, ExecutorService executor); 97 | 98 | } 99 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/internal/Adapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.internal; 23 | 24 | import com.google.gson.JsonDeserializer; 25 | import com.google.gson.JsonSerializer; 26 | 27 | /** 28 | * @author james 29 | * @version $Revision: 1.0 $ 30 | */ 31 | public abstract class Adapter 32 | implements JsonSerializer, JsonDeserializer { 33 | 34 | } 35 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/internal/EnumAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.internal; 23 | import static com.google.common.base.Enums.stringConverter; 24 | import static com.google.common.base.Preconditions.checkArgument; 25 | import static com.ibm.common.activitystreams.util.Converters.stringConverter; 26 | import static com.ibm.common.activitystreams.util.Converters.toLowerConverter; 27 | import static com.ibm.common.activitystreams.util.Converters.toUpperConverter; 28 | 29 | import java.lang.reflect.Type; 30 | 31 | import com.google.common.base.Converter; 32 | import com.google.gson.JsonDeserializationContext; 33 | import com.google.gson.JsonElement; 34 | import com.google.gson.JsonParseException; 35 | import com.google.gson.JsonPrimitive; 36 | import com.google.gson.JsonSerializationContext; 37 | 38 | /** 39 | * @author james 40 | * @version $Revision: 1.0 $ 41 | */ 42 | public final class EnumAdapter> 43 | extends Adapter { 44 | 45 | protected static final Converter toLower = 46 | toLowerConverter(); 47 | protected static final Converter toUpper = 48 | toUpperConverter(); 49 | 50 | private final Converter des; 51 | private final Converter ser; 52 | /** 53 | * Constructor for EnumAdapter. 54 | * @param _enumClass Class 55 | */ 56 | public EnumAdapter(Class _enumClass) { 57 | this(_enumClass, stringConverter(_enumClass)); 58 | } 59 | 60 | /** 61 | * Constructor for EnumAdapter 62 | 63 | 64 | * @param _enumClass Class 65 | * @param or E 66 | */ 67 | public EnumAdapter(Class _enumClass, E or) { 68 | this(_enumClass, stringConverter(_enumClass,or)); 69 | } 70 | 71 | /** 72 | * Constructor for EnumAdapter. 73 | * @param _enumClass Class 74 | 75 | * @param c Converter 76 | */ 77 | public EnumAdapter( 78 | Class _enumClass, 79 | Converter c) { 80 | super(); 81 | this.des = toUpper.andThen(c); 82 | this.ser = c.reverse().andThen(toLower); 83 | } 84 | 85 | /** 86 | * Method serialize. 87 | * @param src E 88 | * @param typeOfSrc Type 89 | * @param context JsonSerializationContext 90 | * @return JsonElement 91 | */ 92 | public JsonElement serialize( 93 | E src, 94 | Type typeOfSrc, 95 | JsonSerializationContext context) { 96 | return context.serialize(ser.convert(src)); 97 | } 98 | 99 | /** 100 | * Method deserialize. 101 | * @param json JsonElement 102 | * @param typeOfT Type 103 | * @param context JsonDeserializationContext 104 | * @return E 105 | * @throws JsonParseException 106 | * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) 107 | */ 108 | public E deserialize( 109 | JsonElement json, 110 | Type typeOfT, 111 | JsonDeserializationContext context) 112 | throws JsonParseException { 113 | checkArgument(json.isJsonPrimitive()); 114 | JsonPrimitive jp = json.getAsJsonPrimitive(); 115 | checkArgument(jp.isString()); 116 | return des.convert(jp.getAsString()); 117 | } 118 | 119 | /** 120 | * Method convert. 121 | * @param s String 122 | * @return E 123 | */ 124 | protected E convert(String s) { 125 | return des.convert(s); 126 | } 127 | 128 | } -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/internal/LazilyParsedNumberComparable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.internal; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.primitives.Longs; 27 | import com.google.gson.internal.LazilyParsedNumber; 28 | 29 | /** 30 | */ 31 | final class LazilyParsedNumberComparable 32 | extends Number 33 | implements Comparable { 34 | private static final long serialVersionUID = 761729254455440231L; 35 | private final LazilyParsedNumber inner; 36 | /** 37 | * Constructor for LazilyParsedNumberComparable. 38 | * @param inner LazilyParsedNumber 39 | */ 40 | public LazilyParsedNumberComparable(LazilyParsedNumber inner) { 41 | this.inner = inner; 42 | } 43 | /** 44 | * Method compareTo. 45 | * @param o Number 46 | * @return int 47 | */ 48 | public int compareTo(Number o) { 49 | return Longs.compare(inner.longValue(), o.longValue()); 50 | } 51 | 52 | /** 53 | * Method doubleValue. 54 | * @return double 55 | */ 56 | public double doubleValue() { 57 | return inner.doubleValue(); 58 | } 59 | 60 | /** 61 | * Method floatValue. 62 | * @return float 63 | */ 64 | public float floatValue() { 65 | return inner.floatValue(); 66 | } 67 | 68 | /** 69 | * Method intValue. 70 | * @return int 71 | */ 72 | public int intValue() { 73 | return inner.intValue(); 74 | } 75 | 76 | /** 77 | * Method longValue. 78 | * @return long 79 | */ 80 | public long longValue() { 81 | return inner.longValue(); 82 | } 83 | /** 84 | * Method byteValue. 85 | * @return byte 86 | */ 87 | public byte byteValue() { 88 | return inner.byteValue(); 89 | } 90 | /** 91 | * Method shortValue. 92 | * @return short 93 | */ 94 | public short shortValue() { 95 | return inner.shortValue(); 96 | } 97 | /** 98 | * Method equals. 99 | * @param obj Object 100 | * @return boolean 101 | */ 102 | public boolean equals(Object obj) { 103 | return inner.equals(obj); 104 | } 105 | /** 106 | * Method hashCode. 107 | * @return int 108 | */ 109 | public int hashCode() { 110 | return inner.hashCode(); 111 | } 112 | /** 113 | * Method toString. 114 | * @return String 115 | */ 116 | public String toString() { 117 | return inner.toString(); 118 | } 119 | 120 | /** 121 | * Method writeReplace. 122 | * @return Object 123 | * @throws ObjectStreamException 124 | */ 125 | private Object writeReplace() throws ObjectStreamException { 126 | return inner; 127 | } 128 | } -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/internal/LinkValueAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.internal; 23 | 24 | import static com.google.common.base.Preconditions.checkArgument; 25 | import static com.ibm.common.activitystreams.Makers.linkValue; 26 | import static com.ibm.common.activitystreams.Makers.linkValues; 27 | 28 | import java.lang.reflect.Type; 29 | 30 | import com.google.gson.JsonDeserializationContext; 31 | import com.google.gson.JsonElement; 32 | import com.google.gson.JsonObject; 33 | import com.google.gson.JsonParseException; 34 | import com.google.gson.JsonPrimitive; 35 | import com.google.gson.JsonSerializationContext; 36 | import com.ibm.common.activitystreams.ASObject; 37 | import com.ibm.common.activitystreams.LinkValue; 38 | import com.ibm.common.activitystreams.LinkValue.SimpleLinkValue; 39 | import com.ibm.common.activitystreams.TypeValue; 40 | 41 | /** 42 | * @author james 43 | * @version $Revision: 1.0 $ 44 | */ 45 | final class LinkValueAdapter 46 | extends Adapter { 47 | 48 | private final Schema schema; 49 | 50 | /** 51 | * Constructor for LinkValueAdapter. 52 | * @param schema Schema 53 | */ 54 | public LinkValueAdapter(Schema schema) { 55 | this.schema = schema; 56 | } 57 | 58 | /** 59 | * Method serialize. 60 | * @param value LinkValue 61 | * @param type Type 62 | * @param context JsonSerializationContext 63 | 64 | * @return JsonElement */ 65 | public JsonElement serialize( 66 | LinkValue value, 67 | Type type, 68 | JsonSerializationContext context) { 69 | switch(value.valueType()) { 70 | case SIMPLE: 71 | LinkValue.SimpleLinkValue simple = (SimpleLinkValue) value; 72 | return context.serialize(simple.url(), String.class); 73 | case ARRAY: 74 | return context.serialize(value, Iterable.class); 75 | case OBJECT: 76 | return context.serialize(value, ASObject.class); 77 | default: 78 | throw new IllegalArgumentException(); 79 | } 80 | } 81 | 82 | /** 83 | * Method deserialize. 84 | * @param el JsonElement 85 | * @param type Type 86 | * @param context JsonDeserializationContext 87 | 88 | 89 | 90 | * @return LinkValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */ 91 | public LinkValue deserialize( 92 | JsonElement el, 93 | Type type, 94 | JsonDeserializationContext context) 95 | throws JsonParseException { 96 | checkArgument( 97 | el.isJsonArray() || 98 | el.isJsonObject() || 99 | el.isJsonPrimitive()); 100 | if (el.isJsonArray()) { 101 | LinkValue.ArrayLinkValue.Builder builder = 102 | linkValues(); 103 | for (JsonElement aryel : el.getAsJsonArray()) 104 | builder.add( 105 | context.deserialize( 106 | aryel, 107 | LinkValue.class)); 108 | return builder.get(); 109 | } else if (el.isJsonObject()) { 110 | JsonObject obj = el.getAsJsonObject(); 111 | if (obj.has("objectType")) { 112 | TypeValue tv = 113 | context.deserialize( 114 | obj.get("objectType"), 115 | TypeValue.class); 116 | Model pMap = 117 | schema.forObjectType(tv.id()); 118 | return context.deserialize( 119 | el, 120 | pMap != null && pMap.type() != null ? 121 | pMap.type() : 122 | ASObject.class); 123 | } else { 124 | return context.deserialize( 125 | el, 126 | ASObject.class); 127 | } 128 | } else { 129 | JsonPrimitive prim = 130 | el.getAsJsonPrimitive(); 131 | checkArgument(prim.isString()); 132 | return linkValue(prim.getAsString()); 133 | } 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/internal/NaturalLanguageValueAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.internal; 23 | 24 | import static com.google.common.base.Preconditions.checkArgument; 25 | 26 | import java.lang.reflect.Type; 27 | import java.util.Map.Entry; 28 | 29 | import com.google.gson.JsonDeserializationContext; 30 | import com.google.gson.JsonElement; 31 | import com.google.gson.JsonObject; 32 | import com.google.gson.JsonParseException; 33 | import com.google.gson.JsonPrimitive; 34 | import com.google.gson.JsonSerializationContext; 35 | import com.ibm.common.activitystreams.NLV; 36 | import com.ibm.common.activitystreams.NLV.MapNLV; 37 | 38 | /** 39 | * @author james 40 | * @version $Revision: 1.0 $ 41 | */ 42 | final class NaturalLanguageValueAdapter 43 | extends Adapter { 44 | 45 | /** 46 | * Method serialize. 47 | * @param nlv NLV 48 | * @param type Type 49 | * @param context JsonSerializationContext 50 | 51 | * @return JsonElement */ 52 | public JsonElement serialize( 53 | NLV nlv, 54 | Type type, 55 | JsonSerializationContext context) { 56 | JsonElement el = null; 57 | switch (nlv.valueType()) { 58 | case SIMPLE: 59 | el = context.serialize(((NLV.SimpleNLV)nlv).value()); 60 | break; 61 | case OBJECT: 62 | NLV.MapNLV map = 63 | (MapNLV) nlv; 64 | JsonObject obj = new JsonObject(); 65 | for (String lang : map) 66 | obj.addProperty( 67 | lang.toString(), 68 | map.value(lang)); 69 | el = obj; 70 | break; 71 | default: 72 | } 73 | return el; 74 | } 75 | 76 | /** 77 | * Method deserialize. 78 | * @param element JsonElement 79 | * @param type1 Type 80 | * @param context JsonDeserializationContext 81 | 82 | 83 | 84 | * @return NLV * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */ 85 | public NLV deserialize( 86 | JsonElement element, 87 | Type type1, 88 | JsonDeserializationContext context) 89 | throws JsonParseException { 90 | checkArgument( 91 | element.isJsonPrimitive() || 92 | element.isJsonObject()); 93 | if (element.isJsonPrimitive()) { 94 | JsonPrimitive prim = 95 | element.getAsJsonPrimitive(); 96 | checkArgument(prim.isString()); 97 | return NLV.SimpleNLV.make( 98 | prim.getAsString()); 99 | } else { 100 | try { 101 | JsonObject obj = 102 | element.getAsJsonObject(); 103 | NLV.MapNLV.Builder builder = 104 | NLV.MapNLV.make(); 105 | for (Entry entry : obj.entrySet()) 106 | builder.set( 107 | entry.getKey(), 108 | entry.getValue().getAsString()); 109 | return builder.get(); 110 | } catch (Throwable t) { 111 | throw new IllegalArgumentException(); 112 | } 113 | } 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/internal/SimpleAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | 23 | package com.ibm.common.activitystreams.internal; 24 | 25 | import java.lang.reflect.Type; 26 | 27 | import com.google.common.base.Function; 28 | import com.google.gson.JsonDeserializationContext; 29 | import com.google.gson.JsonElement; 30 | import com.google.gson.JsonParseException; 31 | import com.google.gson.JsonSerializationContext; 32 | 33 | /** 34 | * @author james 35 | * @version $Revision: 1.0 $ 36 | */ 37 | public abstract class SimpleAdapter 38 | extends Adapter 39 | implements Function { 40 | 41 | /** 42 | * Method serialize. 43 | * @param t T 44 | * @param type Type 45 | * @param context JsonSerializationContext 46 | 47 | 48 | * @return JsonElement * @see com.google.gson.JsonSerializer#serialize(T, Type, JsonSerializationContext) */ 49 | public JsonElement serialize( 50 | T t, 51 | Type type, 52 | JsonSerializationContext context) { 53 | return context.serialize(serialize(t)); 54 | } 55 | 56 | /** 57 | * Method serialize. 58 | * @param t T 59 | 60 | * @return String */ 61 | protected String serialize(T t) { 62 | return t != null ? t.toString() : null; 63 | } 64 | 65 | /** 66 | * Method deserialize. 67 | * @param v String 68 | 69 | * @return T */ 70 | protected T deserialize(String v) { 71 | return apply(v); 72 | } 73 | 74 | /** 75 | * Method deserialize. 76 | * @param json JsonElement 77 | * @param type Type 78 | * @param context JsonDeserializationContext 79 | 80 | 81 | 82 | * @return T * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */ 83 | public T deserialize( 84 | JsonElement json, 85 | Type type, 86 | JsonDeserializationContext context) 87 | throws JsonParseException { 88 | return deserialize(json.getAsJsonPrimitive().getAsString()); 89 | } 90 | } -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/internal/TypeValueAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.internal; 23 | 24 | import static com.google.common.base.Preconditions.checkArgument; 25 | import static com.ibm.common.activitystreams.Makers.type; 26 | 27 | import java.lang.reflect.Type; 28 | 29 | import com.google.gson.JsonDeserializationContext; 30 | import com.google.gson.JsonElement; 31 | import com.google.gson.JsonObject; 32 | import com.google.gson.JsonParseException; 33 | import com.google.gson.JsonPrimitive; 34 | import com.google.gson.JsonSerializationContext; 35 | import com.ibm.common.activitystreams.ASObject; 36 | import com.ibm.common.activitystreams.TypeValue; 37 | import com.ibm.common.activitystreams.ValueType; 38 | 39 | /** 40 | * @author james 41 | * @version $Revision: 1.0 $ 42 | */ 43 | final class TypeValueAdapter 44 | extends Adapter { 45 | 46 | private final Schema schema; 47 | 48 | /** 49 | * Constructor for TypeValueAdapter. 50 | * @param schema Schema 51 | */ 52 | public TypeValueAdapter( 53 | Schema schema) { 54 | this.schema = schema; 55 | } 56 | 57 | /** 58 | * Method serialize. 59 | * @param value TypeValue 60 | * @param type Type 61 | * @param context JsonSerializationContext 62 | 63 | * @return JsonElement */ 64 | public JsonElement serialize( 65 | TypeValue value, 66 | Type type, 67 | JsonSerializationContext context) { 68 | boolean simple = value.valueType() == ValueType.SIMPLE; 69 | return context.serialize( 70 | simple ? value.id() : value, 71 | simple ? String.class : ASObject.class 72 | ); 73 | } 74 | 75 | /** 76 | * Method deserialize. 77 | * @param el JsonElement 78 | * @param type Type 79 | * @param context JsonDeserializationContext 80 | 81 | 82 | 83 | * @return TypeValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */ 84 | public TypeValue deserialize( 85 | JsonElement el, 86 | Type type, 87 | JsonDeserializationContext context) 88 | throws JsonParseException { 89 | checkArgument( 90 | el.isJsonPrimitive() || 91 | el.isJsonObject()); 92 | if (el.isJsonPrimitive()) { 93 | JsonPrimitive prim = 94 | el.getAsJsonPrimitive(); 95 | checkArgument(prim.isString()); 96 | return type(prim.getAsString()); 97 | } else { 98 | JsonObject obj = el.getAsJsonObject(); 99 | if (obj.has("objectType")) { 100 | TypeValue tv = 101 | context.deserialize( 102 | obj.get("objectType"), 103 | TypeValue.class); 104 | Model pMap = 105 | schema.forObjectType(tv.id()); 106 | return 107 | context.deserialize( 108 | el, 109 | pMap.type() != null ? 110 | pMap.type() : 111 | ASObject.class); 112 | } else { 113 | return 114 | context.deserialize( 115 | el, 116 | ASObject.class); 117 | } 118 | } 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/util/AbstractDictionaryObjectAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.util; 23 | 24 | import static com.google.common.base.Preconditions.checkArgument; 25 | 26 | import java.lang.reflect.Type; 27 | import java.util.Map.Entry; 28 | 29 | import com.google.gson.JsonDeserializationContext; 30 | import com.google.gson.JsonElement; 31 | import com.google.gson.JsonObject; 32 | import com.google.gson.JsonParseException; 33 | import com.google.gson.JsonSerializationContext; 34 | import com.ibm.common.activitystreams.internal.Adapter; 35 | 36 | /** 37 | * Abstract base GSON Serializer for AbstractDictionaryObject 38 | * instances. 39 | * @author james 40 | * @version $Revision: 1.0 $ 41 | */ 42 | public abstract class AbstractDictionaryObjectAdapter 43 | , 44 | B extends AbstractDictionaryObject.AbstractBuilder> 45 | extends Adapter { 46 | 47 | private final Class klass; 48 | 49 | /** 50 | * Constructor for AbstractDictionaryObjectAdapter. 51 | * @param klass Class 52 | */ 53 | protected AbstractDictionaryObjectAdapter(Class klass) { 54 | this.klass = klass; 55 | } 56 | 57 | public JsonElement serialize( 58 | X x, 59 | Type type, 60 | JsonSerializationContext context) { 61 | JsonObject obj = new JsonObject(); 62 | for (String key : x) 63 | obj.add( 64 | key, 65 | context.serialize( 66 | x.getSingle(key), 67 | klass)); 68 | return obj; 69 | } 70 | 71 | public X deserialize( 72 | JsonElement element, 73 | Type type1, 74 | JsonDeserializationContext context) 75 | throws JsonParseException { 76 | checkArgument(element.isJsonObject()); 77 | try { 78 | JsonObject obj = 79 | element.getAsJsonObject(); 80 | B builder = builder(); 81 | for (Entry entry : obj.entrySet()) 82 | builder.set( 83 | entry.getKey(), 84 | context.deserialize( 85 | entry.getValue(), 86 | klass)); 87 | return builder.get(); 88 | } catch (Throwable t) { 89 | t.printStackTrace(); 90 | throw new IllegalArgumentException(); 91 | } 92 | } 93 | 94 | protected abstract B builder(); 95 | } 96 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/util/AbstractWritable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.util; 23 | 24 | import static com.ibm.common.activitystreams.IO.makeDefault; 25 | 26 | import java.io.OutputStream; 27 | import java.io.StringWriter; 28 | import java.io.Writer; 29 | import java.util.concurrent.ExecutorService; 30 | import java.util.concurrent.Future; 31 | 32 | import com.google.common.base.Supplier; 33 | import com.ibm.common.activitystreams.IO; 34 | import com.ibm.common.activitystreams.Writable; 35 | 36 | /** 37 | * Utility base class for Writable instances. 38 | * @author james 39 | * @version $Revision: 1.0 $ 40 | */ 41 | public abstract class AbstractWritable 42 | implements Writable { 43 | 44 | public static abstract class AbstractWritableBuilder 45 | > 46 | implements Supplier { 47 | 48 | private IO io = null; 49 | 50 | /** 51 | * Set the IO instance that should be used 52 | * by default for writing instances of this 53 | * object. 54 | */ 55 | @SuppressWarnings("unchecked") 56 | public B writeUsing(IO io) { 57 | this.io = io; 58 | return (B)this; 59 | } 60 | 61 | public void writeTo(OutputStream out) { 62 | get().writeTo(out); 63 | } 64 | 65 | public Future writeTo(OutputStream out, ExecutorService executor) { 66 | return get().writeTo(out,executor); 67 | } 68 | 69 | public void writeTo(Writer out) { 70 | get().writeTo(out); 71 | } 72 | 73 | public Future writeTo(Writer out, ExecutorService executor) { 74 | return get().writeTo(out,executor); 75 | } 76 | 77 | 78 | public void writeTo(OutputStream out, IO io) { 79 | get().writeTo(out,io); 80 | } 81 | 82 | public Future writeTo(OutputStream out, IO io, ExecutorService executor) { 83 | return get().writeTo(out, io, executor); 84 | } 85 | 86 | public void writeTo(Writer out, IO io) { 87 | get().writeTo(out,io); 88 | } 89 | 90 | public Future writeTo(Writer out, IO io, ExecutorService executor) { 91 | return get().writeTo(out, io, executor); 92 | } 93 | 94 | } 95 | 96 | private final IO io; 97 | 98 | protected AbstractWritable(AbstractWritableBuilder b) { 99 | this.io = b.io != null ? b.io : makeDefault(); 100 | } 101 | 102 | public void writeTo(OutputStream out) { 103 | writeTo(out,io); 104 | } 105 | 106 | public Future writeTo(OutputStream out, ExecutorService executor) { 107 | return writeTo(out, io, executor); 108 | } 109 | 110 | public void writeTo(OutputStream out, IO io) { 111 | io.write(this,out); 112 | } 113 | 114 | public Future writeTo(OutputStream out, IO io, ExecutorService executor) { 115 | return io.write(this,out,executor); 116 | } 117 | 118 | public void writeTo(Writer out) { 119 | writeTo(out, io); 120 | } 121 | 122 | public Future writeTo(Writer out, ExecutorService executor) { 123 | return writeTo(out, io, executor); 124 | } 125 | 126 | public void writeTo(Writer out, IO io) { 127 | io.write(this,out); 128 | } 129 | 130 | public Future writeTo(Writer out, IO io, ExecutorService executor) { 131 | return io.write(this, out, executor); 132 | } 133 | 134 | public String toString(IO io) { 135 | StringWriter sw = 136 | new StringWriter(); 137 | io.write(this, sw); 138 | return sw.toString(); 139 | } 140 | 141 | public Future toString(ExecutorService executor) { 142 | return io.write(this, executor); 143 | } 144 | 145 | public Future toString(IO io, ExecutorService executor) { 146 | return io.write(this, executor); 147 | } 148 | 149 | public String toString() { 150 | return toString(io); 151 | } 152 | 153 | } 154 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/util/Module.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.util; 23 | 24 | import com.ibm.common.activitystreams.IO; 25 | import com.ibm.common.activitystreams.internal.Schema; 26 | 27 | public interface Module { 28 | 29 | void apply(Schema.Builder builder); 30 | 31 | void apply(IO.Builder builder, Schema schema); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /core/src/main/java/com/ibm/common/activitystreams/util/Util.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.util; 2 | 3 | import static com.google.common.base.Preconditions.checkNotNull; 4 | 5 | import java.util.Locale; 6 | 7 | public final class Util { 8 | 9 | private Util() {} 10 | 11 | /** 12 | * Method convLocale. 13 | * @param locale Locale 14 | * @return String 15 | **/ 16 | public static String convLocale(Locale locale) { 17 | return checkNotNull(locale).toString().replaceAll("_", "-"); 18 | } 19 | 20 | public static final String DEFAULT_LOCALE = 21 | convLocale(Locale.getDefault()); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /core/src/test/java/com/ibm/common/activitystreams/test/TestIO.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.test; 23 | 24 | import static org.junit.Assert.assertNotNull; 25 | import static org.junit.Assert.assertTrue; 26 | 27 | import org.joda.time.DateTime; 28 | import org.junit.Test; 29 | 30 | import com.ibm.common.activitystreams.ASObject; 31 | import com.ibm.common.activitystreams.Activity; 32 | import com.ibm.common.activitystreams.Collection; 33 | import com.ibm.common.activitystreams.IO; 34 | import com.ibm.common.activitystreams.internal.Schema; 35 | 36 | public final class TestIO { 37 | 38 | @Test 39 | public void testIO() { 40 | IO.Builder iobuilder = IO.make(); 41 | assertNotNull(iobuilder); 42 | IO io = iobuilder.get(); 43 | assertNotNull(io); 44 | } 45 | 46 | @Test 47 | public void testIO2() { 48 | final String in = "{\"a\":1,\"b\":2}"; 49 | final String out = "{\n \"a\": 1,\n \"b\": 2\n}"; 50 | IO io = IO.makeDefaultPrettyPrint(); 51 | assertNotNull(io); 52 | assertTrue(io.read(in).toString(io).equals(out)); 53 | } 54 | 55 | @Test 56 | public void testIO3() { 57 | final String in = "{\"a\":1,\"b\":2}"; 58 | IO io = IO.makeDefault(); 59 | assertTrue(io.read(in) instanceof ASObject); 60 | assertTrue(io.readAs(in,Activity.class) instanceof Activity); 61 | assertTrue(io.readAs(in,Collection.class) instanceof Collection); 62 | assertTrue(io.readAsActivity(in) instanceof Activity); 63 | assertTrue(io.readAsCollection(in) instanceof Collection); 64 | } 65 | 66 | @Test 67 | public void testSchema() { 68 | Schema schema = 69 | Schema.make() 70 | .map("foo", Schema.object.template().dateTime("bar")) 71 | .get(); 72 | IO io = IO.make().schema(schema).get(); 73 | ASObject obj = 74 | io.read( 75 | "{\"objectType\":\"foo\",\"bar\":\"2013-12-12T12:12:12Z\"}"); 76 | assertTrue(obj.get("bar") instanceof DateTime); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /core/src/test/resources/data/1.json: -------------------------------------------------------------------------------- 1 | { 2 | "verb": "post", 3 | "actor": "urn:example:person:joe", 4 | "object": "http://example.org/posts/1" 5 | } -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/AS1Position.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import static com.google.common.primitives.Floats.max; 25 | import static com.google.common.primitives.Floats.min; 26 | 27 | import java.io.ObjectStreamException; 28 | 29 | import com.ibm.common.activitystreams.ASObject; 30 | 31 | /** 32 | * Represents an Activity Streams 1.0 style position object 33 | * see (https://github.com/activitystreams/activity-schema/blob/master/activity-schema.md) 34 | * 35 | * Use of the AS1Position object is deprecated. Use the GeoJSON mechanisms 36 | * instead 37 | * 38 | * @author james 39 | * @deprecated Use Position 40 | */ 41 | public final class AS1Position 42 | extends ASObject { 43 | 44 | public static final class Builder 45 | extends ASObject.AbstractBuilder { 46 | 47 | Builder() { 48 | objectType("position"); 49 | } 50 | 51 | /** 52 | * Set the latitude 53 | * @param latitude float 54 | * @return Builder 55 | */ 56 | public Builder latitude(float latitude) { 57 | return set("latitude", max(0f,min(90.0f,latitude))); 58 | } 59 | 60 | /** 61 | * Set the longitude 62 | * @param longitude float 63 | * @return Builder 64 | */ 65 | public Builder longitude(float longitude) { 66 | return set("longitude", max(-180.0f,min(180.0f,longitude))); 67 | } 68 | 69 | /** 70 | * Set the altitude 71 | * @param altitude float 72 | * @return Builder 73 | */ 74 | public Builder altitude(float altitude) { 75 | return set("altitude", altitude); 76 | } 77 | 78 | @Override 79 | public AS1Position get() { 80 | return new AS1Position(this); 81 | } 82 | 83 | } 84 | 85 | private AS1Position(Builder builder) { 86 | super(builder); 87 | } 88 | 89 | /** 90 | * Get the latitude 91 | * @return float 92 | */ 93 | public float latitude() { 94 | return max(0f,min(90.0f,getFloat("latitude"))); 95 | } 96 | 97 | /** 98 | * Get the longitude 99 | * @return float 100 | */ 101 | public float longitude() { 102 | return max(-180.0f,min(180.0f,getFloat("longitude"))); 103 | } 104 | 105 | /** 106 | * Get the altitude. If the altitude property is not set, this 107 | * will return Float.MIN_VALUE; 108 | * @return float 109 | */ 110 | public float altitude() { 111 | return getFloat("altitude", Float.MIN_VALUE); 112 | } 113 | 114 | // Java Serialization Support 115 | 116 | Object writeReplace() throws java.io.ObjectStreamException { 117 | return new SerializedForm(this); 118 | } 119 | 120 | private static class SerializedForm 121 | extends AbstractSerializedForm { 122 | private static final long serialVersionUID = -2060301713159936285L; 123 | protected SerializedForm(AS1Position obj) { 124 | super(obj); 125 | } 126 | Object readResolve() throws ObjectStreamException { 127 | return super.doReadResolve(); 128 | } 129 | protected AS1Position.Builder builder() { 130 | return GeoMakers.as1Position(); 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/CRS.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import java.io.ObjectStreamException; 25 | import java.io.Serializable; 26 | import java.util.Iterator; 27 | import java.util.Map; 28 | 29 | import com.google.common.base.Objects; 30 | import com.google.common.base.Supplier; 31 | import com.google.common.collect.ImmutableMap; 32 | 33 | /** 34 | * A GeoJSON Coordinate Reference System description 35 | * see http://geojson.org/geojson-spec.html#coordinate-reference-system-objects 36 | * @author james 37 | */ 38 | public final class CRS 39 | implements Iterable, Serializable { 40 | 41 | public static final class Builder implements Supplier { 42 | 43 | private String type; 44 | private ImmutableMap.Builder properties = 45 | ImmutableMap.builder(); 46 | 47 | public Builder type(String type) { 48 | this.type = type; 49 | return this; 50 | } 51 | 52 | public Builder set(String name, Object value) { 53 | this.properties.put(name,value); 54 | return this; 55 | } 56 | 57 | public Builder set(Map properties) { 58 | this.properties.putAll(properties); 59 | return this; 60 | } 61 | 62 | public CRS get() { 63 | return new CRS(this); 64 | } 65 | 66 | } 67 | 68 | private final String type; 69 | private final Map properties; 70 | 71 | CRS(Builder builder) { 72 | this.type = builder.type; 73 | this.properties = builder.properties.build(); 74 | } 75 | 76 | public String toString() { 77 | return Objects.toStringHelper(CRS.class) 78 | .addValue(type) 79 | .addValue(properties) 80 | .toString(); 81 | } 82 | 83 | public int size() { 84 | return properties.size(); 85 | } 86 | 87 | public String type() { 88 | return type; 89 | } 90 | 91 | public Map properties() { 92 | return properties; 93 | } 94 | 95 | @SuppressWarnings("unchecked") 96 | public T get(String name) { 97 | return (T)properties.get(name); 98 | } 99 | 100 | public T get(String name, T defaultValue) { 101 | T t = this.get(name); 102 | return t != null ? t : defaultValue; 103 | } 104 | 105 | public boolean has(String name) { 106 | return properties.containsKey(name); 107 | } 108 | 109 | @Override 110 | public Iterator iterator() { 111 | return properties.keySet().iterator(); 112 | } 113 | 114 | public static final CRS named(String name) { 115 | return new CRS.Builder() 116 | .type("name") 117 | .set("name",name) 118 | .get(); 119 | } 120 | 121 | public static final CRS linked(String href, String type) { 122 | return new CRS.Builder() 123 | .type("link") 124 | .set("href", href) 125 | .set("type", type) 126 | .get(); 127 | } 128 | 129 | Object writeReplace() throws java.io.ObjectStreamException { 130 | return new SerializedForm(this); 131 | } 132 | 133 | private static class SerializedForm implements Serializable { 134 | private static final long serialVersionUID = -2060301713159936285L; 135 | private String type; 136 | private Map properties; 137 | protected SerializedForm(CRS obj) { 138 | this.type = obj.type; 139 | this.properties = obj.properties; 140 | } 141 | Object readResolve() throws ObjectStreamException { 142 | CRS.Builder builder = new CRS.Builder(); 143 | builder.type(type); 144 | builder.properties.putAll(properties); 145 | return builder.get(); 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/FeatureCollection.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import static com.ibm.common.geojson.BoundingBox.calculateBoundingBoxFeatures; 25 | 26 | import java.io.ObjectStreamException; 27 | import java.util.Iterator; 28 | 29 | import com.google.common.base.Supplier; 30 | import com.google.common.collect.ImmutableList; 31 | import com.google.common.collect.Iterables; 32 | 33 | /** 34 | * A GeoJSON FeatureCollection object 35 | * see http://geojson.org/geojson-spec.html#feature-collection-objects 36 | * @author james 37 | * 38 | */ 39 | public final class FeatureCollection 40 | extends GeoObject 41 | implements Iterable { 42 | 43 | public static final class Builder 44 | extends GeoObject.Builder { 45 | 46 | private final ImmutableList.Builder list = 47 | ImmutableList.builder(); 48 | 49 | public Builder() { 50 | type(GeoObject.Type.FEATURECOLLECTION); 51 | } 52 | 53 | public Builder add(Iterable features) { 54 | list.addAll(features); 55 | return this; 56 | } 57 | 58 | public Builder add(Feature feature, Feature... features) { 59 | list.add(feature); 60 | if (features != null) 61 | list.add(features); 62 | return this; 63 | } 64 | 65 | public Builder add(Supplier feature) { 66 | return add(feature.get()); 67 | } 68 | 69 | @Override 70 | public void preGet() { 71 | set("features", list.build()); 72 | } 73 | 74 | @Override 75 | public FeatureCollection doGet() { 76 | return new FeatureCollection(this); 77 | } 78 | 79 | } 80 | 81 | FeatureCollection(Builder builder) { 82 | super(builder); 83 | } 84 | 85 | public Iterable features() { 86 | return this.>get( 87 | "features", 88 | ImmutableList.of()); 89 | } 90 | 91 | public int size() { 92 | return Iterables.size(features()); 93 | } 94 | 95 | public Feature get(int idx) { 96 | return Iterables.get(features(),idx); 97 | } 98 | 99 | public Iterator iterator() { 100 | return features().iterator(); 101 | } 102 | 103 | @Override 104 | protected FeatureCollection makeWithBoundingBox() { 105 | return new FeatureCollection.Builder() 106 | .from(this) 107 | .add(this) 108 | .boundingBox(calculateBoundingBoxFeatures(this)) 109 | .get(); 110 | } 111 | 112 | Object writeReplace() throws java.io.ObjectStreamException { 113 | return new SerializedForm(this); 114 | } 115 | 116 | private static class SerializedForm 117 | extends AbstractSerializedForm { 118 | private static final long serialVersionUID = -2060301713159936281L; 119 | protected SerializedForm(FeatureCollection obj) { 120 | super(obj); 121 | } 122 | Object readResolve() throws ObjectStreamException { 123 | return doReadResolve(); 124 | } 125 | @Override 126 | protected FeatureCollection.Builder builder() { 127 | return GeoMakers.featureCollection(); 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/GeoMakers.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | /** 25 | * Makers for the various GeoJSON object types 26 | * @author james 27 | * 28 | */ 29 | @SuppressWarnings("deprecation") 30 | public final class GeoMakers { 31 | 32 | private GeoMakers() {} 33 | 34 | public static Place.Builder place() { 35 | return new Place.Builder(); 36 | } 37 | 38 | /** @deprecated **/ 39 | public static AS1Position.Builder as1Position() { 40 | return new AS1Position.Builder(); 41 | } 42 | 43 | /** @deprecated **/ 44 | public static AS1Position as1Position( 45 | float latitude, 46 | float longitude, 47 | float altitude) { 48 | return as1Position() 49 | .longitude(longitude) 50 | .latitude(latitude) 51 | .altitude(altitude) 52 | .get(); 53 | } 54 | 55 | /** @deprecated **/ 56 | public static AS1Position as1Position( 57 | Position position) { 58 | return as1Position( 59 | position.northing(), 60 | position.easting(), 61 | position.altitude()); 62 | } 63 | 64 | public static Position position( 65 | AS1Position position) { 66 | return position( 67 | position.latitude(), 68 | position.longitude(), 69 | position.altitude()); 70 | } 71 | 72 | public static Address.Builder address() { 73 | return new Address.Builder(); 74 | } 75 | 76 | public static Position position(float x, float y) { 77 | return GeoObject.position(x,y); 78 | } 79 | 80 | public static Position position(float x, float y, float z) { 81 | return GeoObject.position(x, y, z); 82 | } 83 | 84 | public static Point point(float x, float y) { 85 | return point().position(x,y).get(); 86 | } 87 | 88 | public static Point point(float x, float y, float z) { 89 | return point().position(x, y, z).get(); 90 | } 91 | 92 | public static Point point(Position position) { 93 | return point().position(position).get(); 94 | } 95 | 96 | public static Point.Builder point() { 97 | return new Point.Builder(); 98 | } 99 | 100 | public static MultiPoint.Builder multipoint() { 101 | return new MultiPoint.Builder(); 102 | } 103 | 104 | public static LineString.Builder linestring() { 105 | return new LineString.Builder(); 106 | } 107 | 108 | public static LineString.Builder linearRing() { 109 | return linestring().linearRing(); 110 | } 111 | 112 | public static Feature.Builder feature() { 113 | return new Feature.Builder(); 114 | } 115 | 116 | public static FeatureCollection.Builder featureCollection() { 117 | return new FeatureCollection.Builder(); 118 | } 119 | 120 | public static GeometryCollection.Builder geometryCollection() { 121 | return new GeometryCollection.Builder(); 122 | } 123 | 124 | public static MultiLineString.Builder multiLineString() { 125 | return new MultiLineString.Builder(); 126 | } 127 | 128 | public static MultiPolygon.Builder multiPolygon() { 129 | return new MultiPolygon.Builder(); 130 | } 131 | 132 | public static Polygon.Builder polygon() { 133 | return new Polygon.Builder(); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/Geometry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import com.google.common.collect.ImmutableList; 25 | 26 | public abstract class Geometry, M> 27 | extends GeoObject 28 | implements Iterable { 29 | 30 | private static final long serialVersionUID = -6184360762496309625L; 31 | 32 | protected Geometry(Builder builder) { 33 | super(builder); 34 | } 35 | 36 | public static abstract class CoordinateGeometry, M,P> 37 | extends Geometry { 38 | 39 | private static final long serialVersionUID = -6502745908455054826L; 40 | 41 | protected static abstract class Builder 42 | , B extends Builder> 43 | extends GeoObject.Builder { 44 | 45 | protected abstract P coordinates(); 46 | 47 | public void preGet() { 48 | set("coordinates", coordinates()); 49 | } 50 | 51 | protected abstract C doGet(); 52 | } 53 | 54 | protected CoordinateGeometry(Builder builder) { 55 | super(builder); 56 | } 57 | 58 | @SuppressWarnings("unchecked") 59 | public Iterable coordinates() { 60 | Object o = get("coordinates"); 61 | if (o instanceof Iterable) 62 | return (Iterable)o; 63 | else 64 | return ImmutableList.of((M)o); 65 | } 66 | 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/GeometryCollection.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import static com.ibm.common.geojson.BoundingBox.calculateBoundingBoxGeometries; 25 | 26 | import java.io.ObjectStreamException; 27 | import java.util.Iterator; 28 | 29 | import com.google.common.collect.ImmutableList; 30 | import com.google.common.collect.Iterables; 31 | 32 | /** 33 | * A GeoJSON GeometryCollection object 34 | * see http://geojson.org/geojson-spec.html#geometry-collection 35 | * @author james 36 | * 37 | */ 38 | public final class GeometryCollection 39 | extends Geometry> { 40 | 41 | public static final class Builder 42 | extends GeoObject.Builder { 43 | 44 | private final ImmutableList.Builder> list = 45 | ImmutableList.builder(); 46 | 47 | public Builder() { 48 | type(GeoObject.Type.GEOMETRYCOLLECTION); 49 | } 50 | 51 | public Builder add(Geometry geometry, Geometry... geometries) { 52 | list.add(geometry); 53 | if (geometries != null) 54 | list.add(geometries); 55 | return this; 56 | } 57 | 58 | public Builder add(Iterable> geometries) { 59 | list.addAll(geometries); 60 | return this; 61 | } 62 | 63 | @Override 64 | public void preGet() { 65 | set("geometries", list.build()); 66 | } 67 | 68 | @Override 69 | public GeometryCollection doGet() { 70 | return new GeometryCollection(this); 71 | } 72 | } 73 | 74 | GeometryCollection(Builder builder) { 75 | super(builder); 76 | } 77 | 78 | public Iterable> geometries() { 79 | return this.>>get( 80 | "geometries", 81 | ImmutableList.>of()); 82 | } 83 | 84 | public int size() { 85 | return Iterables.size(geometries()); 86 | } 87 | 88 | @SuppressWarnings("unchecked") 89 | public >G get(int idx) { 90 | return (G)Iterables.get(geometries(),idx); 91 | } 92 | 93 | public Iterator> iterator() { 94 | return geometries().iterator(); 95 | } 96 | 97 | @Override 98 | public GeometryCollection makeWithBoundingBox() { 99 | return new GeometryCollection.Builder() 100 | .from(this) 101 | .add(this) 102 | .boundingBox(calculateBoundingBoxGeometries(this)) 103 | .get(); 104 | } 105 | 106 | Object writeReplace() throws java.io.ObjectStreamException { 107 | return new SerializedForm(this); 108 | } 109 | 110 | private static class SerializedForm 111 | extends AbstractSerializedForm { 112 | private static final long serialVersionUID = -2060301713159936281L; 113 | protected SerializedForm(GeometryCollection obj) { 114 | super(obj); 115 | } 116 | Object readResolve() throws ObjectStreamException { 117 | return doReadResolve(); 118 | } 119 | @SuppressWarnings("unchecked") 120 | @Override 121 | protected boolean handle(Builder builder, String key, Object val) { 122 | if ("geometries".equals(key)) { 123 | Iterable> list = (Iterable>) val; 124 | builder.list.addAll(list); 125 | return true; 126 | } 127 | return false; 128 | } 129 | @Override 130 | protected GeometryCollection.Builder builder() { 131 | return GeoMakers.geometryCollection(); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/MultiLineString.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import static com.ibm.common.geojson.BoundingBox.calculateBoundingBoxLineStrings; 25 | 26 | import java.io.ObjectStreamException; 27 | import java.util.Iterator; 28 | 29 | import com.google.common.base.Supplier; 30 | import com.google.common.collect.ImmutableList; 31 | import com.ibm.common.geojson.Geometry.CoordinateGeometry; 32 | 33 | /** 34 | * A GeoJSON MultiLineString object 35 | * see http://geojson.org/geojson-spec.html#multilinestring 36 | * @author james 37 | * 38 | */ 39 | public final class MultiLineString 40 | extends CoordinateGeometry> { 41 | 42 | public static final class Builder 43 | extends CoordinateGeometry.Builder, MultiLineString, Builder> { 44 | 45 | private final ImmutableList.Builder strings = 46 | ImmutableList.builder(); 47 | 48 | public Builder() { 49 | type(Type.MULTILINESTRING); 50 | } 51 | 52 | /** 53 | * Add one or more LineStrings 54 | * @param line LineString 55 | * @param lines LineString[] optional vararg 56 | * @return Builder 57 | */ 58 | public Builder add(LineString line, LineString... lines) { 59 | this.strings.add(line); 60 | if (lines != null) 61 | for (LineString l : lines) 62 | add(l); 63 | return this; 64 | } 65 | 66 | /** 67 | * Add a single LineString 68 | * @param line Supplier<LineString> 69 | * @return Builder 70 | */ 71 | public Builder add(Supplier line) { 72 | return add(line.get()); 73 | } 74 | 75 | /** 76 | * Add one or more LineStrings 77 | * @param lines Iterable<LineString> 78 | * @return Builder 79 | */ 80 | public Builder add(Iterable lines) { 81 | this.strings.addAll(lines); 82 | return this; 83 | } 84 | 85 | public MultiLineString doGet() { 86 | return new MultiLineString(this); 87 | } 88 | 89 | /** 90 | * Get this objects collection of LineStrings 91 | * @return Iterable<LineString> 92 | */ 93 | @Override 94 | protected Iterable coordinates() { 95 | return strings.build(); 96 | } 97 | 98 | } 99 | 100 | protected MultiLineString( 101 | Builder builder) { 102 | super(builder); 103 | } 104 | 105 | @Override 106 | public Iterator iterator() { 107 | return coordinates().iterator(); 108 | } 109 | 110 | /** 111 | * Copy this object with a calculated bounding box 112 | * @return MultiLineString 113 | */ 114 | @Override 115 | protected MultiLineString makeWithBoundingBox() { 116 | return new MultiLineString.Builder() 117 | .from(this) 118 | .add(this) 119 | .boundingBox( 120 | calculateBoundingBoxLineStrings(this)).get(); 121 | } 122 | 123 | Object writeReplace() throws java.io.ObjectStreamException { 124 | return new SerializedForm(this); 125 | } 126 | 127 | private static class SerializedForm 128 | extends AbstractSerializedForm { 129 | private static final long serialVersionUID = -2060301713159936281L; 130 | protected SerializedForm(MultiLineString obj) { 131 | super(obj); 132 | } 133 | Object readResolve() throws ObjectStreamException { 134 | return doReadResolve(); 135 | } 136 | @SuppressWarnings("unchecked") 137 | protected boolean handle(Builder builder, String key, Object val) { 138 | if ("coordinates".equals(key)) { 139 | Iterable list = (Iterable) val; 140 | builder.strings.addAll(list); 141 | return true; 142 | } 143 | return false; 144 | } 145 | @Override 146 | protected MultiLineString.Builder builder() { 147 | return GeoMakers.multiLineString(); 148 | } 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/MultiPolygon.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import static com.ibm.common.geojson.BoundingBox.calculateBoundingBoxPolygons; 25 | 26 | import java.io.ObjectStreamException; 27 | import java.util.Iterator; 28 | 29 | import com.google.common.base.Supplier; 30 | import com.google.common.collect.ImmutableList; 31 | import com.ibm.common.geojson.Geometry.CoordinateGeometry; 32 | 33 | /** 34 | * A GeoJSON MultiPolygon object. 35 | * see http://geojson.org/geojson-spec.html#multipolygon 36 | * @author james 37 | * 38 | */ 39 | public final class MultiPolygon 40 | extends CoordinateGeometry>{ 41 | 42 | public static final class Builder 43 | extends CoordinateGeometry.Builder, MultiPolygon, Builder> { 44 | 45 | private final ImmutableList.Builder strings = 46 | ImmutableList.builder(); 47 | 48 | public Builder() { 49 | type(Type.MULTIPOLYGON); 50 | } 51 | 52 | /** 53 | * Add one or more Polygons 54 | * @param poly Polygon 55 | * @param polys Polygon[] optional vararg 56 | * @return Builder 57 | */ 58 | public Builder add(Polygon poly, Polygon... polys) { 59 | // TODO: Check hole requirement 60 | this.strings.add(poly); 61 | if (polys != null) 62 | for (Polygon l : polys) 63 | add(l); 64 | return this; 65 | } 66 | 67 | /** 68 | * Add a Polygon 69 | * @param poly Supplier<Polygon> 70 | * @return Builder 71 | */ 72 | public Builder add(Supplier poly) { 73 | return add(poly.get()); 74 | } 75 | 76 | /** 77 | * Add one ore more Polygons 78 | * @param polygons Iterable<Polygon> 79 | * @return Builder 80 | */ 81 | public Builder add(Iterable polygons) { 82 | this.strings.addAll(polygons); 83 | return this; 84 | } 85 | 86 | public MultiPolygon doGet() { 87 | return new MultiPolygon(this); 88 | } 89 | 90 | @Override 91 | protected Iterable coordinates() { 92 | return strings.build(); 93 | } 94 | 95 | } 96 | 97 | protected MultiPolygon( 98 | Builder builder) { 99 | super(builder); 100 | } 101 | 102 | @Override 103 | public Iterator iterator() { 104 | return coordinates().iterator(); 105 | } 106 | 107 | /** 108 | * Return a copy of this object with a calculated bounding box 109 | * @return MultiPolygon 110 | */ 111 | @Override 112 | protected MultiPolygon makeWithBoundingBox() { 113 | return new MultiPolygon.Builder() 114 | .from(this) 115 | .add(this) 116 | .boundingBox(calculateBoundingBoxPolygons(this)) 117 | .get(); 118 | } 119 | 120 | Object writeReplace() throws java.io.ObjectStreamException { 121 | return new SerializedForm(this); 122 | } 123 | 124 | private static class SerializedForm 125 | extends AbstractSerializedForm { 126 | private static final long serialVersionUID = -2060301713159936281L; 127 | protected SerializedForm(MultiPolygon obj) { 128 | super(obj); 129 | } 130 | Object readResolve() throws ObjectStreamException { 131 | return doReadResolve(); 132 | } 133 | @SuppressWarnings("unchecked") 134 | @Override 135 | protected boolean handle(Builder builder, String key, Object val) { 136 | if ("coordinates".equals(key)) { 137 | Iterable list = (Iterable) val; 138 | builder.strings.addAll(list); 139 | return true; 140 | } 141 | return false; 142 | } 143 | @Override 144 | protected MultiPolygon.Builder builder() { 145 | return GeoMakers.multiPolygon(); 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/Point.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import java.io.ObjectStreamException; 25 | import java.util.Iterator; 26 | 27 | import com.google.common.base.Supplier; 28 | 29 | import static com.google.common.collect.Iterables.getFirst; 30 | import static com.ibm.common.geojson.BoundingBox.calculateBoundingBox; 31 | 32 | import com.ibm.common.geojson.Geometry.CoordinateGeometry; 33 | 34 | /** 35 | * A GeoJSON Point object 36 | * see http://geojson.org/geojson-spec.html#point. 37 | * 38 | * A Point object represents a single Position 39 | * 40 | * @author james 41 | * 42 | */ 43 | public final class Point 44 | extends CoordinateGeometry { 45 | 46 | public static final class Builder 47 | extends CoordinateGeometry.Builder { 48 | 49 | protected Position position; 50 | 51 | public Builder() { 52 | type(GeoObject.Type.POINT); 53 | } 54 | 55 | /** 56 | * Set the position 57 | * @param northing float 58 | * @param easting float 59 | * @param altitude float 60 | * @return Builder 61 | */ 62 | public Builder position(float northing, float easting, float altitude) { 63 | this.position = GeoObject.position(northing, easting, altitude); 64 | return this; 65 | } 66 | 67 | /** 68 | * Set the position 69 | * @param northing float 70 | * @param easting float 71 | * @return Builder 72 | */ 73 | public Builder position(float northing, float easting) { 74 | this.position = GeoObject.position(northing,easting); 75 | return this; 76 | } 77 | 78 | /** 79 | * Set the position 80 | * @param position Position 81 | * @return Builder 82 | */ 83 | public Builder position(Position position) { 84 | this.position = position; 85 | return this; 86 | } 87 | 88 | /** 89 | * Set the position 90 | * @param position Supplier<Position> 91 | * @return Builder 92 | */ 93 | public Builder position(Supplier position) { 94 | return position(position.get()); 95 | } 96 | 97 | @Override 98 | protected Position coordinates() { 99 | return position; 100 | } 101 | 102 | @Override 103 | public Point doGet() { 104 | return new Point(this); 105 | } 106 | 107 | } 108 | 109 | Point(Builder builder) { 110 | super(builder); 111 | } 112 | 113 | @Override 114 | public Iterator iterator() { 115 | return coordinates().iterator(); 116 | } 117 | 118 | 119 | @Override 120 | protected Point makeWithBoundingBox() { 121 | Position pos = getFirst(coordinates(),null); 122 | return new Point.Builder() 123 | .from(this) 124 | .position(pos) 125 | .boundingBox(calculateBoundingBox(pos)) 126 | .get(); 127 | } 128 | 129 | Object writeReplace() throws java.io.ObjectStreamException { 130 | return new SerializedForm(this); 131 | } 132 | 133 | private static class SerializedForm 134 | extends AbstractSerializedForm { 135 | private static final long serialVersionUID = -2060301713159936281L; 136 | protected SerializedForm(Point obj) { 137 | super(obj); 138 | } 139 | Object readResolve() throws ObjectStreamException { 140 | return doReadResolve(); 141 | } 142 | @Override 143 | protected boolean handle(Builder builder, String key, Object val) { 144 | if ("coordinates".equals(key)) { 145 | builder.position = (Position) val; 146 | return true; 147 | } 148 | return false; 149 | } 150 | @Override 151 | protected Point.Builder builder() { 152 | return GeoMakers.point(); 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/Polygon.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import static com.google.common.base.Preconditions.checkArgument; 25 | import static com.ibm.common.geojson.BoundingBox.calculateBoundingBoxLineStrings; 26 | 27 | import java.io.ObjectStreamException; 28 | import java.util.Iterator; 29 | 30 | import com.google.common.base.Supplier; 31 | import com.google.common.collect.ImmutableList; 32 | import com.ibm.common.geojson.Geometry.CoordinateGeometry; 33 | 34 | /** 35 | * A GeoJSON Polygon object 36 | * see http://geojson.org/geojson-spec.html#polygon 37 | * @author james 38 | * 39 | */ 40 | public final class Polygon 41 | extends CoordinateGeometry> { 42 | 43 | public static final class Builder 44 | extends CoordinateGeometry.Builder, Polygon, Builder> { 45 | 46 | private final ImmutableList.Builder strings = 47 | ImmutableList.builder(); 48 | 49 | public Builder() { 50 | type(Type.POLYGON); 51 | } 52 | 53 | /** 54 | * Add one or more LineStrings 55 | * @param line LineString 56 | * @param lines LineString[] optional vararg 57 | * @return Builder 58 | */ 59 | public Builder add(LineString line, LineString... lines) { 60 | checkArgument(line.linearRing(), "Polygon coordinates MUST be Linear Rings"); 61 | // TODO: Check hole requirement 62 | this.strings.add(line); 63 | if (lines != null) 64 | for (LineString l : lines) 65 | add(l); 66 | return this; 67 | } 68 | 69 | /** 70 | * Add a LineString 71 | * @param line Supplier<LineString> 72 | * @return Builder 73 | */ 74 | public Builder add(Supplier line) { 75 | return add(line.get()); 76 | } 77 | 78 | /** 79 | * Add one or more LineStrings 80 | * @param lines Iterable<LineString> 81 | * @return Builder 82 | */ 83 | public Builder add(Iterable lines) { 84 | this.strings.addAll(lines); 85 | return this; 86 | } 87 | 88 | public Polygon doGet() { 89 | return new Polygon(this); 90 | } 91 | 92 | @Override 93 | protected Iterable coordinates() { 94 | return strings.build(); 95 | } 96 | 97 | } 98 | 99 | protected Polygon( 100 | Builder builder) { 101 | super(builder); 102 | } 103 | 104 | @Override 105 | public Iterator iterator() { 106 | return coordinates().iterator(); 107 | } 108 | 109 | @Override 110 | protected Polygon makeWithBoundingBox() { 111 | return new Polygon.Builder() 112 | .from(this) 113 | .add(this) 114 | .boundingBox( 115 | calculateBoundingBoxLineStrings(this)).get(); 116 | } 117 | 118 | Object writeReplace() throws java.io.ObjectStreamException { 119 | return new SerializedForm(this); 120 | } 121 | 122 | private static class SerializedForm 123 | extends AbstractSerializedForm { 124 | private static final long serialVersionUID = -2060301713159936281L; 125 | protected SerializedForm(Polygon obj) { 126 | super(obj); 127 | } 128 | Object readResolve() throws ObjectStreamException { 129 | return doReadResolve(); 130 | } 131 | @SuppressWarnings("unchecked") 132 | @Override 133 | protected boolean handle(Builder builder, String key, Object val) { 134 | if ("coordinates".equals(key)) { 135 | Iterable list = (Iterable) val; 136 | builder.strings.addAll(list); 137 | return true; 138 | } 139 | return false; 140 | } 141 | @Override 142 | protected Polygon.Builder builder() { 143 | return GeoMakers.polygon(); 144 | } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/Position.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson; 23 | 24 | import static com.google.common.primitives.Floats.concat; 25 | import static com.google.common.primitives.Floats.toArray; 26 | 27 | import java.io.ObjectStreamException; 28 | import java.io.Serializable; 29 | import java.util.Arrays; 30 | import java.util.Iterator; 31 | 32 | import com.google.common.base.Supplier; 33 | import com.google.common.collect.ImmutableList; 34 | import com.google.common.primitives.Floats; 35 | 36 | /** 37 | * A GeoJSON Position 38 | * see http://geojson.org/geojson-spec.html#positions. 39 | * 40 | * Position objects consist primarily of three distinct values: 41 | * a northing (latitude or x), easting (longitude or y) and altitude (z). 42 | * 43 | * @author james 44 | * 45 | */ 46 | public final class Position 47 | implements Iterable, Serializable { 48 | 49 | public static final class Builder 50 | implements Supplier { 51 | 52 | private float x,y,z; 53 | private boolean hasz = false; 54 | private ImmutableList.Builder rest = 55 | ImmutableList.builder(); 56 | 57 | public Builder northing(float x) { 58 | this.x = x; 59 | return this; 60 | } 61 | 62 | public Builder easting(float y) { 63 | this.y = y; 64 | return this; 65 | } 66 | 67 | public Builder altitude(float z) { 68 | this.hasz = true; 69 | this.z = z; 70 | return this; 71 | } 72 | 73 | public Builder additional(float m) { 74 | this.rest.add(m); 75 | return this; 76 | } 77 | 78 | public Position get() { 79 | return new Position(this); 80 | } 81 | 82 | } 83 | 84 | private final float x,y,z; 85 | private final boolean hasz; 86 | private final float[] rest; 87 | 88 | Position(Builder builder) { 89 | this.x = builder.x; 90 | this.y = builder.y; 91 | this.z = builder.z; 92 | this.hasz = builder.hasz; 93 | this.rest = toArray(builder.rest.build()); 94 | } 95 | 96 | public float northing() { 97 | return x; 98 | } 99 | 100 | public float easting() { 101 | return y; 102 | } 103 | 104 | public float altitude() { 105 | return z; 106 | } 107 | 108 | public boolean hasAltitude() { 109 | return hasz; 110 | } 111 | 112 | public int size() { 113 | return values().length; 114 | } 115 | 116 | private float[] values() { 117 | return hasz? 118 | concat(new float[] {x,y,z}, rest) : 119 | concat(new float[] {x,y}, rest); 120 | } 121 | 122 | public String toString() { 123 | return Arrays.toString(values()); 124 | } 125 | 126 | @Override 127 | public Iterator iterator() { 128 | return Floats.asList(values()).iterator(); 129 | } 130 | 131 | Object writeReplace() throws java.io.ObjectStreamException { 132 | return new SerializedForm(this); 133 | } 134 | 135 | private static class SerializedForm implements Serializable { 136 | private static final long serialVersionUID = -2060301713159936285L; 137 | private float x,y,z; 138 | private boolean hasz; 139 | private float[] rest; 140 | protected SerializedForm(Position obj) { 141 | this.x = obj.x; 142 | this.y = obj.y; 143 | this.z = obj.z; 144 | this.hasz = obj.hasz; 145 | this.rest = obj.rest; 146 | } 147 | Object readResolve() throws ObjectStreamException { 148 | Position.Builder position = 149 | new Position.Builder() 150 | .northing(x) 151 | .easting(y) 152 | .altitude(z); 153 | position.hasz = this.hasz; 154 | if (rest != null) 155 | for (float f : rest) 156 | position.additional(f); 157 | return position.get(); 158 | } 159 | 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/as2/GeoModule.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson.as2; 23 | 24 | import com.ibm.common.activitystreams.IO; 25 | import com.ibm.common.activitystreams.internal.EnumAdapter; 26 | import com.ibm.common.activitystreams.internal.Model; 27 | import com.ibm.common.activitystreams.internal.Schema; 28 | import com.ibm.common.activitystreams.internal.Schema.Builder; 29 | import com.ibm.common.activitystreams.util.Module; 30 | import com.ibm.common.geojson.AS1Position; 31 | import com.ibm.common.geojson.Address; 32 | import com.ibm.common.geojson.GeoObject; 33 | import com.ibm.common.geojson.Place; 34 | 35 | /** 36 | * Enables the use of the GeoJSON extensions with Activity Streams 2.0 37 | * 38 | *
 39 |  *   import com.ibm.common.geojson.as2.GeoModule;
 40 |  *   import com.ibm.common.activitystreams.IO;
 41 |  *   
 42 |  *   //...
 43 |  *   
 44 |  *   IO io = IO.makeDefault(GeoModule.instance);
 45 |  *   
 46 |  * 
47 | * @author james 48 | * 49 | */ 50 | @SuppressWarnings("deprecation") 51 | public final class GeoModule 52 | implements Module { 53 | 54 | public static final Module instance = 55 | new GeoModule(); 56 | 57 | public static final Model place = 58 | Schema.object.template() 59 | .type(Place.class, Place.Builder.class) 60 | .as("geo", GeoObject.class) 61 | .as("address", Address.class) 62 | .as("position", AS1Position.class) 63 | .get(); 64 | 65 | public static final Model address = 66 | Schema.object.template() 67 | .type(Address.class, Address.Builder.class) 68 | .string( 69 | "country", 70 | "formatted", 71 | "locality", 72 | "postalCode", 73 | "region", 74 | "streetAddress") 75 | .get(); 76 | 77 | public static final Model as1Position = 78 | Schema.object.template() 79 | .type(AS1Position.class, AS1Position.Builder.class) 80 | .floatValue( 81 | "latitude", 82 | "longitude", 83 | "altitude") 84 | .get(); 85 | 86 | @Override 87 | public void apply( 88 | Builder builder) { 89 | builder.map("place", place) 90 | .map("address", address) 91 | .map("position", as1Position); 92 | } 93 | 94 | @Override 95 | public void apply( 96 | IO.Builder builder, 97 | Schema schema) { 98 | final GeoObjectAdapter base = 99 | new GeoObjectAdapter(schema); 100 | final GeoAdapter geo = 101 | new GeoAdapter(); 102 | builder.hierarchicalAdapter(Place.class, base) 103 | .hierarchicalAdapter(Address.class, base) 104 | .hierarchicalAdapter(AS1Position.class, base) 105 | .hierarchicalAdapter(GeoObject.class, geo) 106 | .hierarchicalAdapter( 107 | GeoObject.Type.class, 108 | new EnumAdapter(GeoObject.Type.class)); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /geo/src/main/java/com/ibm/common/geojson/as2/GeoObjectAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.geojson.as2; 23 | 24 | import java.lang.reflect.Type; 25 | 26 | import com.google.common.collect.ImmutableSet; 27 | import com.ibm.common.activitystreams.ASObject.AbstractBuilder; 28 | import com.ibm.common.activitystreams.internal.ASObjectAdapter; 29 | import com.ibm.common.activitystreams.internal.Model; 30 | import com.ibm.common.activitystreams.internal.Schema; 31 | import com.ibm.common.geojson.AS1Position; 32 | import com.ibm.common.geojson.Address; 33 | import com.ibm.common.geojson.GeoMakers; 34 | import com.ibm.common.geojson.Place; 35 | 36 | @SuppressWarnings("deprecation") 37 | public class GeoObjectAdapter 38 | extends ASObjectAdapter { 39 | 40 | protected GeoObjectAdapter(Schema schema) { 41 | super(schema); 42 | } 43 | 44 | private static final ImmutableSet knownTypes = 45 | ImmutableSet.of(Address.class,AS1Position.class,Place.class); 46 | 47 | @Override 48 | protected boolean knowsType(Type type) { 49 | if (super.knowsType(type)) 50 | return true; 51 | return knownTypes.contains(type); 52 | } 53 | 54 | @Override 55 | protected AbstractBuilder builderFor(Type type) { 56 | if (super.knowsType(type)) 57 | return super.builderFor(type); 58 | if (type == Address.class) { 59 | return GeoMakers.address(); 60 | } else if (type == AS1Position.class) { 61 | return GeoMakers.as1Position(); 62 | } else if (type == Place.class) { 63 | return GeoMakers.place(); 64 | } else return null; 65 | } 66 | 67 | @Override 68 | protected Model modelFor(Type type) { 69 | if (super.knowsType(type)) 70 | return super.modelFor(type); 71 | if (type == Address.class) { 72 | return schema().forObjectClassOrType( 73 | Address.Builder.class, 74 | "address"); 75 | } else if (type == AS1Position.class) { 76 | return schema().forObjectClassOrType( 77 | AS1Position.Builder.class, 78 | "position"); 79 | } else if (type == Place.class) { 80 | return schema().forObjectClassOrType( 81 | Place.Builder.class, 82 | "place"); 83 | } else return null; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/AudioVisual.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.base.Supplier; 27 | import com.ibm.common.activitystreams.ASObject; 28 | 29 | /** 30 | * For the legacy "audio" and "video" objectTypes. These have 31 | * a String "embedCode" property whose value specifies a 32 | * snippet of HTML for embedding the resource and a stream 33 | * MediaLink property. 34 | * 35 | * @author james 36 | */ 37 | public final class AudioVisual 38 | extends ASObject { 39 | 40 | public static final class Builder 41 | extends ASObject.AbstractBuilder { 42 | 43 | /** 44 | * Set the embedCode 45 | * @param embed String 46 | * @return Builder 47 | */ 48 | public Builder embedCode(String embed) { 49 | return set("embedCode", embed); 50 | } 51 | 52 | /** 53 | * Set the stream MediaLink 54 | * @param mediaLink MediaLink 55 | * @return Builder 56 | */ 57 | public Builder stream(MediaLink mediaLink) { 58 | return set("stream", mediaLink); 59 | } 60 | 61 | /** 62 | * Set the stream MediaLink 63 | * @param mediaLink Supplier<MediaLink> 64 | * @return Builder 65 | */ 66 | public Builder stream(Supplier mediaLink) { 67 | return stream(mediaLink.get()); 68 | } 69 | 70 | /** 71 | * Get the built AudioVisual object 72 | */ 73 | public AudioVisual get() { 74 | return new AudioVisual(this); 75 | } 76 | 77 | } 78 | 79 | private AudioVisual(Builder builder) { 80 | super(builder); 81 | } 82 | 83 | /** 84 | * Get the embedCode property. This should be a snippet of HTML 85 | * @return String 86 | */ 87 | public String embedCode() { 88 | return getString("embedCode"); 89 | } 90 | 91 | /** 92 | * Get the stream MediaLink or null if not provided 93 | * @return MediaLink 94 | */ 95 | public MediaLink stream() { 96 | return this.get("stream"); 97 | } 98 | 99 | // Java Serialization Support 100 | 101 | Object writeReplace() throws java.io.ObjectStreamException { 102 | return new SerializedForm(this); 103 | } 104 | 105 | private static class SerializedForm 106 | extends AbstractSerializedForm { 107 | private static final long serialVersionUID = -2060301713159936285L; 108 | protected SerializedForm(AudioVisual obj) { 109 | super(obj); 110 | } 111 | Object readResolve() throws ObjectStreamException { 112 | return super.doReadResolve(); 113 | } 114 | protected AudioVisual.Builder builder() { 115 | return new Builder(); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/Bookmark.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.ibm.common.activitystreams.ASObject; 27 | 28 | /** 29 | * The legacy "bookmark" objectType 30 | * @author james 31 | */ 32 | public class Bookmark 33 | extends ASObject { 34 | 35 | public static final class Builder 36 | extends ASObject.AbstractBuilder { 37 | 38 | Builder() { 39 | objectType("bookmark"); 40 | } 41 | 42 | /** 43 | * Set the targetUrl property 44 | * @param url String 45 | * @return Builder 46 | */ 47 | public Builder targetUrl(String url) { 48 | return set("targetUrl", url); 49 | } 50 | 51 | /** 52 | * Get the built Bookmark object 53 | */ 54 | public Bookmark get() { 55 | return new Bookmark(this); 56 | } 57 | 58 | } 59 | 60 | private Bookmark(Builder builder) { 61 | super(builder); 62 | } 63 | 64 | /** 65 | * Get the targetUrl property 66 | * @return String 67 | */ 68 | public String targetUrl() { 69 | return getString("targetUrl"); 70 | } 71 | 72 | // Java Serialization Support 73 | 74 | Object writeReplace() throws java.io.ObjectStreamException { 75 | return new SerializedForm(this); 76 | } 77 | 78 | private static class SerializedForm 79 | extends AbstractSerializedForm { 80 | private static final long serialVersionUID = -2060301713159936285L; 81 | protected SerializedForm(Bookmark obj) { 82 | super(obj); 83 | } 84 | Object readResolve() throws ObjectStreamException { 85 | return super.doReadResolve(); 86 | } 87 | protected Bookmark.Builder builder() { 88 | return new Builder(); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/Compression.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.io.IOException; 25 | import java.io.InputStream; 26 | import java.io.OutputStream; 27 | import java.util.zip.DeflaterInputStream; 28 | import java.util.zip.DeflaterOutputStream; 29 | import java.util.zip.GZIPInputStream; 30 | import java.util.zip.GZIPOutputStream; 31 | 32 | /** 33 | * Compression utility for use with the Binary object 34 | * @author james 35 | * 36 | * @param <O extends OutputStream> 37 | * @param <I extends InputStream> 38 | */ 39 | public interface Compression { 40 | String label(); 41 | O compressor(OutputStream wrap) throws IOException; 42 | I decompressor(InputStream in) throws IOException; 43 | void finish(OutputStream out) throws IOException; 44 | 45 | public static final Compression GZipCompression = 46 | new Compression() { 47 | public String label() { 48 | return "gzip"; 49 | } 50 | public GZIPOutputStream compressor(OutputStream wrap) throws IOException { 51 | return new GZIPOutputStream(wrap); 52 | } 53 | public GZIPInputStream decompressor(InputStream in) throws IOException { 54 | return new GZIPInputStream(in); 55 | } 56 | public void finish(OutputStream out) throws IOException { 57 | if (out instanceof GZIPOutputStream) 58 | ((GZIPOutputStream)out).finish(); 59 | } 60 | }; 61 | 62 | public static final Compression DeflateCompression = 63 | new Compression() { 64 | public String label() { 65 | return "deflate"; 66 | } 67 | public DeflaterOutputStream compressor(OutputStream wrap) 68 | throws IOException { 69 | return new DeflaterOutputStream(wrap); 70 | } 71 | public DeflaterInputStream decompressor(InputStream in) 72 | throws IOException { 73 | return new DeflaterInputStream(in); 74 | } 75 | public void finish(OutputStream out) throws IOException { 76 | if (out instanceof DeflaterOutputStream) 77 | ((DeflaterOutputStream)out).finish(); 78 | } 79 | }; 80 | 81 | } 82 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/File.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.net.MediaType; 27 | import com.ibm.common.activitystreams.ASObject; 28 | 29 | /** 30 | * The legacy "file" objectType 31 | * @author james 32 | */ 33 | public class File 34 | extends ASObject { 35 | 36 | public static final class Builder 37 | extends ASObject.AbstractBuilder { 38 | 39 | Builder() { 40 | objectType("file"); 41 | } 42 | 43 | /** 44 | * Set the fileUrl property 45 | * @param url String 46 | * @return Builder 47 | */ 48 | public Builder fileUrl(String url) { 49 | return set("fileUrl", url); 50 | } 51 | 52 | /** 53 | * Set the MIME mediaType using the legacy "mimeType" property 54 | * name rather than the AS 2.0 "mediaType" property name 55 | */ 56 | @Override 57 | public Builder mediaType(MediaType mt) { 58 | return set("mimeType", mt); 59 | } 60 | 61 | /** 62 | * Get the built File object 63 | */ 64 | public File get() { 65 | return new File(this); 66 | } 67 | 68 | } 69 | 70 | private File(Builder builder) { 71 | super(builder); 72 | } 73 | 74 | /** 75 | * Get the MIME mediaType using the legacy "mimeType" property 76 | * name rather than the AS 2.0 "mediaType" property 77 | */ 78 | @Override 79 | public MediaType mediaType() { 80 | return this.get("mimeType"); 81 | } 82 | 83 | /** 84 | * Get the fileUrl property 85 | * @return 86 | */ 87 | public String fileUrl() { 88 | return getString("fileUrl"); 89 | } 90 | 91 | // Java Serialization Support 92 | 93 | Object writeReplace() throws java.io.ObjectStreamException { 94 | return new SerializedForm(this); 95 | } 96 | 97 | private static class SerializedForm 98 | extends AbstractSerializedForm { 99 | private static final long serialVersionUID = -2060301713159936285L; 100 | protected SerializedForm(File obj) { 101 | super(obj); 102 | } 103 | Object readResolve() throws ObjectStreamException { 104 | return super.doReadResolve(); 105 | } 106 | protected File.Builder builder() { 107 | return new Builder(); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/Issue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.collect.ImmutableList; 27 | import com.ibm.common.activitystreams.ASObject; 28 | 29 | /** 30 | * The legacy "issue" objectType. 31 | * @author james 32 | * 33 | */ 34 | public class Issue 35 | extends ASObject { 36 | 37 | public static final class Builder 38 | extends ASObject.AbstractBuilder { 39 | 40 | Builder() { 41 | objectType("issue"); 42 | } 43 | 44 | /** 45 | * Set the "types" property 46 | * @param type String 47 | * @param types String... optional vararg of additional types to set 48 | * @return Builder 49 | */ 50 | public Builder types(String type, String... types) { 51 | ImmutableList.Builder list = 52 | ImmutableList.builder(); 53 | if (type != null) 54 | list.add(type); 55 | if (types != null) 56 | list.add(types); 57 | return types(list.build()); 58 | } 59 | 60 | /** 61 | * Set the "types" property 62 | * @param types Iterable<String> 63 | * @return Builder 64 | */ 65 | public Builder types(Iterable types) { 66 | return set("types", types); 67 | } 68 | 69 | /** 70 | * Get the built Issue object 71 | */ 72 | public Issue get() { 73 | return new Issue(this); 74 | } 75 | 76 | } 77 | 78 | private Issue(Builder builder) { 79 | super(builder); 80 | } 81 | 82 | /** 83 | * Get the listing of types 84 | * @return Iterable<String> 85 | */ 86 | public Iterable types() { 87 | return this.>get("types"); 88 | } 89 | 90 | // Java Serialization Support ` 91 | 92 | Object writeReplace() throws java.io.ObjectStreamException { 93 | return new SerializedForm(this); 94 | } 95 | 96 | private static class SerializedForm 97 | extends AbstractSerializedForm { 98 | private static final long serialVersionUID = -2060301713159936285L; 99 | protected SerializedForm(Issue obj) { 100 | super(obj); 101 | } 102 | Object readResolve() throws ObjectStreamException { 103 | return super.doReadResolve(); 104 | } 105 | protected Issue.Builder builder() { 106 | return new Builder(); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/LegacyObjectAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.lang.reflect.Type; 25 | 26 | import com.google.common.collect.ImmutableSet; 27 | import com.ibm.common.activitystreams.ASObject.AbstractBuilder; 28 | import com.ibm.common.activitystreams.internal.ASObjectAdapter; 29 | import com.ibm.common.activitystreams.internal.Model; 30 | import com.ibm.common.activitystreams.internal.Schema; 31 | import static com.ibm.common.activitystreams.Makers.object; 32 | 33 | public class LegacyObjectAdapter 34 | extends ASObjectAdapter { 35 | 36 | protected LegacyObjectAdapter(Schema schema) { 37 | super(schema); 38 | } 39 | 40 | @SuppressWarnings("unchecked") 41 | public static final ImmutableSet knownTypes = 42 | ImmutableSet.of( 43 | Binary.class, 44 | AudioVisual.class, 45 | Bookmark.class, 46 | Event.class, 47 | File.class, 48 | Issue.class, 49 | Membership.class, 50 | Question.class, 51 | Task.class, 52 | WithImage.class); 53 | 54 | @Override 55 | protected boolean knowsType(Type type) { 56 | if (super.knowsType(type)) 57 | return true; 58 | return knownTypes.contains(type); 59 | } 60 | 61 | @Override 62 | protected AbstractBuilder builderFor(Type type) { 63 | if (super.knowsType(type)) 64 | return super.builderFor(type); 65 | if (knowsType(type)) { 66 | if (type == AudioVisual.class) 67 | return LegacyMakers.audioVisual(); 68 | else if (type == Binary.class) 69 | return LegacyMakers.binary(); 70 | else if (type == Bookmark.class) 71 | return LegacyMakers.bookmark(); 72 | else if (type == Event.class) 73 | return LegacyMakers.event(); 74 | else if (type == File.class) 75 | return LegacyMakers.file(); 76 | else if (type == Issue.class) 77 | return LegacyMakers.issue(); 78 | else if (type == Membership.class) 79 | return LegacyMakers.membership(); 80 | else if (type == Question.class) 81 | return LegacyMakers.question(); 82 | else if (type == Task.class) 83 | return LegacyMakers.task(); 84 | else if (type == WithImage.class) 85 | return LegacyMakers.withImage(); 86 | else return object(); 87 | } else return null; 88 | } 89 | 90 | @Override 91 | protected Model modelFor(Type type) { 92 | if (super.knowsType(type)) 93 | return super.modelFor(type); 94 | if (knowsType(type)) { 95 | if (type == AudioVisual.class) 96 | return LegacyModule.audioVisual; 97 | else if (type == Bookmark.class) 98 | return LegacyModule.bookmark; 99 | else if (type == Binary.class) 100 | return LegacyModule.binary; 101 | else if (type == Event.class) 102 | return LegacyModule.event; 103 | else if (type == File.class) 104 | return LegacyModule.file; 105 | else if (type == Issue.class) 106 | return LegacyModule.issue; 107 | else if (type == Membership.class) 108 | return LegacyModule.membership; 109 | else if (type == Question.class) 110 | return LegacyModule.question; 111 | else if (type == Task.class) 112 | return LegacyModule.task; 113 | else if (type == WithImage.class) 114 | return LegacyModule.withImage; 115 | else return Schema.object; 116 | } else return null; 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/MediaLinkAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.lang.reflect.Type; 25 | import java.util.Map; 26 | 27 | import static com.google.common.base.Preconditions.checkArgument; 28 | 29 | import com.google.gson.JsonDeserializationContext; 30 | import com.google.gson.JsonElement; 31 | import com.google.gson.JsonObject; 32 | import com.google.gson.JsonParseException; 33 | import com.google.gson.JsonSerializationContext; 34 | import com.ibm.common.activitystreams.ASObject; 35 | import static com.ibm.common.activitystreams.internal.ASObjectAdapter.primConverter; 36 | import com.ibm.common.activitystreams.internal.Adapter; 37 | 38 | public final class MediaLinkAdapter 39 | extends Adapter { 40 | 41 | public JsonElement serialize( 42 | MediaLink src, 43 | Type typeOfSrc, 44 | JsonSerializationContext context) { 45 | JsonObject el = new JsonObject(); 46 | for (String key : src) { 47 | Object val = src.get(key); 48 | if (val != null) 49 | el.add(key, context.serialize(val, val.getClass())); 50 | } 51 | return el; 52 | } 53 | 54 | public MediaLink deserialize( 55 | JsonElement json, 56 | Type typeOfT, 57 | JsonDeserializationContext context) 58 | throws JsonParseException { 59 | 60 | checkArgument(json.isJsonObject()); 61 | JsonObject obj = (JsonObject) json; 62 | MediaLink.Builder builder = 63 | LegacyMakers.mediaLink(); 64 | for (Map.Entry entry : obj.entrySet()) { 65 | String name = entry.getKey(); 66 | JsonElement val = entry.getValue(); 67 | if (val.isJsonPrimitive()) 68 | builder.set( 69 | name, 70 | primConverter.convert(val.getAsJsonPrimitive())); 71 | else if (val.isJsonArray()) 72 | builder.set( 73 | name, 74 | context.deserialize(val, Iterable.class)); 75 | else if (val.isJsonObject()) 76 | builder.set( 77 | name, 78 | context.deserialize( 79 | val, 80 | ASObject.class)); 81 | } 82 | return builder.get(); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/Membership.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.base.Supplier; 27 | import com.ibm.common.activitystreams.ASObject; 28 | import com.ibm.common.activitystreams.Collection; 29 | 30 | /** 31 | * For the legacy "role", "group" and "team" objectTypes. 32 | * Membership objects include an additional "members" 33 | * collection property. 34 | * 35 | * @author james 36 | * 37 | */ 38 | public class Membership extends ASObject { 39 | 40 | public static final class Builder 41 | extends ASObject.AbstractBuilder { 42 | 43 | /** 44 | * Set the members collection 45 | * @param collection Collection 46 | * @return Builder 47 | */ 48 | public Builder members(Collection collection) { 49 | return set("members", collection); 50 | } 51 | 52 | /** 53 | * Set the members collection 54 | * @param collection Collection 55 | * @return Builder 56 | */ 57 | public Builder members(Supplier collection) { 58 | return members(collection.get()); 59 | } 60 | 61 | /** 62 | * Get the built Membership object 63 | */ 64 | public Membership get() { 65 | return new Membership(this); 66 | } 67 | 68 | } 69 | 70 | private Membership(Builder builder) { 71 | super(builder); 72 | } 73 | 74 | /** 75 | * Return the members collection 76 | * @return Collection 77 | */ 78 | public Collection members() { 79 | return this.get("members"); 80 | } 81 | 82 | // Java Serialization Support 83 | 84 | Object writeReplace() throws java.io.ObjectStreamException { 85 | return new SerializedForm(this); 86 | } 87 | 88 | private static class SerializedForm 89 | extends AbstractSerializedForm { 90 | private static final long serialVersionUID = -2060301713159936285L; 91 | protected SerializedForm(Membership obj) { 92 | super(obj); 93 | } 94 | Object readResolve() throws ObjectStreamException { 95 | return super.doReadResolve(); 96 | } 97 | protected Membership.Builder builder() { 98 | return new Builder(); 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/Question.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.base.Predicate; 27 | import com.google.common.base.Supplier; 28 | import com.ibm.common.activitystreams.ASObject; 29 | import com.ibm.common.activitystreams.LinkValue; 30 | 31 | /** 32 | * The legacy "question" object. Question objects 33 | * have an additional "options" property that lists 34 | * the possible answers to the question. 35 | * 36 | * This implementation varies from the Legacy AS 1.0 37 | * model in that it allows AS 2.0 style LinkValues 38 | * to be used as the value of the options property. 39 | * 40 | * @author james 41 | * 42 | */ 43 | public class Question extends ASObject { 44 | 45 | public static final class Builder 46 | extends ASObject.AbstractBuilder { 47 | 48 | Builder() { 49 | objectType("question"); 50 | } 51 | 52 | /** 53 | * Add one or more answers to the question 54 | * @param url String 55 | * @param urls String[] optional vararg 56 | * @return Builder 57 | */ 58 | public Builder option(String url, String... urls) { 59 | if (url != null) 60 | link("options", url); 61 | if (urls != null) 62 | for (String u : urls) 63 | link("options", u); 64 | return this; 65 | } 66 | 67 | /** 68 | * Add one or more answers to the question 69 | * @param url LinkValue 70 | * @param urls LinkValue[] optional vararg 71 | * @return Builder 72 | */ 73 | public Builder option(LinkValue link, LinkValue... links) { 74 | if (link != null) 75 | link("options", link); 76 | if (links != null) 77 | for (LinkValue l : links) 78 | link("options", l); 79 | return this; 80 | } 81 | 82 | /** 83 | * Add an answer to the question 84 | * @param link Supplier<? extends LinkValue> 85 | * @return Builder 86 | */ 87 | public Builder option(Supplier link) { 88 | return option(link.get()); 89 | } 90 | 91 | /** 92 | * Get the built question object 93 | */ 94 | public Question get() { 95 | return new Question(this); 96 | } 97 | 98 | } 99 | 100 | public Question(Builder builder) { 101 | super(builder); 102 | } 103 | 104 | /** 105 | * Get the list of options for the question 106 | * @return Iterable<LinkValue> 107 | */ 108 | public Iterable options() { 109 | return links("options"); 110 | } 111 | 112 | /** 113 | * Get the list of options for the question 114 | * @param filter Predicate<? super LinkValue> 115 | * @return Iterable<LinkValue> 116 | */ 117 | public Iterable options(Predicate filter) { 118 | return links("options", filter); 119 | } 120 | 121 | // Java Serialization Support 122 | 123 | Object writeReplace() throws java.io.ObjectStreamException { 124 | return new SerializedForm(this); 125 | } 126 | 127 | private static class SerializedForm 128 | extends AbstractSerializedForm { 129 | private static final long serialVersionUID = -2060301713159936285L; 130 | protected SerializedForm(Question obj) { 131 | super(obj); 132 | } 133 | Object readResolve() throws ObjectStreamException { 134 | return super.doReadResolve(); 135 | } 136 | protected Question.Builder builder() { 137 | return new Builder(); 138 | } 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /legacy/src/main/java/com/ibm/common/activitystreams/legacy/WithImage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.legacy; 23 | 24 | import java.io.ObjectStreamException; 25 | 26 | import com.google.common.base.Supplier; 27 | import com.ibm.common.activitystreams.ASObject; 28 | 29 | /** 30 | * For the legacy "product" and "image" objectTypes. These 31 | * include an additional "fullImage" property whose value 32 | * is a MediaLink. 33 | * 34 | * @author james 35 | * 36 | */ 37 | public class WithImage extends ASObject { 38 | 39 | public static final class Builder 40 | extends ASObject.AbstractBuilder { 41 | 42 | /** 43 | * Set the fullImage property 44 | * @param link MediaLink 45 | * @return Builder 46 | */ 47 | public Builder fullImage(MediaLink link) { 48 | return set("fullImage", link); 49 | } 50 | 51 | /** 52 | * Set the fullImage property 53 | * @param link Supplier<? extends MediaLink> 54 | * @return Builder 55 | */ 56 | public Builder fullImage(Supplier link) { 57 | return fullImage(link.get()); 58 | } 59 | 60 | public WithImage get() { 61 | return new WithImage(this); 62 | } 63 | 64 | } 65 | 66 | private WithImage(Builder builder) { 67 | super(builder); 68 | } 69 | 70 | /** 71 | * Get the fullImage property 72 | * @return MediaLink 73 | */ 74 | public MediaLink fullImage() { 75 | return this.get("fullImage"); 76 | } 77 | 78 | // Java Serialization Support 79 | 80 | Object writeReplace() throws java.io.ObjectStreamException { 81 | return new SerializedForm(this); 82 | } 83 | 84 | private static class SerializedForm 85 | extends AbstractSerializedForm { 86 | private static final long serialVersionUID = -2060301713159936285L; 87 | protected SerializedForm(WithImage obj) { 88 | super(obj); 89 | } 90 | Object readResolve() throws ObjectStreamException { 91 | return super.doReadResolve(); 92 | } 93 | protected WithImage.Builder builder() { 94 | return new Builder(); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.ibm.common 4 | activitystreams 5 | 0.0.1-SNAPSHOT 6 | pom 7 | Activity Streams 2.0 Java Reference Implementation 8 | 9 | 10 | 11 | jasnell 12 | James M Snell 13 | jasnell@us.ibm.com 14 | IBM 15 | http://www.ibm.com 16 | 17 | owner 18 | developer 19 | 20 | -8 21 | 22 | 23 | 24 | 25 | 26 | Apache License, Version 2.0 27 | http://www.apache.org/licenses/LICENSE-2.0.txt 28 | repo 29 | 30 | 31 | 32 | 33 | IBM 34 | http://www.ibm.com 35 | 36 | 37 | scm:https://github.com/OpenSocial/incubator.git 38 | http://github.com/OpenSocial/activitystreams 39 | 40 | 41 | 42 | UTF-8 43 | 44 | 45 | 46 | core 47 | actions 48 | geo 49 | assembly 50 | legacy 51 | typext 52 | 53 | 54 | 55 | 56 | com.google.code.gson 57 | gson 58 | 2.2.4 59 | 60 | 61 | com.google.guava 62 | guava 63 | 16.0.1 64 | 65 | 66 | joda-time 67 | joda-time 68 | 2.3 69 | 70 | 71 | 72 | junit 73 | junit 74 | 4.11 75 | test 76 | 77 | 78 | 79 | http://github.com/OpenSocial/activitystreams 80 | An implementation of the Activity Streams 2.0 data format. 81 | 2013 82 | 83 | https://github.com/OpenSocial/activitystreams/issues 84 | 85 | 86 | -------------------------------------------------------------------------------- /typext/EXPERIMENTAL: -------------------------------------------------------------------------------- 1 | This module is currently experimental -------------------------------------------------------------------------------- /typext/README.md: -------------------------------------------------------------------------------- 1 | # Type Value Extension Support 2 | 3 | This module provides two basic pieces of functionality relating to the 4 | use of extension Type Value definitions: 5 | 6 | 1. Verb and ObjectType objects 7 | 8 | 2. A TypeValueRegistry that is used to resolve information about extension type value identifiers 9 | 10 | ## The TypeValueRegistry 11 | 12 | Suppose we have the following Activity object: 13 | 14 | ```json 15 | { 16 | "actor": "acct:john@example.org", 17 | "verb": "http://example.org/verbs/like", 18 | "object": "http://example.com/notes/1" 19 | } 20 | ``` 21 | 22 | The verb "http://example.org/verbs/like" is an "extension verb". The first 23 | time an application encounters an extension verb, it may not have any idea 24 | what to do with it, or what exactly it means. The TypeValueRegistry is 25 | intended to provide a partial solution to that problem by allowing an 26 | application to resolve simple TypeValue identifiers into rich object 27 | identifiers. 28 | 29 | For example: 30 | 31 | ```java 32 | TypeValueRegistry tvr = 33 | TypeValueRegistry 34 | .makeDefaultSilent(io); 35 | 36 | TypeValue simple = Makers.type("http://example.org/verbs/like"); 37 | 38 | Future object = tvr.resolve(simple); 39 | 40 | ASObject obj = (ASObject)object.get(); 41 | ``` 42 | 43 | By default, the TypeValueRegistry above does three things: 44 | 45 | 1. When created, the TypeValueRegistry checks the java classpath for Activity 46 | Stream 2.0 Collection documents that contain TypeValue definitions. If 47 | found, these are preloaded into an in-memory cache. 48 | 49 | 2. When asked to resolve a simple TypeValue, the TypeValueRegistry will first 50 | check to see if a resolved object TypeValue already exists in memory. If 51 | it finds one, it returns it. 52 | 53 | 3. If there currently is not a resolved object TypeValue in memory, the 54 | TypeValueRegistry will attempt to do an HTTP fetch on the IRI given by 55 | the TypeValue ID. If the ID is not an HTTP URL, this request will fail. 56 | If the GET returns an Activity Streams 2.0 document, the document is 57 | parsed and is examined for information about the given ID. If found, 58 | this information is cached and a resolved object TypeValue is 59 | returned. 60 | 61 | In other words, let's say that the URL "http://example.org/verbs/like" 62 | points to the following Activity Streams 2.0 collection document: 63 | 64 | ```json 65 | { 66 | "objectType": "collection", 67 | "items": [ 68 | { 69 | "objectType": "verb", 70 | "id": "http://example.org/verbs/like", 71 | "displayName": "like" 72 | }, 73 | { 74 | "objectType": "objectType", 75 | "id": "http://example.org/objects/note", 76 | "displayName": "note" 77 | } 78 | ] 79 | } 80 | ``` 81 | 82 | The TypeValueRegistry will, by default, find and cache both of the TypeValue 83 | definitions included in the document. Then, it will return the object that 84 | contains "id": "http://example.org/verbs/like". 85 | 86 | Because I used the "makeDefaultSilent" method, if there are any errors 87 | encountered throughout this process, the process will be aborted and the 88 | original simple TypeValue will be returned. 89 | 90 | ### Customizing the TypeValueRegistry 91 | 92 | The TypeValueRegistry implementation can be customized by specifying your 93 | own ResolutionStrategy and PreloadStrategy implementations using the 94 | TypeValueRegistry.Builder. You can also provide your own ExecutorService 95 | implementation. Refer to the Javadocs for details. 96 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/ext/ExtAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013 OpenSocial Foundation 3 | * Copyright 2013 International Business Machines Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Utility library for working with Activity Streams Actions 18 | * Requires underscorejs. 19 | * 20 | * @author James M Snell (jasnell@us.ibm.com) 21 | */ 22 | package com.ibm.common.activitystreams.ext; 23 | 24 | import java.lang.reflect.Type; 25 | 26 | import com.google.common.collect.ImmutableSet; 27 | import com.ibm.common.activitystreams.ASObject.AbstractBuilder; 28 | import com.ibm.common.activitystreams.internal.ASObjectAdapter; 29 | import com.ibm.common.activitystreams.internal.Model; 30 | import com.ibm.common.activitystreams.internal.Schema; 31 | 32 | public class ExtAdapter 33 | extends ASObjectAdapter { 34 | 35 | protected ExtAdapter(Schema schema) { 36 | super(schema); 37 | } 38 | 39 | private static final ImmutableSet knownTypes = 40 | ImmutableSet.of(Verb.class,ObjectType.class); 41 | 42 | @Override 43 | protected boolean knowsType(Type type) { 44 | if (super.knowsType(type)) 45 | return true; 46 | return knownTypes.contains(type); 47 | } 48 | 49 | @Override 50 | protected AbstractBuilder builderFor(Type type) { 51 | if (super.knowsType(type)) 52 | return super.builderFor(type); 53 | if (type == Verb.class) { 54 | return ExtMakers.verb(); 55 | } else if (type == ObjectType.class) { 56 | return ExtMakers.objectType(); 57 | } else return null; 58 | } 59 | 60 | @Override 61 | protected Model modelFor(Type type) { 62 | if (super.knowsType(type)) 63 | return super.modelFor(type); 64 | if (type == Verb.class) { 65 | return schema().forObjectClassOrType( 66 | Verb.Builder.class, 67 | "verb"); 68 | } else if (type == ObjectType.class) { 69 | return schema().forObjectClassOrType( 70 | ObjectType.Builder.class, 71 | "objectType"); 72 | } else return null; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/ext/ExtMakers.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.ext; 2 | 3 | public final class ExtMakers { 4 | 5 | private ExtMakers() {} 6 | 7 | public static Verb.Builder verb() { 8 | return new Verb.Builder(); 9 | } 10 | 11 | public static Verb.Builder verb(String id) { 12 | return verb().id(id); 13 | } 14 | 15 | public static ObjectType.Builder objectType() { 16 | return new ObjectType.Builder(); 17 | } 18 | 19 | public static ObjectType.Builder objectType(String id) { 20 | return objectType().id(id); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/ext/ExtModule.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.ext; 2 | 3 | import com.ibm.common.activitystreams.IO; 4 | import com.ibm.common.activitystreams.internal.Model; 5 | import com.ibm.common.activitystreams.internal.Schema; 6 | import com.ibm.common.activitystreams.internal.Schema.Builder; 7 | import com.ibm.common.activitystreams.util.Module; 8 | 9 | public class ExtModule implements Module { 10 | 11 | public static Module instance = new ExtModule(); 12 | 13 | public static final Model verb = 14 | Schema.object.template() 15 | .type(Verb.class, Verb.Builder.class) 16 | .get(); 17 | 18 | public static final Model objectType = 19 | Schema.object.template() 20 | .type(ObjectType.class, ObjectType.Builder.class) 21 | .get(); 22 | 23 | public void apply(Builder builder) { 24 | builder.map("verb", verb); 25 | builder.map("objectType", objectType); 26 | } 27 | 28 | public void apply( 29 | IO.Builder builder, 30 | Schema schema) { 31 | ExtAdapter base = new ExtAdapter(schema); 32 | builder.adapter(Verb.class, base); 33 | builder.adapter(ObjectType.class, base); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/ext/ObjectType.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.ext; 2 | 3 | import com.ibm.common.activitystreams.ASObject; 4 | 5 | public final class ObjectType 6 | extends ASObject { 7 | 8 | public static final class Builder 9 | extends ASObject.AbstractBuilder { 10 | 11 | Builder() { 12 | objectType("objectType"); 13 | } 14 | 15 | public ObjectType get() { 16 | return new ObjectType(this); 17 | } 18 | 19 | } 20 | 21 | private ObjectType(Builder builder) { 22 | super(builder); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/ext/Verb.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.ext; 2 | 3 | import com.ibm.common.activitystreams.ASObject; 4 | 5 | public final class Verb 6 | extends ASObject { 7 | 8 | public static final class Builder 9 | extends ASObject.AbstractBuilder { 10 | 11 | Builder() { 12 | objectType("verb"); 13 | } 14 | 15 | public Verb get() { 16 | return new Verb(this); 17 | } 18 | 19 | } 20 | 21 | private Verb(Builder builder) { 22 | super(builder); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/registry/CachingResolutionStrategy.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.registry; 2 | 3 | import static com.google.common.base.Throwables.propagate; 4 | 5 | import java.util.concurrent.Callable; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | import com.google.common.base.Supplier; 9 | import com.google.common.cache.CacheBuilder; 10 | import com.google.common.cache.CacheLoader; 11 | import com.google.common.cache.LoadingCache; 12 | import com.ibm.common.activitystreams.Makers; 13 | import com.ibm.common.activitystreams.TypeValue; 14 | import com.ibm.common.activitystreams.ValueType; 15 | 16 | public abstract class CachingResolutionStrategy 17 | implements ResolutionStrategy { 18 | 19 | @SuppressWarnings("unchecked") 20 | public static abstract class AbstractBuilder 21 | > 22 | implements Supplier { 23 | 24 | private boolean silentfail = false; 25 | private final CacheBuilder cache = 26 | CacheBuilder.newBuilder() 27 | .expireAfterAccess(10, TimeUnit.MINUTES) 28 | .expireAfterWrite(10, TimeUnit.MINUTES) 29 | .maximumSize(100) 30 | .initialCapacity(50); 31 | 32 | public B silentfail() { 33 | this.silentfail = true; 34 | return (B)this; 35 | } 36 | 37 | public B customizeCache(Receiver> receiver) { 38 | if (receiver != null) 39 | receiver.receive(cache); 40 | return (B)this; 41 | } 42 | 43 | } 44 | 45 | private final LoadingCache cache; 46 | private final boolean silentfail; 47 | 48 | protected LoadingCache cache() { 49 | return cache; 50 | } 51 | 52 | CachingResolutionStrategy(AbstractBuilder builder) { 53 | this.cache = initCache(builder); 54 | this.silentfail = builder.silentfail; 55 | } 56 | 57 | protected boolean silentfail() { 58 | return silentfail; 59 | } 60 | 61 | private LoadingCache initCache(AbstractBuilder builder) { 62 | return builder.cache.build(loader()); 63 | } 64 | 65 | public Callable resolverFor(TypeValue tv) { 66 | return new Resolver(tv); 67 | } 68 | 69 | protected abstract CacheLoader loader(); 70 | 71 | public final class Resolver 72 | implements Callable { 73 | 74 | private final TypeValue input; 75 | 76 | Resolver(TypeValue input) { 77 | this.input = input; 78 | } 79 | 80 | public TypeValue call() throws Exception { 81 | try { 82 | if (input == null) return null; 83 | switch(input.valueType()) { 84 | case OBJECT: 85 | return input; 86 | case SIMPLE: 87 | return cache.get(input); 88 | default: 89 | throw new IllegalArgumentException(); 90 | } 91 | } catch (Throwable t) { 92 | if (silentfail()) 93 | return input; 94 | else throw propagate(t); 95 | } 96 | } 97 | 98 | } 99 | 100 | public Receiver preloader() { 101 | return new CachePreloader(cache()); 102 | } 103 | 104 | private static final class CachePreloader 105 | implements Receiver { 106 | 107 | private final LoadingCache cache; 108 | 109 | CachePreloader(LoadingCache cache) { 110 | this.cache = cache; 111 | } 112 | 113 | public void receive(final TypeValue t) { 114 | if (t.valueType() == ValueType.OBJECT && t.id() != null) { 115 | final TypeValue tv = Makers.type(t.id()); 116 | cache.invalidate(tv); 117 | try { 118 | cache.get(tv, new Callable() { 119 | public TypeValue call() { 120 | return t; 121 | } 122 | }); 123 | } catch (Throwable e) {} 124 | } 125 | } 126 | 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/registry/ClasspathPreloader.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.registry; 2 | 3 | import java.io.InputStream; 4 | import java.io.ObjectInputStream; 5 | import java.net.URL; 6 | import java.util.Enumeration; 7 | 8 | import com.google.common.base.Charsets; 9 | import com.google.common.base.Function; 10 | import com.google.common.base.Supplier; 11 | import com.google.common.base.Throwables; 12 | import com.google.common.collect.ImmutableList; 13 | import com.google.common.hash.BloomFilter; 14 | import com.google.common.hash.Funnels; 15 | import com.ibm.common.activitystreams.ASObject; 16 | import com.ibm.common.activitystreams.Collection; 17 | import com.ibm.common.activitystreams.IO; 18 | import com.ibm.common.activitystreams.TypeValue; 19 | 20 | public final class ClasspathPreloader 21 | implements PreloadStrategy { 22 | 23 | public static final class Builder 24 | implements Supplier { 25 | 26 | private ClassLoader loader = 27 | Thread.currentThread().getContextClassLoader(); 28 | private boolean avoidDuplicates = false; 29 | 30 | public Builder avoidDuplicates() { 31 | this.avoidDuplicates = true; 32 | return this; 33 | } 34 | 35 | public Builder classLoader(ClassLoader loader) { 36 | this.loader = loader != null ? 37 | loader : Thread.currentThread().getContextClassLoader(); 38 | return this; 39 | } 40 | 41 | public ClasspathPreloader get() { 42 | return new ClasspathPreloader(this); 43 | } 44 | 45 | } 46 | 47 | private final ClassLoader loader; 48 | private final boolean avoidDuplicates; 49 | 50 | private ClasspathPreloader(Builder builder) { 51 | this.loader = builder.loader; 52 | this.avoidDuplicates = builder.avoidDuplicates; 53 | } 54 | 55 | public void load(IO io, Receiver receiver) { 56 | 57 | final BloomFilter filter = 58 | avoidDuplicates ? 59 | BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), 1000) : null; 60 | 61 | try { 62 | for (InputStream in : streams.apply(loader.getResources("typeValues.bin"))) { 63 | try { 64 | ObjectInputStream oin = new ObjectInputStream(in); 65 | Collection col = (Collection) oin.readObject(); 66 | load(col, receiver, filter); 67 | } catch (Throwable t) {} 68 | } 69 | 70 | for (InputStream in : streams.apply(loader.getResources("typeValues.json"))) { 71 | try { 72 | load(io.readAsCollection(in), receiver, filter); 73 | } catch (Throwable t) {} 74 | } 75 | 76 | } catch (Throwable t) { 77 | throw Throwables.propagate(t); 78 | } 79 | 80 | } 81 | 82 | private void load( 83 | Collection col, 84 | Receiver receiver, 85 | BloomFilter filter) { 86 | if (col != null && receiver != null) 87 | for (ASObject obj : col.items()) 88 | if (obj.id() != null && (filter == null || filter.put(obj.id()))) { 89 | try { 90 | receiver.receive(obj); 91 | } catch (Throwable t) {} 92 | } 93 | } 94 | 95 | private static Function,Iterable> streams = 96 | new Function,Iterable> () { 97 | public Iterable apply(Enumeration input) { 98 | ImmutableList.Builder list = 99 | ImmutableList.builder(); 100 | while(input.hasMoreElements()) { 101 | try { 102 | list.add(input.nextElement().openStream()); 103 | } catch (Throwable t) {} 104 | } 105 | return list.build(); 106 | } 107 | }; 108 | 109 | public static final PreloadStrategy instance = 110 | new Builder().get(); 111 | } 112 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/registry/NonOpResolutionStrategy.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.registry; 2 | 3 | import java.util.concurrent.Callable; 4 | 5 | import com.ibm.common.activitystreams.TypeValue; 6 | 7 | class NonOpResolutionStrategy 8 | implements ResolutionStrategy { 9 | 10 | public Callable resolverFor(final TypeValue tv) { 11 | return new Callable() { 12 | public TypeValue call() throws Exception { 13 | return tv; 14 | } 15 | }; 16 | } 17 | 18 | public Receiver preloader() { 19 | return new Receiver() { 20 | public void receive(TypeValue t) {} 21 | }; 22 | } 23 | 24 | public void shutdown() {} 25 | } 26 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/registry/PreloadStrategy.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.registry; 2 | 3 | import com.ibm.common.activitystreams.IO; 4 | import com.ibm.common.activitystreams.TypeValue; 5 | 6 | /** 7 | * Preloads TypeValue instances. 8 | */ 9 | public interface PreloadStrategy { 10 | 11 | void load(IO io, Receiver receiver); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/registry/Receiver.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.registry; 2 | 3 | public interface Receiver { 4 | 5 | void receive(T t); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/registry/ResolutionStrategy.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.registry; 2 | 3 | import java.util.concurrent.Callable; 4 | 5 | import com.ibm.common.activitystreams.TypeValue; 6 | 7 | /** 8 | * Implements a strategy for resolving TypeValue identifiers. 9 | */ 10 | public interface ResolutionStrategy { 11 | 12 | /** 13 | * Returns the Receiver instance that is used to 14 | * process preloaded TypeValue instances 15 | * @return Receiver<TypeValue> 16 | */ 17 | Receiver preloader(); 18 | 19 | /** 20 | * Returns a Callable that implements TypeValue resolution 21 | * for the given TypeValue. 22 | */ 23 | Callable resolverFor(TypeValue tv); 24 | 25 | /** 26 | * Shutdown and cleanup any resources 27 | */ 28 | void shutdown(); 29 | 30 | public static final ResolutionStrategy nonop = 31 | new NonOpResolutionStrategy(); 32 | } 33 | -------------------------------------------------------------------------------- /typext/src/main/java/com/ibm/common/activitystreams/registry/UncacheableResponseException.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.registry; 2 | 3 | public final class UncacheableResponseException extends RuntimeException { 4 | private static final long serialVersionUID = -8868045836116771952L; 5 | } -------------------------------------------------------------------------------- /typext/src/test/java/com/ibm/common/activitystreams/ext/test/ExtTest.java: -------------------------------------------------------------------------------- 1 | package com.ibm.common.activitystreams.ext.test; 2 | 3 | import java.util.concurrent.Future; 4 | 5 | import org.junit.Test; 6 | 7 | import com.ibm.common.activitystreams.IO; 8 | import com.ibm.common.activitystreams.TypeValue; 9 | import com.ibm.common.activitystreams.ext.ExtModule; 10 | import com.ibm.common.activitystreams.registry.TypeValueRegistry; 11 | 12 | public class ExtTest { 13 | 14 | private static final IO io = IO.makeDefault(ExtModule.instance); 15 | 16 | @Test 17 | public void extTest() throws Exception { 18 | 19 | TypeValueRegistry tvr = 20 | TypeValueRegistry 21 | .makeDefaultSilent(io); 22 | 23 | Future object = tvr.resolve("urn:example:verbs:foo"); 24 | 25 | System.out.println(object.get().valueType()); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /typext/src/test/resources/typeValues.json: -------------------------------------------------------------------------------- 1 | { 2 | "objectType": "collection", 3 | "items": [ 4 | { 5 | "objectType": "verb", 6 | "id": "urn:example:verbs:foo", 7 | "displayName": "Foo" 8 | }, 9 | { 10 | "objectType": "objectType", 11 | "id": "urn:example:types:bar", 12 | "displayName": "Bar" 13 | } 14 | ] 15 | } --------------------------------------------------------------------------------