View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.ui.commons;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Optional;
25  import javax.ws.rs.core.MediaType;
26  import org.apache.syncope.client.ui.commons.rest.ResponseHolder;
27  import org.apache.wicket.util.lang.Bytes;
28  import org.apache.wicket.util.resource.AbstractResourceStream;
29  import org.apache.wicket.util.resource.IFixedLocationResourceStream;
30  import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
31  
32  public class HttpResourceStream extends AbstractResourceStream implements IFixedLocationResourceStream {
33  
34      private static final long serialVersionUID = 5811207817876330189L;
35  
36      private final ResponseHolder responseHolder;
37  
38      public HttpResourceStream(final ResponseHolder responseHolder) {
39          super();
40          this.responseHolder = responseHolder;
41      }
42  
43      @Override
44      public InputStream getInputStream()
45              throws ResourceStreamNotFoundException {
46  
47          return responseHolder.getInputStream() == null
48                  ? new ByteArrayInputStream(new byte[0])
49                  : responseHolder.getInputStream();
50      }
51  
52      @Override
53      public Bytes length() {
54          return responseHolder.getInputStream() == null
55                  ? Bytes.bytes(0)
56                  : null;
57      }
58  
59      @Override
60      public void close() throws IOException {
61          // No need for explict closing
62      }
63  
64      @Override
65      public String locationAsString() {
66          return responseHolder.getLocation();
67      }
68  
69      @Override
70      public String getContentType() {
71          return Optional.ofNullable(responseHolder.getContentType()).orElse(MediaType.APPLICATION_OCTET_STREAM);
72      }
73  
74      public String getFilename() {
75          return responseHolder.getFilename();
76      }
77  }