diff options
Diffstat (limited to 'branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache')
21 files changed, 2407 insertions, 0 deletions
diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/AtomBinding.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/AtomBinding.java new file mode 100644 index 0000000000..00aa813af2 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/AtomBinding.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed; + +import org.apache.tuscany.sca.assembly.Binding; + +/** + * Atom binding model. + * + * @version $Rev$ $Date$ + */ +public interface AtomBinding extends Binding { +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/AtomBindingFactory.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/AtomBindingFactory.java new file mode 100644 index 0000000000..050ba9b73c --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/AtomBindingFactory.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed; + +/** + * Factory for the Atom binding + * + * @version $Rev$ $Date$ + */ +public interface AtomBindingFactory { + + /** + * Creates a new Atom binding. + * @return the new Atom binding + */ + AtomBinding createAtomBinding(); + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/RSSBinding.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/RSSBinding.java new file mode 100644 index 0000000000..81b9fd95ad --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/RSSBinding.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed; + +import org.apache.tuscany.sca.assembly.Binding; + +/** + * RSS Feed binding model. + * + * @version $Rev$ $Date$ + */ +public interface RSSBinding extends Binding { +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/RSSBindingFactory.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/RSSBindingFactory.java new file mode 100644 index 0000000000..c95c8ac012 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/RSSBindingFactory.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed; + +/** + * Factory for the RSS binding + * + * @version $Rev$ $Date$ + */ +public interface RSSBindingFactory { + + /** + * Creates a new RSS binding. + * @return the new RSS binding + */ + RSSBinding createRSSBinding(); + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/Collection.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/Collection.java new file mode 100644 index 0000000000..30068afa0a --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/Collection.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.binding.feed.collection; + +import org.osoa.sca.annotations.Remotable; + +import com.sun.syndication.feed.atom.Entry; + +/** + * Provides access to a collection of resources using Atom. + * + * @version $Rev$ $Date$ + */ +@Remotable +public interface Collection { + + /** + * Get an RSS or Atom feed for a collection of resources. + * + * @return the RSS or Atom feed + */ + com.sun.syndication.feed.atom.Feed getFeed(); + + /** + * Creates a new entry. + * + * @param entry + * @return + */ + Entry post(Entry entry); + + /** + * Retrieves an entry. + * + * @param id + * @return + */ + Entry get(String id) throws NotFoundException; + + /** + * Update an entry. + * + * @param id + * @param entry + * @return + */ + void put(String id, Entry entry) throws NotFoundException; + + /** + * Delete an entry. + * + * @param id + */ + void delete(String id) throws NotFoundException; + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/MediaCollection.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/MediaCollection.java new file mode 100644 index 0000000000..39a4213a65 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/MediaCollection.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.binding.feed.collection; + +import java.io.InputStream; + +import org.osoa.sca.annotations.Remotable; + +import com.sun.syndication.feed.atom.Entry; + +/** + * Provides access to a collection of resources using Atom. + * + * @version $Rev$ $Date$ + */ +@Remotable +public interface MediaCollection extends Collection { + + /** + * Creates a new media entry + * + * @param title + * @param slug + * @param contentType + * @param media + */ + Entry postMedia(String title, String slug, String contentType, InputStream media); + + /** + * Update a media entry. + * + * @param id + * @param contentType + * @param media + * @return + */ + void putMedia(String id, String contentType, InputStream media) throws NotFoundException; + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/NotFoundException.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/NotFoundException.java new file mode 100644 index 0000000000..503f30e685 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/collection/NotFoundException.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.collection; + +/** + * Indicates that a resource could not be found. + * + * @version $Rev$ $Date$ + */ +public class NotFoundException extends Exception { + private static final long serialVersionUID = -5046027674128627383L; + + public NotFoundException() { + } + + public NotFoundException(String message) { + super(message); + } + + public NotFoundException(Throwable cause) { + super(cause); + } + + public NotFoundException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/AtomBindingFactoryImpl.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/AtomBindingFactoryImpl.java new file mode 100644 index 0000000000..a9a1167544 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/AtomBindingFactoryImpl.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.impl; + +import org.apache.tuscany.sca.binding.feed.AtomBinding; +import org.apache.tuscany.sca.binding.feed.AtomBindingFactory; + +/** + * Factory for the Atom binding model. + * + * @version $Rev$ $Date$ + */ +public class AtomBindingFactoryImpl implements AtomBindingFactory { + + public AtomBinding createAtomBinding() { + return new AtomBindingImpl(); + } + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/AtomBindingImpl.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/AtomBindingImpl.java new file mode 100644 index 0000000000..76551bc412 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/AtomBindingImpl.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.impl; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.binding.feed.AtomBinding; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.IntentAttachPointType; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySetAttachPoint; + +/** + * Implementation of the Atom Feed binding model. + * + * @version $Rev$ $Date$ + */ +class AtomBindingImpl implements AtomBinding, PolicySetAttachPoint { + + private String name; + private String uri; + private List<Intent> requiredIntents = new ArrayList<Intent>(); + private List<PolicySet> policySets = new ArrayList<PolicySet>(); + private IntentAttachPointType intentAttachPointType; + private List<PolicySet> applicablePolicySets = new ArrayList<PolicySet>(); + + public List<PolicySet> getApplicablePolicySets() { + return applicablePolicySets; + } + + public String getName() { + return name; + } + + public String getURI() { + return uri; + } + + public void setName(String name) { + this.name = name; + } + + public void setURI(String uri) { + this.uri = uri; + } + + + public boolean isUnresolved() { + // The binding is always resolved + return false; + } + + public void setUnresolved(boolean unresolved) { + // The binding is always resolved + } + + public List<PolicySet> getPolicySets() { + return policySets; + } + + public List<Intent> getRequiredIntents() { + return requiredIntents; + } + + public IntentAttachPointType getType() { + return intentAttachPointType; + } + + public void setType(IntentAttachPointType intentAttachPointType) { + this.intentAttachPointType = intentAttachPointType; + } + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/RSSBindingFactoryImpl.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/RSSBindingFactoryImpl.java new file mode 100644 index 0000000000..4d8fb943f0 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/RSSBindingFactoryImpl.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.impl; + +import org.apache.tuscany.sca.binding.feed.RSSBinding; +import org.apache.tuscany.sca.binding.feed.RSSBindingFactory; + +/** + * Factory for the RSS binding model. + * + * @version $Rev$ $Date$ + */ +public class RSSBindingFactoryImpl implements RSSBindingFactory { + + public RSSBinding createRSSBinding() { + return new RSSBindingImpl(); + } + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/RSSBindingImpl.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/RSSBindingImpl.java new file mode 100644 index 0000000000..9df2ec60da --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/impl/RSSBindingImpl.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.impl; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.binding.feed.RSSBinding; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.IntentAttachPointType; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySetAttachPoint; + +/** + * Implementation of the RSS Feed binding model. + * + * @version $Rev$ $Date$ + */ +class RSSBindingImpl implements RSSBinding, PolicySetAttachPoint { + + private String name; + private String uri; + private List<Intent> requiredIntents = new ArrayList<Intent>(); + private List<PolicySet> policySets = new ArrayList<PolicySet>(); + private IntentAttachPointType intentAttachPointType; + private List<PolicySet> applicablePolicySets = new ArrayList<PolicySet>(); + + public List<PolicySet> getApplicablePolicySets() { + return applicablePolicySets; + } + + public String getName() { + return name; + } + + public String getURI() { + return uri; + } + + public void setName(String name) { + this.name = name; + } + + public void setURI(String uri) { + this.uri = uri; + } + + public boolean isUnresolved() { + return false; + } + + public void setUnresolved(boolean unresolved) { + // The binding is always resolved + } + + public List<PolicySet> getPolicySets() { + return policySets; + } + + public List<Intent> getRequiredIntents() { + return requiredIntents; + } + + public IntentAttachPointType getType() { + return intentAttachPointType; + } + + public void setType(IntentAttachPointType intentAttachPointType) { + this.intentAttachPointType = intentAttachPointType; + } + + public void setPolicySets(List<PolicySet> policySets) { + this.policySets = policySets; + } + + public void setRequiredIntents(List<Intent> intents) { + this.requiredIntents = intents; + } + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomBindingInvoker.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomBindingInvoker.java new file mode 100644 index 0000000000..45b35d9206 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomBindingInvoker.java @@ -0,0 +1,345 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.binding.feed.provider; + +import java.io.InputStreamReader; +import java.io.StringWriter; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.apache.tuscany.sca.binding.feed.collection.NotFoundException; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.DataExchangeSemantics; +import org.osoa.sca.ServiceRuntimeException; + +import com.sun.syndication.feed.atom.Entry; +import com.sun.syndication.feed.atom.Feed; +import com.sun.syndication.io.WireFeedInput; +import com.sun.syndication.io.XmlReader; + +/** + * Invoker for the Atom binding. + * + * @version $Rev$ $Date$ + */ +class AtomBindingInvoker implements Invoker, DataExchangeSemantics { + + Operation operation; + String uri; + HttpClient httpClient; + String authorizationHeader; + + //FIXME Support conversion to/from data api entries + + AtomBindingInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) { + this.operation = operation; + this.uri = uri; + this.httpClient = httpClient; + this.authorizationHeader = authorizationHeader; + } + + public Message invoke(Message msg) { + // Shouldn't get here, as the only supported methods are + // defined in the ResourceCollection interface, and implemented + // by specific invoker subclasses + throw new UnsupportedOperationException(operation.getName()); + } + + public boolean allowsPassByReference() { + return true; + } + + /** + * Get operation invoker + */ + public static class GetInvoker extends AtomBindingInvoker { + + public GetInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) { + super(operation, uri, httpClient, authorizationHeader); + } + + @Override + public Message invoke(Message msg) { + + // Get an entry + String id = (String)((Object[])msg.getBody())[0]; + + // Send an HTTP GET + GetMethod getMethod = new GetMethod(uri + "/" + id); + getMethod.setRequestHeader("Authorization", authorizationHeader); + try { + httpClient.executeMethod(getMethod); + int status = getMethod.getStatusCode(); + + // Read the Atom entry + if (status == 200) { + Entry feedEntry = + AtomFeedEntryUtil.readFeedEntry("atom_1.0", new InputStreamReader(getMethod.getResponseBodyAsStream())); + msg.setBody(feedEntry); + + } else if (status == 404) { + msg.setFaultBody(new NotFoundException()); + } else { + msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); + } + + } catch (Exception e) { + msg.setFaultBody(new ServiceRuntimeException(e)); + } finally { + getMethod.releaseConnection(); + } + + return msg; + } + } + + /** + * Post operation invoker + */ + public static class PostInvoker extends AtomBindingInvoker { + + public PostInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) { + super(operation, uri, httpClient, authorizationHeader); + } + + @Override + public Message invoke(Message msg) { + + // Post an entry + Entry feedEntry = (Entry)((Object[])msg.getBody())[0]; + + // Send an HTTP POST + PostMethod postMethod = new PostMethod(uri); + postMethod.setRequestHeader("Authorization", authorizationHeader); + try { + + // Write the Atom entry + StringWriter writer = new StringWriter(); + AtomFeedEntryUtil.writeFeedEntry(feedEntry, "atom_1.0", writer); + postMethod.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8"); + postMethod.setRequestEntity(new StringRequestEntity(writer.toString())); + + httpClient.executeMethod(postMethod); + int status = postMethod.getStatusCode(); + + // Read the Atom entry + if (status == 200 || status == 201) { + Entry createdEntry = + AtomFeedEntryUtil + .readFeedEntry("atom_1.0", new InputStreamReader(postMethod.getResponseBodyAsStream())); + msg.setBody(createdEntry); + + } else if (status == 404) { + msg.setFaultBody(new NotFoundException()); + } else { + msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); + } + + } catch (Exception e) { + msg.setFaultBody(new ServiceRuntimeException(e)); + } finally { + postMethod.releaseConnection(); + } + + return msg; + } + } + + /** + * Put operation invoker + */ + public static class PutInvoker extends AtomBindingInvoker { + + public PutInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) { + super(operation, uri, httpClient, authorizationHeader); + } + + @Override + public Message invoke(Message msg) { + + // Put an entry + Object[] args = (Object[])msg.getBody(); + String id = (String)args[0]; + Entry feedEntry = (Entry)args[1]; + + // Send an HTTP PUT + PutMethod putMethod = new PutMethod(uri + "/" + id); + putMethod.setRequestHeader("Authorization", authorizationHeader); + try { + + // Write the Atom entry + StringWriter writer = new StringWriter(); + AtomFeedEntryUtil.writeFeedEntry(feedEntry, "atom_1.0", writer); + putMethod.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8"); + putMethod.setRequestEntity(new StringRequestEntity(writer.toString())); + + httpClient.executeMethod(putMethod); + int status = putMethod.getStatusCode(); + + // Read the Atom entry + if (status == 200 || status == 201) { + try { + Entry updatedEntry = + AtomFeedEntryUtil.readFeedEntry("atom_1.0", new InputStreamReader(putMethod + .getResponseBodyAsStream())); + msg.setBody(updatedEntry); + } catch (Exception e) { + // Returning the updated entry is optional + } + + } else if (status == 404) { + msg.setFaultBody(new NotFoundException()); + } else { + msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); + } + + } catch (Exception e) { + msg.setFaultBody(new ServiceRuntimeException(e)); + } finally { + putMethod.releaseConnection(); + } + + return msg; + } + } + + /** + * Delete operation invoker + */ + public static class DeleteInvoker extends AtomBindingInvoker { + + public DeleteInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) { + super(operation, uri, httpClient, authorizationHeader); + } + + @Override + public Message invoke(Message msg) { + + // Delete an entry + String id = (String)((Object[])msg.getBody())[0]; + + // Send an HTTP DELETE + DeleteMethod deleteMethod = new DeleteMethod(uri + "/" + id); + deleteMethod.setRequestHeader("Authorization", authorizationHeader); + try { + httpClient.executeMethod(deleteMethod); + int status = deleteMethod.getStatusCode(); + + // Read the Atom entry + if (status == 200) { + msg.setBody(null); + + } else if (status == 404) { + msg.setFaultBody(new NotFoundException()); + } else { + msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); + } + + } catch (Exception e) { + msg.setFaultBody(new ServiceRuntimeException(e)); + } finally { + deleteMethod.releaseConnection(); + } + + return msg; + } + } + + /** + * GetAll operation invoker + */ + public static class GetAllInvoker extends AtomBindingInvoker { + + public GetAllInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) { + super(operation, uri, httpClient, authorizationHeader); + } + + @Override + public Message invoke(Message msg) { + + // Get a feed + + // Send an HTTP GET + GetMethod getMethod = new GetMethod(uri); + getMethod.setRequestHeader("Authorization", authorizationHeader); + try { + httpClient.executeMethod(getMethod); + int status = getMethod.getStatusCode(); + + // Read the Atom feed + if (status == 200) { + WireFeedInput input = new WireFeedInput(); + Feed feed = (Feed)input.build(new XmlReader(getMethod.getResponseBodyAsStream())); + msg.setBody(feed); + + } else if (status == 404) { + msg.setFaultBody(new NotFoundException()); + } else { + msg.setFaultBody(new ServiceRuntimeException("HTTP status code: " + status)); + } + + } catch (Exception e) { + msg.setFaultBody(new ServiceRuntimeException(e)); + } finally { + getMethod.releaseConnection(); + } + + return msg; + } + } + + /** + * PostMedia operation invoker + */ + public static class PostMediaInvoker extends AtomBindingInvoker { + + public PostMediaInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) { + super(operation, uri, httpClient, authorizationHeader); + } + + @Override + public Message invoke(Message msg) { + // TODO implement + return super.invoke(msg); + } + } + + /** + * PutMedia operation invoker + */ + public static class PutMediaInvoker extends AtomBindingInvoker { + + public PutMediaInvoker(Operation operation, String uri, HttpClient httpClient, String authorizationHeader) { + super(operation, uri, httpClient, authorizationHeader); + } + + @Override + public Message invoke(Message msg) { + // TODO implement + return super.invoke(msg); + } + } + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomBindingProviderFactory.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomBindingProviderFactory.java new file mode 100644 index 0000000000..6e134fd0c2 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomBindingProviderFactory.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.provider; + +import org.apache.tuscany.sca.binding.feed.AtomBinding; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * Implementation of the Atom binding model. + * + * @version $Rev$ $Date$ + */ +public class AtomBindingProviderFactory implements BindingProviderFactory<AtomBinding> { + + private MessageFactory messageFactory; + private Mediator mediator; + private ServletHost servletHost; + + public AtomBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + ServletHostExtensionPoint servletHosts = extensionPoints.getExtensionPoint(ServletHostExtensionPoint.class); + this.servletHost = servletHosts.getServletHosts().get(0); + ModelFactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + this.messageFactory = modelFactories.getFactory(MessageFactory.class); + this.mediator = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class).getUtility(Mediator.class); + } + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component, + RuntimeComponentReference reference, + AtomBinding binding) { + return new AtomReferenceBindingProvider(component, reference, binding); + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component, + RuntimeComponentService service, + AtomBinding binding) { + return new AtomServiceBindingProvider(component, service, binding, servletHost, messageFactory, mediator); + } + + public Class<AtomBinding> getModelType() { + return AtomBinding.class; + } +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomFeedEntryUtil.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomFeedEntryUtil.java new file mode 100644 index 0000000000..7adbfca6e8 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomFeedEntryUtil.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.provider; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; +import org.jdom.output.Format; +import org.jdom.output.XMLOutputter; + +import com.sun.syndication.feed.atom.Content; +import com.sun.syndication.feed.atom.Entry; +import com.sun.syndication.feed.atom.Feed; +import com.sun.syndication.io.FeedException; +import com.sun.syndication.io.WireFeedInput; +import com.sun.syndication.io.WireFeedOutput; + +/** + * Utility methods to read/write Atom entries. + * + * @version $Rev$ $Date$ + */ +class AtomFeedEntryUtil { + + /** + * Read an Atom entry + * + * @param request + * @return + * @throws IOException + * @throws JDOMException + * @throws FeedException + * @throws IllegalArgumentException + */ + static Entry readFeedEntry(String feedType, Reader reader) throws JDOMException, IOException, IllegalArgumentException, + FeedException { + SAXBuilder builder = new SAXBuilder(); + Document document = builder.build(reader); + Element root = document.getRootElement(); + root.detach(); + Feed feed = new Feed(); + feed.setFeedType(feedType); + WireFeedOutput wireFeedOutput = new WireFeedOutput(); + document = wireFeedOutput.outputJDom(feed); + document.getRootElement().addContent(root); + WireFeedInput input = new WireFeedInput(); + feed = (Feed)input.build(document); + Entry feedEntry = (Entry)feed.getEntries().get(0); + if (feedEntry.getContents().size() != 0) { + Content content = (Content)feedEntry.getContents().get(0); + if ("text/xml".equals(content.getType())) { + Element element = root.getChild("content", root.getNamespace()); + if (!element.getChildren().isEmpty()) { + element = (Element)element.getChildren().get(0); + XMLOutputter outputter = new XMLOutputter(); + StringWriter sw = new StringWriter(); + outputter.output(element, sw); + content.setValue(sw.toString()); + } + } + } + return feedEntry; + } + + /** + * Write an Atom entry. + * + * @param entry + * @param response + * @throws FeedException + * @throws IllegalArgumentException + * @throws IOException + * @throws ServletException + */ + static void writeFeedEntry(Entry feedEntry, String feedType, Writer writer) throws IllegalArgumentException, FeedException, + IOException { + Feed feed = new Feed(); + feed.setFeedType(feedType); + List<Entry> feedEntries = new ArrayList<Entry>(); + feedEntries.add(feedEntry); + feed.setEntries(feedEntries); + + WireFeedOutput wireFeedOutput = new WireFeedOutput(); + Document document = wireFeedOutput.outputJDom(feed); + Element root = document.getRootElement(); + Element element = (Element)root.getChildren().get(0); + XMLOutputter outputter = new XMLOutputter(); + outputter.setFormat(Format.getPrettyFormat()); + outputter.output(element, writer); + } + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomReferenceBindingProvider.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomReferenceBindingProvider.java new file mode 100644 index 0000000000..d9bda1fb9a --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomReferenceBindingProvider.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.provider; + +import java.net.URI; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.tuscany.sca.binding.feed.AtomBinding; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; + +/** + * Implementation of the Atom binding provider. + * + * @version $Rev$ $Date$ + */ +class AtomReferenceBindingProvider implements ReferenceBindingProvider { + + private RuntimeComponentReference reference; + private AtomBinding binding; + private String authorizationHeader; + private HttpClient httpClient; + + /** + * Constructs a new AtomReferenceBindingProvider + * @param component + * @param reference + * @param binding + */ + AtomReferenceBindingProvider(RuntimeComponent component, + RuntimeComponentReference reference, + AtomBinding binding) { + this.reference = reference; + this.binding = binding; + + // Prepare authorization header + String authorization = "admin" + ":" + "admin"; + authorizationHeader = "Basic " + new String(Base64.encodeBase64(authorization.getBytes())); + + // Create an HTTP client + httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); + httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000); + } + + public Invoker createInvoker(Operation operation) { + String operationName = operation.getName(); + if (operationName.equals("get")) { + return new AtomBindingInvoker.GetInvoker(operation, binding.getURI(), httpClient, authorizationHeader); + } else if (operationName.equals("post")) { + return new AtomBindingInvoker.PostInvoker(operation, binding.getURI(), httpClient, authorizationHeader); + } else if (operationName.equals("put")) { + return new AtomBindingInvoker.PutInvoker(operation, binding.getURI(), httpClient, authorizationHeader); + } else if (operationName.equals("delete")) { + return new AtomBindingInvoker.DeleteInvoker(operation, binding.getURI(), httpClient, + authorizationHeader); + } else if (operationName.equals("getFeed") || operationName.equals("getAll")) { + return new AtomBindingInvoker.GetAllInvoker(operation, binding.getURI(), httpClient, + authorizationHeader); + } else if (operationName.equals("postMedia")) { + return new AtomBindingInvoker.PostMediaInvoker(operation, binding.getURI(), httpClient, + authorizationHeader); + } else if (operationName.equals("putMedia")) { + return new AtomBindingInvoker.PutMediaInvoker(operation, binding.getURI(), httpClient, + authorizationHeader); + } + + return new AtomBindingInvoker(operation, binding.getURI(), httpClient, authorizationHeader); + } + + public InterfaceContract getBindingInterfaceContract() { + return reference.getInterfaceContract(); + } + + public void start() { + + // Configure the HTTP client credentials + Credentials credentials = new UsernamePasswordCredentials("admin", "admin"); + httpClient.getParams().setAuthenticationPreemptive(true); + URI uri = URI.create(binding.getURI()); + httpClient.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), credentials); + } + + public void stop() { + } + + public boolean supportsOneWayInvocation() { + return false; + } + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomServiceBindingProvider.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomServiceBindingProvider.java new file mode 100644 index 0000000000..23c36f51e3 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/AtomServiceBindingProvider.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.provider; + +import org.apache.tuscany.sca.binding.feed.AtomBinding; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +/** + * Implementation of the Atom binding provider. + * + * @version $Rev$ $Date$ + */ +class AtomServiceBindingProvider implements ServiceBindingProvider { + + private RuntimeComponentService service; + private AtomBinding binding; + private ServletHost servletHost; + private MessageFactory messageFactory; + private String servletMapping; + private Mediator mediator; + + AtomServiceBindingProvider(RuntimeComponent component, + RuntimeComponentService service, + AtomBinding binding, + ServletHost servletHost, + MessageFactory messageFactory, + Mediator mediator) { + this.service = service; + this.binding = binding; + this.servletHost = servletHost; + this.messageFactory = messageFactory; + this.mediator = mediator; + } + + public InterfaceContract getBindingInterfaceContract() { + return service.getInterfaceContract(); + } + + public boolean supportsOneWayInvocation() { + return false; + } + + public void start() { + RuntimeComponentService componentService = (RuntimeComponentService)service; + RuntimeWire wire = componentService.getRuntimeWire(binding); + + FeedBindingListenerServlet servlet = + new FeedBindingListenerServlet(wire, messageFactory, mediator, "atom_1.0"); + + servletMapping = binding.getURI(); + if (!servletMapping.endsWith("/")) { + servletMapping += "/"; + } + if (!servletMapping.endsWith("*")) { + servletMapping += "*"; + } + servletHost.addServletMapping(servletMapping, servlet); + } + + public void stop() { + servletHost.removeServletMapping(servletMapping); + } +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/FeedBindingListenerServlet.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/FeedBindingListenerServlet.java new file mode 100644 index 0000000000..89366456f5 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/FeedBindingListenerServlet.java @@ -0,0 +1,788 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.binding.feed.provider; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.StringTokenizer; +import java.util.logging.Logger; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.xml.namespace.QName; + +import org.apache.commons.codec.binary.Base64; +import org.apache.tuscany.sca.data.collection.Item; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.util.XMLType; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.Namespace; +import org.jdom.output.Format; +import org.jdom.output.XMLOutputter; + +import com.sun.syndication.feed.atom.Content; +import com.sun.syndication.feed.atom.Entry; +import com.sun.syndication.feed.atom.Feed; +import com.sun.syndication.feed.atom.Link; +import com.sun.syndication.feed.synd.SyndFeed; +import com.sun.syndication.feed.synd.SyndFeedImpl; +import com.sun.syndication.io.FeedException; +import com.sun.syndication.io.SyndFeedOutput; +import com.sun.syndication.io.WireFeedOutput; + +/** + * A resource collection binding listener, implemented as a Servlet and + * registered in a Servlet host provided by the SCA hosting runtime. + * + * @version $Rev$ $Date$ + */ +class FeedBindingListenerServlet extends HttpServlet { + private static final Logger logger = Logger.getLogger(FeedBindingListenerServlet.class.getName()); + private static final long serialVersionUID = 1L; + + private static final Namespace APP_NS = Namespace.getNamespace("app", "http://purl.org/atom/app#"); + private static final Namespace ATOM_NS = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom"); + + private RuntimeWire wire; + private Invoker getFeedInvoker; + private Invoker getAllInvoker; + private Invoker queryInvoker; + private Invoker getInvoker; + private Invoker postInvoker; + private Invoker postMediaInvoker; + private Invoker putInvoker; + private Invoker putMediaInvoker; + private Invoker deleteInvoker; + private MessageFactory messageFactory; + private String feedType; + private Mediator mediator; + private DataType<?> itemClassType; + private DataType<?> itemXMLType; + private boolean supportsFeedEntries; + + /** + * Constructs a new binding listener. + * + * @param wire + * @param messageFactory + * @param feedType + */ + FeedBindingListenerServlet(RuntimeWire wire, MessageFactory messageFactory, Mediator mediator, String feedType) { + this.wire = wire; + this.messageFactory = messageFactory; + this.mediator = mediator; + this.feedType = feedType; + + // Get the invokers for the supported operations + Operation getOperation = null; + for (InvocationChain invocationChain : this.wire.getInvocationChains()) { + invocationChain.setAllowsPassByReference(true); + Operation operation = invocationChain.getTargetOperation(); + String operationName = operation.getName(); + if (operationName.equals("getFeed")) { + getFeedInvoker = invocationChain.getHeadInvoker(); + } else if (operationName.equals("getAll")) { + getAllInvoker = invocationChain.getHeadInvoker(); + } else if (operationName.equals("query")) { + queryInvoker = invocationChain.getHeadInvoker(); + } else if (operationName.equals("get")) { + getInvoker = invocationChain.getHeadInvoker(); + getOperation = operation; + } else if (operationName.equals("put")) { + putInvoker = invocationChain.getHeadInvoker(); + } else if (operationName.equals("putMedia")) { + putMediaInvoker = invocationChain.getHeadInvoker(); + } else if (operationName.equals("post")) { + postInvoker = invocationChain.getHeadInvoker(); + } else if (operationName.equals("postMedia")) { + postMediaInvoker = invocationChain.getHeadInvoker(); + } else if (operationName.equals("delete")) { + deleteInvoker = invocationChain.getHeadInvoker(); + } + } + + // Determine the collection item type + itemXMLType = new DataTypeImpl<Class<?>>(String.class.getName(), String.class, String.class); + Class<?> itemClass = getOperation.getOutputType().getPhysical(); + if (itemClass == Entry.class) { + supportsFeedEntries = true; + } + DataType<XMLType> outputType = getOperation.getOutputType(); + QName qname = outputType.getLogical().getElementName(); + qname = new QName(qname.getNamespaceURI(), itemClass.getSimpleName()); + itemClassType = new DataTypeImpl<XMLType>("java:complexType", itemClass, new XMLType(qname, null)); + + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + // No authentication required for a get request + + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + + // The feedType parameter is used to override what type of feed is going + // to be produced + String requestFeedType = request.getParameter("feedType"); + if (requestFeedType == null) + requestFeedType = feedType; + + logger.info(">>> FeedEndPointServlet (" + requestFeedType + ") " + request.getRequestURI()); + + // Handle an Atom request + if (requestFeedType.startsWith("atom_")) { + + if (path != null && path.equals("/atomsvc")) { + + // Return the Atom service document + response.setContentType("application/atomsvc+xml; charset=utf-8"); + Document document = new Document(); + Element service = new Element("service", APP_NS); + document.setRootElement(service); + + Element workspace = new Element("workspace", APP_NS); + Element title = new Element("title", ATOM_NS); + title.setText("resource"); + workspace.addContent(title); + service.addContent(workspace); + + Element collection = new Element("collection", APP_NS); + String href = request.getRequestURL().toString(); + href = href.substring(0, href.length() - "/atomsvc".length()); + collection.setAttribute("href", href); + Element collectionTitle = new Element("title", ATOM_NS); + collectionTitle.setText("entries"); + collection.addContent(collectionTitle); + workspace.addContent(collection); + + XMLOutputter outputter = new XMLOutputter(); + Format format = Format.getPrettyFormat(); + format.setEncoding("UTF-8"); + outputter.setFormat(format); + outputter.output(document, getWriter(response)); + + } else if (path == null || path.length() == 0 || path.equals("/")) { + + // Return a feed containing the entries in the collection + Feed feed = null; + if (supportsFeedEntries) { + + // The service implementation supports feed entries, invoke its getFeed operation + Message requestMessage = messageFactory.createMessage(); + Message responseMessage = getFeedInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + feed = (Feed)responseMessage.getBody(); + + } else { + + // The service implementation does not support feed entries, + // invoke its getAll operation to get the data item collection, then create + // feed entries from the items + Message requestMessage = messageFactory.createMessage(); + Message responseMessage; + if (request.getQueryString() != null) { + requestMessage.setBody(new Object[] {request.getQueryString()}); + responseMessage = queryInvoker.invoke(requestMessage); + } else { + responseMessage = getAllInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + org.apache.tuscany.sca.data.collection.Entry<Object, Object>[] collection = + (org.apache.tuscany.sca.data.collection.Entry<Object, Object>[])responseMessage.getBody(); + if (collection != null) { + // Create the feed + feed = new Feed(); + feed.setTitle("Feed"); + for (org.apache.tuscany.sca.data.collection.Entry<Object, Object> entry: collection) { + Entry feedEntry = createFeedEntry(entry); + feed.getEntries().add(feedEntry); + } + } + } + if (feed != null) { + + // Write the Atom feed + response.setContentType("application/atom+xml; charset=utf-8"); + feed.setFeedType(requestFeedType); + WireFeedOutput feedOutput = new WireFeedOutput(); + try { + feedOutput.output(feed, getWriter(response)); + } catch (FeedException e) { + throw new ServletException(e); + } + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + + } else if (path.startsWith("/")) { + + // Return a specific entry in the collection + Entry feedEntry; + + // Invoke the get operation on the service implementation + Message requestMessage = messageFactory.createMessage(); + String id = path.substring(1); + requestMessage.setBody(new Object[] {id}); + Message responseMessage = getInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + if (supportsFeedEntries) { + + // The service implementation returns a feed entry + feedEntry = responseMessage.getBody(); + + } else { + + // The service implementation only returns a data item, create an entry + // from it + feedEntry = createFeedEntry(new org.apache.tuscany.sca.data.collection.Entry<Object, Object>(id, responseMessage.getBody())); + } + + // Write the Atom entry + if (feedEntry != null) { + response.setContentType("application/atom+xml; charset=utf-8"); + try { + AtomFeedEntryUtil.writeFeedEntry(feedEntry, feedType, getWriter(response)); + } catch (FeedException e) { + throw new ServletException(e); + } + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + + } else { + + // Path doesn't match any known pattern + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } else { + + // Handle an RSS request + if (path == null || path.length() == 0 || path.equals("/")) { + + // Return an RSS feed containing the entries in the collection + Feed feed = null; + if (supportsFeedEntries) { + + // The service implementation supports feed entries, invoke its getFeed operation + Message requestMessage = messageFactory.createMessage(); + Message responseMessage = getFeedInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + feed = (Feed)responseMessage.getBody(); + + } else { + + // The service implementation does not support feed entries, invoke its + // getAll operation to get the data item collection. then create feed entries + // from the data items + Message requestMessage = messageFactory.createMessage(); + Message responseMessage; + if (request.getQueryString() != null) { + requestMessage.setBody(new Object[] {request.getQueryString()}); + responseMessage = queryInvoker.invoke(requestMessage); + } else { + responseMessage = getAllInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + org.apache.tuscany.sca.data.collection.Entry<Object, Object>[] collection = + (org.apache.tuscany.sca.data.collection.Entry<Object, Object>[])responseMessage.getBody(); + if (collection != null) { + // Create the feed + feed = new Feed(); + feed.setTitle("Feed"); + for (org.apache.tuscany.sca.data.collection.Entry<Object, Object> entry: collection) { + Entry feedEntry = createFeedEntry(entry); + feed.getEntries().add(feedEntry); + } + } + } + + // Convert to an RSS feed + if (feed != null) { + response.setContentType("application/rss+xml; charset=utf-8"); + feed.setFeedType("atom_1.0"); + SyndFeed syndFeed = new SyndFeedImpl(feed); + syndFeed.setFeedType(requestFeedType); + syndFeed.setLink(path); + SyndFeedOutput syndOutput = new SyndFeedOutput(); + try { + syndOutput.output(syndFeed, getWriter(response)); + } catch (FeedException e) { + throw new ServletException(e); + } + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } + } + + /** + * Create an Atom entry from a data collection entry. + * @param entry + * @return + */ + private Entry createFeedEntry(org.apache.tuscany.sca.data.collection.Entry<Object, Object> entry) { + Object key = entry.getKey(); + Object data = entry.getData(); + if (data instanceof Item) { + Item item = (Item)data; + + Entry feedEntry = new Entry(); + feedEntry.setId(key.toString()); + feedEntry.setTitle(item.getTitle()); + + String value = item.getContents(); + if (value != null) { + Content content = new Content(); + content.setType("text/xml"); + content.setValue(value); + List<Content> contents = new ArrayList<Content>(); + contents.add(content); + feedEntry.setContents(contents); + } + + String href = item.getLink(); + if (href == null) { + href = key.toString(); + } + Link link = new Link(); + link.setRel("edit"); + link.setHref(href); + feedEntry.getOtherLinks().add(link); + link = new Link(); + link.setRel("alternate"); + link.setHref(href); + feedEntry.getAlternateLinks().add(link); + + Date date = item.getDate(); + if (date == null) { + date = new Date(); + } + feedEntry.setCreated(date); + return feedEntry; + + } else if (data != null) { + Entry feedEntry = new Entry(); + feedEntry.setId(key.toString()); + feedEntry.setTitle("item"); + + // Convert the item to XML + String value = mediator.mediate(data, itemClassType, itemXMLType, null).toString(); + + Content content = new Content(); + content.setType("text/xml"); + content.setValue(value); + List<Content> contents = new ArrayList<Content>(); + contents.add(content); + feedEntry.setContents(contents); + + Link link = new Link(); + link.setRel("edit"); + link.setHref(key.toString()); + feedEntry.getOtherLinks().add(link); + link = new Link(); + link.setRel("alternate"); + link.setHref(key.toString()); + feedEntry.getAlternateLinks().add(link); + + feedEntry.setCreated(new Date()); + return feedEntry; + } else { + return null; + } + } + + /** + * Create a data collection entry from an Atom entry. + * @param feedEntry + * @return + */ + private org.apache.tuscany.sca.data.collection.Entry<Object, Object> createEntry(Entry feedEntry) { + if (feedEntry != null) { + if (itemClassType.getPhysical() == Item.class) { + String key = feedEntry.getId(); + + Item item = new Item(); + item.setTitle(feedEntry.getTitle()); + + List<?> contents = feedEntry.getContents(); + if (!contents.isEmpty()) { + Content content = (Content)contents.get(0); + String value = content.getValue(); + item.setContents(value); + } + + for (Object l : feedEntry.getOtherLinks()) { + Link link = (Link)l; + if (link.getRel() == null || "edit".equals(link.getRel())) { + String href = link.getHref(); + if (href.startsWith("null/")) { + href = href.substring(5); + } + item.setLink(href); + break; + } + } + + item.setDate(feedEntry.getCreated()); + + return new org.apache.tuscany.sca.data.collection.Entry<Object, Object>(key, item); + + } else { + String key = feedEntry.getId(); + + // Create the item from XML + List<?> contents = feedEntry.getContents(); + if (contents.isEmpty()) { + return null; + } + Content content = (Content)contents.get(0); + String value = content.getValue(); + Object data = mediator.mediate(value, itemXMLType, itemClassType, null); + + return new org.apache.tuscany.sca.data.collection.Entry<Object, Object>(key, data); + } + } else { + return null; + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, + IOException { + + // Authenticate the user + String user = processAuthorizationHeader(request); + if (user == null) { + unauthorized(response); + return; + } + + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + + if (path == null || path.length() == 0 || path.equals("/")) { + Entry createdFeedEntry = null; + + // Create a new Atom entry + String contentType = request.getContentType(); + if (contentType != null && contentType.startsWith("application/atom+xml")) { + + // Read the entry from the request + Entry feedEntry; + try { + feedEntry = AtomFeedEntryUtil.readFeedEntry(feedType, request.getReader()); + } catch (JDOMException e) { + throw new ServletException(e); + } catch (FeedException e) { + throw new ServletException(e); + } + + // Let the component implementation create it + if (supportsFeedEntries) { + + // The service implementation supports feed entries, pass the entry to it + Message requestMessage = messageFactory.createMessage(); + requestMessage.setBody(new Object[] {feedEntry}); + Message responseMessage = postInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + createdFeedEntry = responseMessage.getBody(); + } else { + + // The service implementation does not support feed entries, pass the data item to it + Message requestMessage = messageFactory.createMessage(); + org.apache.tuscany.sca.data.collection.Entry<Object, Object> entry = createEntry(feedEntry); + requestMessage.setBody(new Object[] {entry.getKey(), entry.getData()}); + Message responseMessage = postInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + entry.setKey(responseMessage.getBody()); + createdFeedEntry = createFeedEntry(entry); + } + + } else if (contentType != null) { + + // Create a new media entry + + // Get incoming headers + String title = request.getHeader("Title"); + String slug = request.getHeader("Slug"); + + // Let the component implementation create the media entry + Message requestMessage = messageFactory.createMessage(); + requestMessage.setBody(new Object[] {title, slug, contentType, request.getInputStream()}); + Message responseMessage = postMediaInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + createdFeedEntry = responseMessage.getBody(); + } else { + response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); + } + + // A new entry was created successfully + if (createdFeedEntry != null) { + + // Set location of the created entry in the Location header + for (Object l : createdFeedEntry.getOtherLinks()) { + Link link = (Link)l; + if (link.getRel() == null || "edit".equals(link.getRel())) { + response.addHeader("Location", link.getHref()); + break; + } + } + + // Write the created Atom entry + response.setStatus(HttpServletResponse.SC_CREATED); + response.setContentType("application/atom+xml; charset=utf-8"); + try { + AtomFeedEntryUtil.writeFeedEntry(createdFeedEntry, feedType, getWriter(response)); + } catch (FeedException e) { + throw new ServletException(e); + } + + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } + + private Writer getWriter(HttpServletResponse response) throws UnsupportedEncodingException, IOException { + Writer writer = new OutputStreamWriter(response.getOutputStream(), "UTF-8"); + return writer; + } + + @Override + protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + // Authenticate the user + String user = processAuthorizationHeader(request); + if (user == null) { + unauthorized(response); + return; + } + + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + + if (path != null && path.startsWith("/")) { + String id = path.substring(1); + + // Update an Atom entry + String contentType = request.getContentType(); + if (contentType != null && contentType.startsWith("application/atom+xml")) { + + // Read the entry from the request + Entry feedEntry; + try { + feedEntry = AtomFeedEntryUtil.readFeedEntry(feedType, request.getReader()); + } catch (JDOMException e) { + throw new ServletException(e); + } catch (FeedException e) { + throw new ServletException(e); + } + + // Let the component implementation create it + if (supportsFeedEntries) { + + // The service implementation supports feed entries, pass the entry to it + Message requestMessage = messageFactory.createMessage(); + requestMessage.setBody(new Object[] {id, feedEntry}); + Message responseMessage = putInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + if (body.getClass().getName().endsWith(".NotFoundException")) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + throw new ServletException((Throwable)responseMessage.getBody()); + } + } + } else { + + // The service implementation does not support feed entries, pass the data item to it + Message requestMessage = messageFactory.createMessage(); + org.apache.tuscany.sca.data.collection.Entry<Object, Object> entry = createEntry(feedEntry); + requestMessage.setBody(new Object[] {entry.getKey(), entry.getData()}); + Message responseMessage = putInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + if (body.getClass().getName().endsWith(".NotFoundException")) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + throw new ServletException((Throwable)responseMessage.getBody()); + } + } + } + + } else if (contentType != null) { + + // Updated a media entry + + // Let the component implementation create the media entry + Message requestMessage = messageFactory.createMessage(); + requestMessage.setBody(new Object[] {id, contentType, request.getInputStream()}); + Message responseMessage = putMediaInvoker.invoke(requestMessage); + Object body = responseMessage.getBody(); + if (responseMessage.isFault()) { + if (body.getClass().getName().endsWith(".NotFoundException")) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + throw new ServletException((Throwable)responseMessage.getBody()); + } + } + } else { + response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); + } + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } + + @Override + protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, + IOException { + + // Authenticate the user + String user = processAuthorizationHeader(request); + if (user == null) { + unauthorized(response); + return; + } + + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + + String id; + if (path != null && path.startsWith("/")) { + id = path.substring(1); + } else { + id = ""; + } + + // Delete a specific entry from the collection + Message requestMessage = messageFactory.createMessage(); + requestMessage.setBody(new Object[] {id}); + Message responseMessage = deleteInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + if (body.getClass().getName().endsWith(".NotFoundException")) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + throw new ServletException((Throwable)responseMessage.getBody()); + } + } + } + + /** + * Process the authorization header + * + * @param request + * @return + * @throws ServletException + */ + private String processAuthorizationHeader(HttpServletRequest request) throws ServletException { + try { + String authorization = request.getHeader("Authorization"); + if (authorization != null) { + StringTokenizer tokens = new StringTokenizer(authorization); + if (tokens.hasMoreTokens()) { + String basic = tokens.nextToken(); + if (basic.equalsIgnoreCase("Basic")) { + String credentials = tokens.nextToken(); + String userAndPassword = new String(Base64.decodeBase64(credentials.getBytes())); + int colon = userAndPassword.indexOf(":"); + if (colon != -1) { + String user = userAndPassword.substring(0, colon); + String password = userAndPassword.substring(colon + 1); + + // Authenticate the User. + if (authenticate(user, password)) { + return user; + } + } + } + } + } + } catch (Exception e) { + throw new ServletException(e); + } + return null; + } + + /** + * Authenticate a user. + * + * @param user + * @param password + * @return + */ + private boolean authenticate(String user, String password) { + + // TODO Handle this using SCA security policies + //FIXME Why are we using endsWith instead of equals here?? + return ("admin".endsWith(user) && "admin".equals(password)); + } + + /** + * Reject an unauthorized request. + * + * @param response + */ + private void unauthorized(HttpServletResponse response) throws IOException { + response.setHeader("WWW-Authenticate", "BASIC realm=\"Tuscany\""); + response.sendError(HttpServletResponse.SC_UNAUTHORIZED); + } +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSBindingInvoker.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSBindingInvoker.java new file mode 100644 index 0000000000..c5bd3ec009 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSBindingInvoker.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.binding.feed.provider; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.DataExchangeSemantics; + +import com.sun.syndication.feed.atom.Feed; +import com.sun.syndication.feed.synd.SyndFeed; +import com.sun.syndication.io.FeedException; +import com.sun.syndication.io.SyndFeedInput; +import com.sun.syndication.io.WireFeedInput; +import com.sun.syndication.io.XmlReader; + +/** + * Invoker for the RSS binding. + * + * @version $Rev$ $Date$ + */ +class RSSBindingInvoker implements Invoker, DataExchangeSemantics { + private static final Logger logger = Logger.getLogger(RSSBindingInvoker.class.getName()); + + private String feedType; + private String uri; + + RSSBindingInvoker(String uri, String feedType) { + this.uri = uri; + this.feedType = feedType; + } + + public Message invoke(Message msg) { + try { + logger.info(">>> RSSBindingInvoker (" + feedType + ") " + uri); + + // Read the configured feed URI into a Feed object + Feed feed; + if (feedType.startsWith("atom_")) { + + // Read an Atom feed + WireFeedInput input = new WireFeedInput(); + feed = (Feed)input.build(new XmlReader(new URL(uri))); + } else { + + // Read an RSS feed and convert it to an Atom feed + SyndFeedInput input = new SyndFeedInput(); + SyndFeed syndFeed = input.build(new XmlReader(new URL(uri))); + feed = (Feed)syndFeed.createWireFeed("atom_1.0"); + } + + //FIXME Support conversion to data-api entries + + msg.setBody(feed); + + } catch (MalformedURLException e) { + msg.setFaultBody(e); + } catch (IllegalArgumentException e) { + msg.setFaultBody(e); + } catch (FeedException e) { + msg.setFaultBody(e); + } catch (IOException e) { + msg.setFaultBody(e); + } + return msg; + } + + public boolean allowsPassByReference() { + return true; + } +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSBindingProviderFactory.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSBindingProviderFactory.java new file mode 100644 index 0000000000..649af78c5b --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSBindingProviderFactory.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.provider; + +import org.apache.tuscany.sca.binding.feed.RSSBinding; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * Implementation of the RSS binding provider factory. + * + * @version $Rev$ $Date$ + */ +public class RSSBindingProviderFactory implements BindingProviderFactory<RSSBinding> { + + private MessageFactory messageFactory; + private Mediator mediator; + private ServletHost servletHost; + + public RSSBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + ServletHostExtensionPoint servletHosts = extensionPoints.getExtensionPoint(ServletHostExtensionPoint.class); + this.servletHost = servletHosts.getServletHosts().get(0); + ModelFactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + this.messageFactory = modelFactories.getFactory(MessageFactory.class); + this.mediator = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class).getUtility(Mediator.class); + } + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component, + RuntimeComponentReference reference, + RSSBinding binding) { + return new RSSReferenceBindingProvider(component, reference, binding); + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component, + RuntimeComponentService service, + RSSBinding binding) { + return new RSSServiceBindingProvider(component, service, binding, servletHost, messageFactory, mediator); + } + + public Class<RSSBinding> getModelType() { + return RSSBinding.class; + } +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSReferenceBindingProvider.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSReferenceBindingProvider.java new file mode 100644 index 0000000000..278ed9cb7e --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSReferenceBindingProvider.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.provider; + +import org.apache.tuscany.sca.binding.feed.RSSBinding; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; + +/** + * Implementation of the RSS binding provider. + * + * @version $Rev$ $Date$ + */ +class RSSReferenceBindingProvider implements ReferenceBindingProvider { + + private RuntimeComponentReference reference; + private RSSBinding binding; + + RSSReferenceBindingProvider(RuntimeComponent component, + RuntimeComponentReference reference, + RSSBinding binding) { + this.reference = reference; + this.binding = binding; + } + + public Invoker createInvoker(Operation operation) { + return new RSSBindingInvoker(binding.getURI(), "rss_2.0"); + } + + public boolean supportsOneWayInvocation() { + return false; + } + + public InterfaceContract getBindingInterfaceContract() { + return reference.getInterfaceContract(); + } + + public void start() { + } + + public void stop() { + } + +} diff --git a/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSServiceBindingProvider.java b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSServiceBindingProvider.java new file mode 100644 index 0000000000..779b28cef6 --- /dev/null +++ b/branches/sca-java-1.5.1/modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/RSSServiceBindingProvider.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.feed.provider; + +import org.apache.tuscany.sca.binding.feed.RSSBinding; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +/** + * Implementation of the RSS binding provider. + * + * @version $Rev$ $Date$ + */ +class RSSServiceBindingProvider implements ServiceBindingProvider { + + private RuntimeComponentService service; + private RSSBinding binding; + private ServletHost servletHost; + private MessageFactory messageFactory; + private String servletMapping; + private Mediator mediator; + + RSSServiceBindingProvider(RuntimeComponent component, + RuntimeComponentService service, + RSSBinding binding, + ServletHost servletHost, + MessageFactory messageFactory, + Mediator mediator) { + this.service = service; + this.binding = binding; + this.servletHost = servletHost; + this.messageFactory = messageFactory; + this.mediator = mediator; + } + + public InterfaceContract getBindingInterfaceContract() { + return service.getInterfaceContract(); + } + + public boolean supportsOneWayInvocation() { + return false; + } + + public void start() { + RuntimeComponentService componentService = (RuntimeComponentService)service; + RuntimeWire wire = componentService.getRuntimeWire(binding); + + FeedBindingListenerServlet servlet = + new FeedBindingListenerServlet(wire, messageFactory, mediator, "rss_2.0"); + + servletMapping = binding.getURI(); + servletHost.addServletMapping(servletMapping, servlet); + + // Save the actual binding URI in the binding + binding.setURI(servletHost.getURLMapping(binding.getURI()).toString()); + } + + public void stop() { + servletHost.removeServletMapping(servletMapping); + } + +} |