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.rest;
20  
21  import java.io.InputStream;
22  import java.io.Serializable;
23  import javax.ws.rs.core.HttpHeaders;
24  import javax.ws.rs.core.Response;
25  import org.apache.commons.lang3.StringUtils;
26  
27  public class ResponseHolder implements Serializable {
28  
29      private static final long serialVersionUID = 2627155013246805827L;
30  
31      private transient InputStream inputStream;
32  
33      private String contentType;
34  
35      private String location;
36  
37      private String filename;
38  
39      public ResponseHolder(final Response response) {
40          Object entity = response.getEntity();
41          if (response.getStatusInfo().getStatusCode() == Response.Status.OK.getStatusCode()
42                  && (entity instanceof InputStream)) {
43  
44              this.inputStream = (InputStream) entity;
45              this.contentType = response.getHeaderString(HttpHeaders.CONTENT_TYPE);
46              this.location = response.getLocation() == null ? null : response.getLocation().toASCIIString();
47              String contentDisposition = response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION);
48              if (StringUtils.isNotBlank(contentDisposition)) {
49                  String[] splitted = contentDisposition.split("=");
50                  if (splitted != null && splitted.length > 1) {
51                      this.filename = splitted[1].trim();
52                  }
53              }
54          }
55      }
56  
57      public InputStream getInputStream() {
58          return inputStream;
59      }
60  
61      public String getContentType() {
62          return contentType;
63      }
64  
65      public String getLocation() {
66          return location;
67      }
68  
69      public String getFilename() {
70          return filename;
71      }
72  }