Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0cc031788b4fd3ea044a0ecb25215fc4aabc5a2c (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * 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.orcs.db.internal.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.IResourceLocator;
import org.eclipse.osee.framework.resource.management.IResourceLocatorManager;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.orcs.db.internal.util.ByteStreamResource;

/**
 * @author Roberto E. Escobar
 */
public class ResourceHandler implements DataHandler {
   private static final PropertyStore DEFAULT_OPTIONS = new PropertyStore();

   private final IResourceManager resourceManager;
   private final IResourceLocatorManager resourceLocator;

   public ResourceHandler(IResourceManager resourceManager, IResourceLocatorManager resourceLocator) {
      super();
      this.resourceManager = resourceManager;
      this.resourceLocator = resourceLocator;
   }

   @Override
   public byte[] acquire(DataResource dataResource) throws OseeCoreException {
      String path = dataResource.getLocator();
      Conditions.checkNotNull(path, "resource path");

      IResourceLocator locator = resourceLocator.getResourceLocator(path);
      Conditions.checkNotNull(locator, "resource locator", "Unable to locate resource: [%s]",
         dataResource.getStorageName());

      IResource resource = resourceManager.acquire(locator, DEFAULT_OPTIONS);
      resource.getName();

      InputStream inputStream = resource.getContent();
      try {
         String mimeType = URLConnection.guessContentTypeFromStream(inputStream);
         if (mimeType == null) {
            mimeType = URLConnection.guessContentTypeFromName(resource.getLocation().toASCIIString());
            if (mimeType == null) {
               mimeType = "application/*";
            }
         }
         dataResource.setContentType(mimeType);
         dataResource.setEncoding("ISO-8859-1");
         return Lib.inputStreamToBytes(inputStream);
      } catch (IOException ex) {
         throw new OseeCoreException(ex, "Error acquiring resource - name[%s] locator[%s]",
            dataResource.getStorageName(), dataResource.getLocator());
      } finally {
         Lib.close(inputStream);
      }
   }

   @Override
   public void save(int storageId, DataResource dataResource, byte[] rawContent) throws OseeCoreException {
      StringBuilder storageName = new StringBuilder();
      storageName.append(dataResource.getStorageName());
      String extension = dataResource.getExtension();
      if (Strings.isValid(extension)) {
         storageName.append(".");
         storageName.append(extension);
      }

      String seed = Integer.toString(storageId);
      IResourceLocator locatorHint = resourceLocator.generateResourceLocator("attr", seed, storageName.toString());

      String contentType = dataResource.getContentType();
      boolean isCompressed = Strings.isValid(contentType) && contentType.contains("zip");

      IResource resource = new ByteStreamResource(locatorHint, rawContent, isCompressed);
      IResourceLocator locator = resourceManager.save(locatorHint, resource, DEFAULT_OPTIONS);
      Conditions.checkNotNull(locator, "locator", "Error saving resource [%s]", locatorHint.getRawPath());

      dataResource.setLocator(locator.getLocation().toASCIIString());
   }

   @Override
   public void delete(DataResource dataResource) throws OseeCoreException {
      String path = dataResource.getLocator();
      Conditions.checkNotNull(path, "resource path");

      IResourceLocator locator = resourceLocator.getResourceLocator(path);
      Conditions.checkNotNull(locator, "resource locator", "Unable to locate resource: [%s]",
         dataResource.getStorageName());

      int result = resourceManager.delete(locator);
      if (IResourceManager.OK != result) {
         throw new OseeDataStoreException("Error deleting resource located at [%s]", dataResource.getLocator());
      }
   }
}

Back to the top