Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorslewis2013-12-04 03:23:52 +0000
committerslewis2013-12-04 03:23:52 +0000
commit3ed60267c3fa4ee5aa9fc254f70d52cde1d55f17 (patch)
tree3fd610f1cd57148bd0e00e7f1bfffe5b83a6a01d /examples
parenta514f32f7f00749b3cfb46d0337fd09fa817a2a0 (diff)
downloadorg.eclipse.ecf-3ed60267c3fa4ee5aa9fc254f70d52cde1d55f17.tar.gz
org.eclipse.ecf-3ed60267c3fa4ee5aa9fc254f70d52cde1d55f17.tar.xz
org.eclipse.ecf-3ed60267c3fa4ee5aa9fc254f70d52cde1d55f17.zip
Simplification of timeservice rest host and rest client provider
Diffstat (limited to 'examples')
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.consumer.filediscovery.rest/launch/TimeServiceRestConsumer DS.launch2
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.host/launch/TimeServiceRestHost.launch2
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/META-INF/MANIFEST.MF2
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestID.java25
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestNamespace.java26
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/META-INF/MANIFEST.MF1
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java75
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainerInstantiator.java32
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/META-INF/MANIFEST.MF1
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java61
-rw-r--r--examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainerInstantiator.java53
11 files changed, 118 insertions, 162 deletions
diff --git a/examples/bundles/com.mycorp.examples.timeservice.consumer.filediscovery.rest/launch/TimeServiceRestConsumer DS.launch b/examples/bundles/com.mycorp.examples.timeservice.consumer.filediscovery.rest/launch/TimeServiceRestConsumer DS.launch
index c57b7b23c..4c543a9eb 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.consumer.filediscovery.rest/launch/TimeServiceRestConsumer DS.launch
+++ b/examples/bundles/com.mycorp.examples.timeservice.consumer.filediscovery.rest/launch/TimeServiceRestConsumer DS.launch
@@ -22,5 +22,5 @@
<booleanAttribute key="tracing" value="false"/>
<booleanAttribute key="useCustomFeatures" value="false"/>
<booleanAttribute key="useDefaultConfigArea" value="true"/>
-<stringAttribute key="workspace_bundles" value="com.mycorp.examples.timeservice.consumer.ds@default:default,com.mycorp.examples.timeservice.consumer.filediscovery.rest@default:false,com.mycorp.examples.timeservice.provider.rest.common@default:default,com.mycorp.examples.timeservice.provider.rest.consumer@default:default,com.mycorp.examples.timeservice@default:default,org.eclipse.ecf.remoteservice.rest@default:default,org.eclipse.ecf.remoteservice@default:default"/>
+<stringAttribute key="workspace_bundles" value="com.mycorp.examples.timeservice.consumer.ds@default:default,com.mycorp.examples.timeservice.consumer.filediscovery.rest@default:false,com.mycorp.examples.timeservice.provider.rest.common@default:default,com.mycorp.examples.timeservice.provider.rest.consumer@default:default,com.mycorp.examples.timeservice@default:default,org.eclipse.ecf.remoteservice.rest@default:default,org.eclipse.ecf.remoteservice@default:default,org.json@default:default"/>
</launchConfiguration>
diff --git a/examples/bundles/com.mycorp.examples.timeservice.host/launch/TimeServiceRestHost.launch b/examples/bundles/com.mycorp.examples.timeservice.host/launch/TimeServiceRestHost.launch
index 7f07269e7..fbe6721fe 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.host/launch/TimeServiceRestHost.launch
+++ b/examples/bundles/com.mycorp.examples.timeservice.host/launch/TimeServiceRestHost.launch
@@ -22,5 +22,5 @@
<booleanAttribute key="tracing" value="false"/>
<booleanAttribute key="useCustomFeatures" value="false"/>
<booleanAttribute key="useDefaultConfigArea" value="true"/>
-<stringAttribute key="workspace_bundles" value="com.mycorp.examples.timeservice.host@default:default,com.mycorp.examples.timeservice.provider.rest.common@default:default,com.mycorp.examples.timeservice.provider.rest.host@default:default,com.mycorp.examples.timeservice@default:default,org.apache.commons.codec@default:default,org.apache.commons.logging@default:default,org.apache.httpcomponents.httpclient@default:default,org.apache.httpcomponents.httpcore@default:default,org.eclipse.ecf.remoteservice.rest@default:default,org.eclipse.ecf.remoteservice.servlet@default:default,org.eclipse.ecf.remoteservice@default:default,org.eclipse.ecf.ssl@default:false,org.eclipse.equinox.transforms.hook@default:false"/>
+<stringAttribute key="workspace_bundles" value="com.mycorp.examples.timeservice.host@default:default,com.mycorp.examples.timeservice.provider.rest.common@default:default,com.mycorp.examples.timeservice.provider.rest.host@default:default,com.mycorp.examples.timeservice@default:default,org.apache.commons.codec@default:default,org.apache.commons.logging@default:default,org.apache.httpcomponents.httpclient@default:default,org.apache.httpcomponents.httpcore@default:default,org.eclipse.ecf.remoteservice.rest@default:default,org.eclipse.ecf.remoteservice.servlet@default:default,org.eclipse.ecf.remoteservice@default:default,org.eclipse.ecf.ssl@default:false,org.json@default:default"/>
</launchConfiguration>
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/META-INF/MANIFEST.MF b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/META-INF/MANIFEST.MF
index bdd086d78..baf07f427 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/META-INF/MANIFEST.MF
+++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Timerservice Rest Provider Common
Bundle-SymbolicName: com.mycorp.examples.timeservice.provider.rest.common;singleton:=true
-Bundle-Version: 1.0.0.qualifier
+Bundle-Version: 2.0.0.qualifier
Bundle-Vendor: MyCorp, Inc.
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Import-Package: org.eclipse.ecf.core.identity,
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestID.java b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestID.java
deleted file mode 100644
index 6bc017dfd..000000000
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestID.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*******************************************************************************
-* Copyright (c) 2013 Composent, Inc. and others. 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:
-* Composent, Inc. - initial API and implementation
-******************************************************************************/
-package com.mycorp.examples.timeservice.provider.rest.common;
-
-import java.net.URI;
-
-import org.eclipse.ecf.core.identity.Namespace;
-import org.eclipse.ecf.remoteservice.rest.identity.RestID;
-
-public class TimeServiceRestID extends RestID {
-
- private static final long serialVersionUID = 6964783426775839086L;
-
- public TimeServiceRestID(Namespace namespace, URI uri) {
- super(namespace, uri);
- }
-
-}
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestNamespace.java b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestNamespace.java
index e8c194de1..74bb06f48 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestNamespace.java
+++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.common/src/com/mycorp/examples/timeservice/provider/rest/common/TimeServiceRestNamespace.java
@@ -13,6 +13,8 @@ import java.net.URI;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDCreateException;
+import org.eclipse.ecf.core.identity.Namespace;
+import org.eclipse.ecf.remoteservice.rest.identity.RestID;
import org.eclipse.ecf.remoteservice.rest.identity.RestNamespace;
public class TimeServiceRestNamespace extends RestNamespace {
@@ -23,19 +25,19 @@ public class TimeServiceRestNamespace extends RestNamespace {
@Override
public ID createInstance(Object[] parameters) throws IDCreateException {
- try {
- String uriString = getInitStringFromExternalForm(parameters);
- if (uriString == null)
- uriString = (String) parameters[0];
- return new TimeServiceRestID(this, URI.create(uriString));
- } catch (Exception e) {
- throw new IDCreateException("Could not create TimeServiceRestID", e);
- }
+ return new TimeServiceRestID(this, URI.create((String) parameters[0]));
}
- @SuppressWarnings("rawtypes")
- @Override
- public Class[][] getSupportedParameterTypes() {
- return new Class[][] { { String.class } };
+ /**
+ * @since 2.0
+ */
+ public static class TimeServiceRestID extends RestID {
+
+ private static final long serialVersionUID = 688293496962799572L;
+
+ public TimeServiceRestID(Namespace namespace, URI uri) {
+ super(namespace, uri);
+ }
}
+
}
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/META-INF/MANIFEST.MF b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/META-INF/MANIFEST.MF
index 409d44783..d255de3d2 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/META-INF/MANIFEST.MF
+++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/META-INF/MANIFEST.MF
@@ -15,6 +15,7 @@ Import-Package: com.mycorp.examples.timeservice;version="1.0.0",
org.eclipse.ecf.remoteservice.rest.identity;version="2.2.200",
org.eclipse.ecf.remoteservice.util;version="8.1.0",
org.eclipse.equinox.concurrent.future;version="1.1.0",
+ org.json,
org.osgi.framework
Require-Bundle: org.eclipse.equinox.common,
org.eclipse.ecf
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java
index 32d13f6eb..df0f1efc5 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java
+++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainer.java
@@ -1,15 +1,14 @@
/*******************************************************************************
-* Copyright (c) 2013 Composent, Inc. and others. 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:
-* Composent, Inc. - initial API and implementation
-******************************************************************************/
+ * Copyright (c) 2013 Composent, Inc. and others. 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:
+ * Composent, Inc. - initial API and implementation
+ ******************************************************************************/
package com.mycorp.examples.timeservice.internal.provider.rest.consumer;
-import java.io.IOException;
import java.io.NotSerializableException;
import java.util.Map;
@@ -23,39 +22,28 @@ import org.eclipse.ecf.remoteservice.IRemoteServiceRegistration;
import org.eclipse.ecf.remoteservice.client.IRemoteCallable;
import org.eclipse.ecf.remoteservice.client.IRemoteResponseDeserializer;
import org.eclipse.ecf.remoteservice.rest.RestCallableFactory;
-import org.eclipse.ecf.remoteservice.rest.client.HttpPostRequestType;
+import org.eclipse.ecf.remoteservice.rest.client.HttpGetRequestType;
import org.eclipse.ecf.remoteservice.rest.client.RestClientContainer;
import org.eclipse.ecf.remoteservice.rest.identity.RestID;
-import org.eclipse.ecf.remoteservice.util.ObjectSerializationUtil;
+import org.json.JSONException;
+import org.json.JSONObject;
import com.mycorp.examples.timeservice.ITimeService;
import com.mycorp.examples.timeservice.provider.rest.common.TimeServiceRestNamespace;
public class TimeServiceRestClientContainer extends RestClientContainer {
- public static final String NAME = "com.mycorp.examples.timeservice.rest.consumer";
-
- class TimeServiceRestResponseDeserializer implements
- IRemoteResponseDeserializer {
- public Object deserializeResponse(String endpoint, IRemoteCall call,
- IRemoteCallable callable,
- @SuppressWarnings("rawtypes") Map responseHeaders,
- byte[] responseBody) throws NotSerializableException {
- try {
- return new ObjectSerializationUtil()
- .deserializeFromBytes(responseBody);
- } catch (IOException e) {
- throw new NotSerializableException(
- "Could not deserialize server response");
- }
- }
-
- }
+ public static final String TIMESERVICE_CONSUMER_CONFIG_NAME = "com.mycorp.examples.timeservice.rest.consumer";
private IRemoteServiceRegistration reg;
- public TimeServiceRestClientContainer(RestID id) {
- super(id);
+ TimeServiceRestClientContainer() {
+ // Create a random ID for the client container
+ super((RestID) IDFactory.getDefault().createID(
+ TimeServiceRestNamespace.NAME, "uuid:"
+ + java.util.UUID.randomUUID().toString()));
+ // This sets up the JSON deserialization of the server's response.
+ // See below for implementation of TimeServiceRestResponseDeserializer
setResponseDeserializer(new TimeServiceRestResponseDeserializer());
}
@@ -63,10 +51,13 @@ public class TimeServiceRestClientContainer extends RestClientContainer {
public void connect(ID targetID, IConnectContext connectContext1)
throws ContainerConnectException {
super.connect(targetID, connectContext1);
- // Now setup TimeService remote registration
+ // Create the IRemoteCallable to represent
+ // access to the ITimeService method.
IRemoteCallable callable = RestCallableFactory.createCallable(
"getCurrentTime", ITimeService.class.getName(), null,
- new HttpPostRequestType(),30000);
+ new HttpGetRequestType(), 30000);
+ // Register the callable and associate it with the ITimeService class
+ // name
reg = registerCallables(new String[] { ITimeService.class.getName() },
new IRemoteCallable[][] { { callable } }, null);
}
@@ -80,6 +71,24 @@ public class TimeServiceRestClientContainer extends RestClientContainer {
}
}
+ class TimeServiceRestResponseDeserializer implements
+ IRemoteResponseDeserializer {
+ public Object deserializeResponse(String endpoint, IRemoteCall call,
+ IRemoteCallable callable,
+ @SuppressWarnings("rawtypes") Map responseHeaders,
+ byte[] responseBody) throws NotSerializableException {
+ // We simply need to read the response body (json String),
+ // And return the value of the "time" field
+ try {
+ return new JSONObject(new String(responseBody)).get("time");
+ } catch (JSONException e1) {
+ throw new NotSerializableException(
+ TimeServiceRestResponseDeserializer.class.getName());
+ }
+ }
+
+ }
+
@Override
public Namespace getConnectNamespace() {
return IDFactory.getDefault().getNamespaceByName(
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainerInstantiator.java b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainerInstantiator.java
index c22e84d16..bd4531625 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainerInstantiator.java
+++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.consumer/src/com/mycorp/examples/timeservice/internal/provider/rest/consumer/TimeServiceRestClientContainerInstantiator.java
@@ -15,45 +15,29 @@ import java.util.List;
import org.eclipse.ecf.core.ContainerCreateException;
import org.eclipse.ecf.core.ContainerTypeDescription;
import org.eclipse.ecf.core.IContainer;
-import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.remoteservice.rest.client.RestClientContainerInstantiator;
-import com.mycorp.examples.timeservice.provider.rest.common.TimeServiceRestID;
-import com.mycorp.examples.timeservice.provider.rest.common.TimeServiceRestNamespace;
-
public class TimeServiceRestClientContainerInstantiator extends
RestClientContainerInstantiator {
- private static final String TIMESERVICE_SERVER_NAME = "com.mycorp.examples.timeservice.rest.host";
+ private static final String TIMESERVICE_HOST_CONFIG_NAME = "com.mycorp.examples.timeservice.rest.host";
@Override
public IContainer createInstance(ContainerTypeDescription description,
Object[] parameters) throws ContainerCreateException {
- try {
- String containerId = "uuid:"
- + java.util.UUID.randomUUID().toString();
- return new TimeServiceRestClientContainer(
- (TimeServiceRestID) IDFactory.getDefault().createID(
- TimeServiceRestNamespace.NAME, containerId));
- } catch (Exception e) {
- throw new ContainerCreateException(
- "Could not create TimeServiceRestClientContainer", e);
- }
- }
-
- @Override
- public String[] getSupportedAdapterTypes(
- ContainerTypeDescription description) {
- return getInterfacesAndAdaptersForClass(TimeServiceRestClientContainer.class);
+ // Create new container instance
+ return new TimeServiceRestClientContainer();
}
public String[] getImportedConfigs(ContainerTypeDescription description,
String[] exporterSupportedConfigs) {
@SuppressWarnings("rawtypes")
List supportedConfigs = Arrays.asList(exporterSupportedConfigs);
- if (supportedConfigs.contains(TIMESERVICE_SERVER_NAME))
- return new String[] { TimeServiceRestClientContainer.NAME };
- return null;
+ // If the supportedConfigs contains the timeservice host config,
+ // then we are the client to handle it!
+ if (supportedConfigs.contains(TIMESERVICE_HOST_CONFIG_NAME))
+ return new String[] { TimeServiceRestClientContainer.TIMESERVICE_CONSUMER_CONFIG_NAME };
+ else return null;
}
}
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/META-INF/MANIFEST.MF b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/META-INF/MANIFEST.MF
index 1fb280d4f..020ce6679 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/META-INF/MANIFEST.MF
+++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/META-INF/MANIFEST.MF
@@ -17,6 +17,7 @@ Import-Package: com.mycorp.examples.timeservice;version="1.0.0",
org.eclipse.ecf.remoteservice.servlet,
org.eclipse.ecf.remoteservice.util;version="8.1.0",
org.eclipse.equinox.concurrent.future;version="1.1.0",
+ org.json,
org.osgi.framework;version="1.7.0",
org.osgi.service.http;version="1.2.1"
Service-Component: OSGI-INF/httpservicecomponent.xml
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java
index fa592cbba..e94a0a0c3 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java
+++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainer.java
@@ -15,42 +15,43 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.eclipse.ecf.core.identity.ID;
+import org.eclipse.ecf.core.ContainerCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;
+import org.eclipse.ecf.remoteservice.servlet.HttpServiceComponent;
import org.eclipse.ecf.remoteservice.servlet.RemoteServiceHttpServlet;
import org.eclipse.ecf.remoteservice.servlet.ServletServerContainer;
-import org.eclipse.ecf.remoteservice.servlet.HttpServiceComponent;
-import org.eclipse.ecf.remoteservice.servlet.ObjectSerializationResponseSerializer;
+import org.json.JSONException;
+import org.json.JSONObject;
import org.osgi.service.http.HttpService;
-import org.osgi.service.http.NamespaceException;
import com.mycorp.examples.timeservice.ITimeService;
import com.mycorp.examples.timeservice.provider.rest.common.TimeServiceRestNamespace;
public class TimeServiceServerContainer extends ServletServerContainer {
- public static final String NAME = "com.mycorp.examples.timeservice.rest.host";
+ public static final String TIMESERVICE_HOST_CONFIG_NAME = "com.mycorp.examples.timeservice.rest.host";
+ public static final String TIMESERVICE_SERVLET_NAME = "/" + ITimeService.class.getName();
- private final String timeServiceServletName = "/" + ITimeService.class.getName();
-
- private HttpService httpService;
+ private final HttpService httpService;
- public TimeServiceServerContainer(ID id, HttpService httpService) throws ServletException,
- NamespaceException {
- super(id);
+ TimeServiceServerContainer(String id, HttpService httpService) throws ContainerCreateException {
+ super(IDFactory.getDefault()
+ .createID(TimeServiceRestNamespace.NAME, id));
this.httpService = httpService;
- // Register our servlet with
- this.httpService.registerServlet(timeServiceServletName,
- new TimeRemoteServiceHttpServlet(), null, null);
+ // Register our servlet with the given httpService with the TIMESERVICE_SERVLET_NAME
+ // which is "/com.mycorp.examples.timeservice.ITimeService"
+ try {
+ this.httpService.registerServlet(TIMESERVICE_SERVLET_NAME,
+ new TimeRemoteServiceHttpServlet(), null, null);
+ } catch (Exception e) {
+ throw new ContainerCreateException("Could not create Time Service Server Container",e);
+ }
}
@Override
public void dispose() {
- if (httpService != null) {
- httpService.unregister(timeServiceServletName);
- httpService = null;
- }
+ httpService.unregister(TIMESERVICE_SERVLET_NAME);
super.dispose();
}
@@ -64,30 +65,28 @@ public class TimeServiceServerContainer extends ServletServerContainer {
private static final long serialVersionUID = 3906126401901826462L;
- public TimeRemoteServiceHttpServlet() {
- // Set response serializer to serialized time
- // service call
- setRemoteCallResponseSerializer(new ObjectSerializationResponseSerializer());
- }
-
- // Handle post call right here.
+ // Handle get call right here.
@Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// No arguments to getCurrentTime() method, so
- // nothing to deserialize
+ // nothing to deserialize from request
- // Get local ITimeService
+ // Get local OSGi ITimeService
ITimeService timeService = HttpServiceComponent.getDefault()
.getService(ITimeService.class);
- // Call local service
+ // Call local service to get the time
Long currentTime = timeService.getCurrentTime();
// Serialize response
- getRemoteCallResponseSerializer().serializeResponse(resp,
- currentTime);
+ try {
+ JSONObject json = new JSONObject().put("time", currentTime);
+ resp.getOutputStream().print(json.toString());
+ } catch (JSONException e) {
+ throw new ServletException("json response object could not be created for time service", e);
+ }
}
}
diff --git a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainerInstantiator.java b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainerInstantiator.java
index d8456e562..22860220c 100644
--- a/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainerInstantiator.java
+++ b/examples/bundles/com.mycorp.examples.timeservice.provider.rest.host/src/com/mycorp/examples/timeservice/internal/provider/rest/host/TimeServiceServerContainerInstantiator.java
@@ -1,56 +1,41 @@
/*******************************************************************************
-* Copyright (c) 2013 Composent, Inc. and others. 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:
-* Composent, Inc. - initial API and implementation
-******************************************************************************/
+ * Copyright (c) 2013 Composent, Inc. and others. 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:
+ * Composent, Inc. - initial API and implementation
+ ******************************************************************************/
package com.mycorp.examples.timeservice.internal.provider.rest.host;
-import java.util.Collection;
import java.util.Map;
import org.eclipse.ecf.core.ContainerCreateException;
import org.eclipse.ecf.core.ContainerTypeDescription;
import org.eclipse.ecf.core.IContainer;
-import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.remoteservice.servlet.ServletServerContainerInstantiator;
import org.osgi.service.http.HttpService;
-import com.mycorp.examples.timeservice.provider.rest.common.TimeServiceRestNamespace;
-
public class TimeServiceServerContainerInstantiator extends
ServletServerContainerInstantiator {
@Override
public IContainer createInstance(ContainerTypeDescription description,
Object[] parameters) throws ContainerCreateException {
- try {
- // Get HttpServices
- Collection<HttpService> httpServices = TimeServiceHttpServiceComponent.getDefault().getHttpServices();
- if (httpServices == null || httpServices.size() == 0) throw new NullPointerException("Cannot get HttpService for TimeServiceServerContainer creation");
- // If we've got more than one, then we'll just use the first one
- HttpService httpService = httpServices.iterator().next();
- @SuppressWarnings("unchecked")
- Map<String, Object> map = (Map<String, Object>) parameters[0];
- String id = (String) map.get("id");
- return new TimeServiceServerContainer(IDFactory.getDefault()
- .createID(TimeServiceRestNamespace.NAME, id), httpService);
- } catch (Exception e) {
- throw new ContainerCreateException(
- "Could not create TimeServiceServerContainer", e);
- }
- }
-
- @Override
- public String[] getSupportedAdapterTypes(
- ContainerTypeDescription description) {
- return getInterfacesAndAdaptersForClass(TimeServiceServerContainer.class);
+ // Get first http services from HttpServiceComponent
+ HttpService httpService = TimeServiceHttpServiceComponent.getDefault()
+ .getHttpServices().iterator().next();
+ @SuppressWarnings("unchecked")
+ // First parameter should be Map
+ Map<String, Object> map = (Map<String, Object>) parameters[0];
+ // Get the ID parameter from Map
+ return new TimeServiceServerContainer((String) map.get("id"),
+ httpService);
}
public String[] getSupportedConfigs(ContainerTypeDescription description) {
- return new String[] { TimeServiceServerContainer.NAME };
+ return new String[] { TimeServiceServerContainer.TIMESERVICE_HOST_CONFIG_NAME };
}
+
}

Back to the top