View Javadoc

1   /*
2    * Copyright 2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.nordija.tapestry.bayeux.stream;
17  
18  import org.apache.hivemind.Location;
19  import org.apache.hivemind.Resource;
20  import org.apache.tapestry.IAsset;
21  import org.apache.tapestry.engine.IEngineService;
22  import org.apache.tapestry.engine.ILink;
23  import org.apache.tapestry.link.DirectLink;
24  
25  import java.io.InputStream;
26  
27  /**
28   * Asset capable of streaming data by invoking the stream service that will delegate the responsibility to the
29   * given streamResourceSourceExtentionName.
30   * @author Jacob von Eyben - Nordija A/S
31   */
32  public class StreamAsset implements IAsset {
33  
34      private IEngineService streamService;
35      private String streamResourceSourceExtentionName;
36      private Object[] parameters;
37      private IStream stream;
38  
39      public StreamAsset(IEngineService streamService, String streamResourceSourceExtentionName, Object[] parameters, IStream stream) {
40          this.streamService = streamService;
41          this.streamResourceSourceExtentionName = streamResourceSourceExtentionName;
42          this.parameters = parameters;
43          this.stream = stream;
44      }
45  
46      public String buildURL() {
47          return getLink().getURL();
48      }
49  
50      public InputStream getResourceAsStream() {
51          throw new UnsupportedOperationException("getResourceAsStream is not supported by the StreamAsset. Only use buildURL on a StreamAsset");
52      }
53  
54      public Resource getResourceLocation() {
55          throw new UnsupportedOperationException("getResourceLocation is not supported by the StreamAsset. Only use buildURL on a StreamAsset");
56      }
57  
58      public Location getLocation() {
59          throw new UnsupportedOperationException("getLocation is not supported by the StreamAsset. Only use buildURL on a StreamAsset");
60      }
61  
62      protected ILink getLink() {
63          Object[] serviceParameters = DirectLink.constructServiceParameters(parameters);
64          StreamServiceParameter ssp = new StreamServiceParameter(stream, streamResourceSourceExtentionName, serviceParameters);
65          return streamService.getLink(stream.isStateful(), ssp);
66      }
67  
68  }