summaryrefslogtreecommitdiffstats
blob: 97c6fce568b9edfc462c2678e23b09b91a6f7e96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.server.admin.conversion;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.eclipse.osee.framework.resource.management.IResource;

/**
 * @author Andrew M. Finkbeiner
 */
public class DbResource implements IResource {

   //   private String name = "unknown";
   private final InputStream inputStream;
   private boolean isCompressed = false;
   private URI uri;
   private final String hrid;
   private final String artName;
   private final String fileTypeExtension;

   public DbResource(InputStream inputStream, String artName, String hrid, String fileTypeExtension) {
      this.hrid = hrid;
      this.artName = artName;
      this.fileTypeExtension = fileTypeExtension;
      this.inputStream = inputStream;
      try {
         uri = new URI("db://");
      } catch (URISyntaxException ex1) {
      }
      if (inputStream.markSupported()) {
         inputStream.mark(1000);
         ZipInputStream in = new ZipInputStream(inputStream);
         // find out if it was compressed
         try {
            ZipEntry entry = in.getNextEntry();
            if (entry == null) {
               isCompressed = false;
            } else {
               //               name = entry.getName();
               isCompressed = true;
            }
         } catch (IOException ex) {
            isCompressed = false;
         }
         try {
            inputStream.reset();
         } catch (IOException ex) {
         }
      }
   }

   @Override
   public InputStream getContent() {
      return inputStream;
   }

   @Override
   public URI getLocation() {
      return uri;
   }

   @Override
   public String getName() {
      return generateFileName(artName, hrid, fileTypeExtension);
   }

   @Override
   public boolean isCompressed() {
      return isCompressed;
   }

   private String generateFileName(String artName, String hrid, String fileTypeExtension) {
      StringBuilder builder = new StringBuilder();
      try {
         builder.append(URLEncoder.encode(artName, "UTF-8"));
         builder.append(".");
      } catch (Exception ex) {
         // Do Nothing - this is not important
      }
      builder.append(hrid);

      if (fileTypeExtension != null && fileTypeExtension.length() > 0) {
         builder.append(".");
         builder.append(fileTypeExtension);
      }
      return builder.toString();
   }
}