feature[ats_GVJG0]: Update OSEE Rest Client for non-OSGI env

Allow rest client API to be used in a non-OSGI enabled
environment.

Change-Id: I56e83cf517f5d7ee6e2a494be8a3a753d6b1eedf
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.orcs.rest.client/META-INF/MANIFEST.MF
index fc78c40..f1c643d 100644
--- a/plugins/org.eclipse.osee.orcs.rest.client/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.osee.orcs.rest.client/META-INF/MANIFEST.MF
@@ -6,7 +6,8 @@
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-Vendor: Eclipse Open System Engineering Environment
 Service-Component: OSGI-INF/*.xml
-Import-Package: javax.ws.rs,
+Import-Package: javax.inject,
+ javax.ws.rs,
  javax.ws.rs.core,
  org.apache.commons.lang.time,
  org.eclipse.core.net.proxy,
@@ -18,8 +19,13 @@
  org.eclipse.osee.framework.jdk.core.util
 Export-Package: org.eclipse.osee.orcs.rest.client
 Bundle-ActivationPolicy: lazy
-Require-Bundle: org.eclipse.core.runtime,
- org.eclipse.osee.orcs.rest.model,
- com.sun.jersey;bundle-version="1.8.0",
- com.sun.jersey.client.apache;bundle-version="1.8.0",
- javax.servlet
+Require-Bundle: org.eclipse.core.runtime;visibility:=reexport,
+ org.eclipse.osee.orcs.rest.model;visibility:=reexport,
+ com.sun.jersey;bundle-version="1.8.0";visibility:=reexport,
+ com.sun.jersey.client.apache;bundle-version="1.8.0";visibility:=reexport,
+ javax.servlet;visibility:=reexport,
+ com.google.inject,
+ org.apache.commons.httpclient;visibility:=reexport,
+ org.apache.commons.logging;visibility:=reexport,
+ org.apache.log4j;visibility:=reexport,
+ org.slf4j.api;visibility:=reexport
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/OseeClientConfig.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/OseeClientConfig.java
new file mode 100644
index 0000000..bbe2f9e
--- /dev/null
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/OseeClientConfig.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.rest.client;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class OseeClientConfig {
+
+   private String serverAddress = "";
+   private String proxyAddress = "";
+
+   public OseeClientConfig(String serverAddress) {
+      this.serverAddress = serverAddress;
+   }
+
+   public String getServerAddress() {
+      return serverAddress;
+   }
+
+   public void setServerAddress(String serverAddress) {
+      this.serverAddress = serverAddress;
+   }
+
+   public String getProxyAddress() {
+      return proxyAddress;
+   }
+
+   public void setProxyAddress(String proxyAddress) {
+      this.proxyAddress = proxyAddress;
+   }
+
+   @Override
+   public String toString() {
+      return "OseeClientConfig [serverAddress=" + serverAddress + ", proxyAddress=" + proxyAddress + "]";
+   }
+
+}
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/OseeClientStandaloneSetup.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/OseeClientStandaloneSetup.java
new file mode 100644
index 0000000..7141870
--- /dev/null
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/OseeClientStandaloneSetup.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.rest.client;
+
+import org.eclipse.osee.orcs.rest.client.internal.OseeClientImpl;
+import org.eclipse.osee.orcs.rest.client.internal.StandaloneModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
+/**
+ * Class to use when using the API
+ * 
+ * @author Roberto E. Escobar
+ */
+public final class OseeClientStandaloneSetup {
+
+   private OseeClientStandaloneSetup() {
+      // Utility class
+   }
+
+   public static OseeClient createClient(OseeClientConfig config) {
+      Injector injector = Guice.createInjector(new StandaloneModule(config));
+      return injector.getInstance(OseeClientImpl.class);
+   }
+}
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeClientImpl.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeClientImpl.java
index cec4564..9fff938 100644
--- a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeClientImpl.java
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeClientImpl.java
@@ -22,6 +22,7 @@
 import org.eclipse.osee.orcs.rest.client.internal.search.QueryExecutorV1;
 import org.eclipse.osee.orcs.rest.client.internal.search.QueryOptions;
 import org.eclipse.osee.orcs.rest.model.search.Predicate;
+import com.google.inject.Inject;
 
 /**
  * @author John Misinco
@@ -35,10 +36,12 @@
    private URIProvider uriProvider;
    private WebClientProvider clientProvider;
 
+   @Inject
    public void setWebClientProvider(WebClientProvider clientProvider) {
       this.clientProvider = clientProvider;
    }
 
+   @Inject
    public void setUriProvider(URIProvider uriProvider) {
       this.uriProvider = uriProvider;
    }
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeHttpProxyAddress.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeHttpProxyAddress.java
new file mode 100644
index 0000000..d9ba792
--- /dev/null
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeHttpProxyAddress.java
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.rest.client.internal;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import com.google.inject.BindingAnnotation;
+
+/**
+ * @author Roberto E. Escobar
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@BindingAnnotation
+public @interface OseeHttpProxyAddress {
+   //
+}
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeServerAddress.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeServerAddress.java
new file mode 100644
index 0000000..952e752
--- /dev/null
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/OseeServerAddress.java
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.rest.client.internal;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import com.google.inject.BindingAnnotation;
+
+/**
+ * @author Roberto E. Escobar
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@BindingAnnotation
+public @interface OseeServerAddress {
+   //
+}
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandadloneUriProviderImpl.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandadloneUriProviderImpl.java
new file mode 100644
index 0000000..c2f8c03
--- /dev/null
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandadloneUriProviderImpl.java
@@ -0,0 +1,63 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.rest.client.internal;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Map;
+import org.eclipse.osee.framework.core.exception.OseeCoreException;
+import org.eclipse.osee.framework.core.exception.OseeExceptions;
+import org.eclipse.osee.framework.core.services.URIProvider;
+import org.eclipse.osee.framework.jdk.core.util.HttpUrlBuilder;
+import com.google.inject.Inject;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class StandadloneUriProviderImpl implements URIProvider {
+
+   private final String serverAddress;
+
+   @Inject
+   public StandadloneUriProviderImpl(@OseeServerAddress String serverAddress) {
+      this.serverAddress = serverAddress;
+   }
+
+   public String getServerAddress() {
+      String value = serverAddress;
+      if (!value.endsWith("/")) {
+         value += '/';
+      }
+      return value;
+   }
+
+   @Override
+   public URI getApplicationServerURI() throws OseeCoreException {
+      URI toReturn = null;
+      try {
+         toReturn = new URI(getServerAddress());
+      } catch (URISyntaxException ex) {
+         OseeExceptions.wrapAndThrow(ex);
+      }
+      return toReturn;
+   }
+
+   @Override
+   public URI getEncodedURI(String context, Map<String, String> params) throws OseeCoreException {
+      URI toReturn = null;
+      try {
+         toReturn = new URI(HttpUrlBuilder.createURL(getServerAddress(), context, params));
+      } catch (Exception ex) {
+         OseeExceptions.wrapAndThrow(ex);
+      }
+      return toReturn;
+   }
+};
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandadloneWebClientProvider.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandadloneWebClientProvider.java
new file mode 100644
index 0000000..1abed53
--- /dev/null
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandadloneWebClientProvider.java
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.rest.client.internal;
+
+import java.net.URI;
+import java.util.Map;
+import org.eclipse.osee.framework.jdk.core.util.Strings;
+import com.google.inject.Inject;
+import com.sun.jersey.api.client.AsyncWebResource;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.client.apache.ApacheHttpClient;
+import com.sun.jersey.client.apache.config.ApacheHttpClientConfig;
+import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class StandadloneWebClientProvider implements WebClientProvider {
+
+   private Client client;
+   private final String proxyAddress;
+
+   @Inject
+   public StandadloneWebClientProvider(@OseeHttpProxyAddress String proxyAddress) {
+      this.proxyAddress = proxyAddress;
+   }
+
+   @Override
+   public WebResource createResource(URI uri) {
+      Client client = createClient(uri);
+      return client.resource(uri);
+   }
+
+   @Override
+   public AsyncWebResource createAsyncResource(URI uri) {
+      Client client = createClient(uri);
+      return client.asyncResource(uri);
+   }
+
+   private Client createClient(URI uri) {
+      if (client == null) {
+         DefaultApacheHttpClientConfig clientConfig = new DefaultApacheHttpClientConfig();
+         Map<String, Object> properties = clientConfig.getProperties();
+
+         properties.put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
+         if (Strings.isValid(proxyAddress)) {
+            properties.put(ApacheHttpClientConfig.PROPERTY_PROXY_URI, proxyAddress);
+         }
+         client = ApacheHttpClient.create(clientConfig);
+      }
+      return client;
+   }
+
+}
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandaloneModule.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandaloneModule.java
new file mode 100644
index 0000000..73c98f0
--- /dev/null
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/StandaloneModule.java
@@ -0,0 +1,79 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.rest.client.internal;
+
+import org.eclipse.osee.framework.core.services.URIProvider;
+import org.eclipse.osee.orcs.rest.client.OseeClient;
+import org.eclipse.osee.orcs.rest.client.OseeClientConfig;
+import com.google.inject.AbstractModule;
+import com.google.inject.TypeLiteral;
+import com.google.inject.matcher.AbstractMatcher;
+import com.google.inject.matcher.Matcher;
+import com.google.inject.spi.InjectionListener;
+import com.google.inject.spi.TypeEncounter;
+import com.google.inject.spi.TypeListener;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class StandaloneModule extends AbstractModule {
+
+   private final OseeClientConfig config;
+
+   public StandaloneModule(OseeClientConfig config) {
+      this.config = config;
+   }
+
+   @Override
+   protected void configure() {
+      bindConstant().annotatedWith(OseeServerAddress.class).to(config.getServerAddress());
+      bindConstant().annotatedWith(OseeHttpProxyAddress.class).to(config.getProxyAddress());
+
+      bind(OseeClient.class).to(OseeClientImpl.class);
+      bind(WebClientProvider.class).to(StandadloneWebClientProvider.class);
+      bind(URIProvider.class).to(StandadloneUriProviderImpl.class);
+
+      TypeListener listener = new TypeListener() {
+
+         @Override
+         public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
+            encounter.register(new InjectionListener<I>() {
+
+               @Override
+               public void afterInjection(I injectee) {
+                  OseeClientImpl client = (OseeClientImpl) injectee;
+                  client.start();
+               }
+            });
+         }
+      };
+      bindListener(subtypeOf(OseeClientImpl.class), listener);
+   }
+
+   private static Matcher<? super TypeLiteral<?>> subtypeOf(Class<?> superclass) {
+      return new SubTypeOfMatcher(TypeLiteral.get(OseeClientImpl.class));
+   }
+
+   private static final class SubTypeOfMatcher extends AbstractMatcher<TypeLiteral<?>> {
+      private final TypeLiteral<OseeClientImpl> superType;
+
+      public SubTypeOfMatcher(TypeLiteral<OseeClientImpl> superType) {
+         super();
+         this.superType = superType;
+      }
+
+      @Override
+      public boolean matches(TypeLiteral<?> subType) {
+         return subType.equals(superType) || superType.getRawType().isAssignableFrom(subType.getRawType());
+      }
+
+   }
+}
diff --git a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/search/PredicateFactoryImpl.java b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/search/PredicateFactoryImpl.java
index 395bab4..e11b776 100644
--- a/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/search/PredicateFactoryImpl.java
+++ b/plugins/org.eclipse.osee.orcs.rest.client/src/org/eclipse/osee/orcs/rest/client/internal/search/PredicateFactoryImpl.java
@@ -147,6 +147,8 @@
          case LESS_THAN:
             toReturn = SearchOp.LESS_THAN;
             break;
+         default:
+            break;
       }
       return toReturn;
    }