summaryrefslogtreecommitdiffstats
blob: 949a63b9d582677aacdc69fe20f29834f9d8d3f1 (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
/*******************************************************************************
 * 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.manager.servlet.ats;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.server.OseeServerProperties;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.IResourceLocator;
import org.eclipse.osee.framework.resource.management.IResourceProvider;
import org.eclipse.osee.framework.resource.management.exception.MalformedLocatorException;
import org.eclipse.osee.framework.resource.management.util.OptionsProcessor;

/**
 * @author Roberto E. Escobar
 */
public class AtsResourceProvider implements IResourceProvider {
   private static final String BASE_PATH = OseeServerProperties.getOseeApplicationServerData();
   private static final String RESOLVED_PATH =
      BASE_PATH + File.separator + AtsResourceLocatorProvider.PROTOCOL + File.separator;

   public AtsResourceProvider() {
   }

   public static String getExchangeFilePath() {
      return RESOLVED_PATH;
   }

   private URI resolve(IResourceLocator locator) throws OseeCoreException {
      URI toReturn = null;
      StringBuilder builder = new StringBuilder();
      String rawPath = locator.getRawPath();
      if (!rawPath.startsWith("file:/")) {
         builder.append(RESOLVED_PATH);
         builder.append(rawPath);
         toReturn = new File(builder.toString()).toURI();
      } else {
         rawPath = rawPath.replaceAll(" ", "%20");
         try {
            toReturn = new URI(rawPath);
         } catch (URISyntaxException ex) {
            throw new MalformedLocatorException(ex);
         }
      }
      return toReturn;
   }

   @Override
   public IResource acquire(IResourceLocator locator, PropertyStore options) throws OseeCoreException {
      IResource toReturn = null;
      OptionsProcessor optionsProcessor = new OptionsProcessor(resolve(locator), locator, null, options);
      toReturn = optionsProcessor.getResourceToServer();
      return toReturn;
   }

   @Override
   public int delete(IResourceLocator locator) {
      throw new UnsupportedOperationException();
   }

   @Override
   public boolean isValid(IResourceLocator locator) {
      return locator != null && getSupportedProtocols().contains(locator.getProtocol());
   }

   @Override
   public IResourceLocator save(IResourceLocator locator, IResource resource, PropertyStore options) {
      throw new UnsupportedOperationException();
   }

   @Override
   public boolean exists(IResourceLocator locator) throws OseeCoreException {
      URI uri = resolve(locator);
      File testFile = new File(uri);
      return testFile.exists();
   }

   @Override
   public Collection<String> getSupportedProtocols() {
      return Arrays.asList(AtsResourceLocatorProvider.PROTOCOL);
   }
}