Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 31 provide jaxrs and jersey defaults #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Bundle-SymbolicName: org.eclipse.ecf.provider.jaxrs.client
Bundle-Version: 1.6.0.qualifier
Bundle-Vendor: Eclipse.org - ECF
Require-Bundle: org.eclipse.equinox.common,
org.eclipse.ecf
org.eclipse.ecf,
org.eclipse.ecf.osgi.services.remoteserviceadmin;bundle-version="4.6.1102"
Import-Package: com.fasterxml.jackson.core;version="2.9.2",
com.fasterxml.jackson.databind;version="2.9.2",
com.fasterxml.jackson.jaxrs.base;version="2.9.2",
Expand All @@ -22,7 +23,8 @@ Import-Package: com.fasterxml.jackson.core;version="2.9.2",
org.eclipse.ecf.remoteservice.provider;version="1.0.0",
org.eclipse.ecf.remoteservice.util;version="8.3.0",
org.eclipse.equinox.concurrent.future;version="1.1.0",
org.osgi.framework
org.osgi.framework,
org.osgi.service.remoteserviceadmin;version="1.1.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.ecf.provider.jaxrs.client;version="1.1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*******************************************************************************
* Copyright (c) 2020 Patrick Paulin 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:
* Patrick Paulin - initial implementation
******************************************************************************/
package org.eclipse.ecf.provider.jaxrs.client;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.EndpointDescriptionParser;
import org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescriptionReader;
import org.eclipse.ecf.provider.jaxrs.JaxRSNamespace;
import org.osgi.service.remoteserviceadmin.EndpointDescription;

/**
* An endpoint reader that sets JAX-RS defaults. These defaults are consistent
* across most clients and should not need to be set in each EDEF file.
* Currently, the only properties that are required in an EDEF file are:
* <ul>
* <li>ecf.endpoint.id (REST endpoint URL)
* <li>objectClass (interface for REST service)
* </ul>
* All other properties can be omitted, though they may be set if desired.
* <p>
* Note that a reader must be registered as an OSGi service. It is assumed that
* subclasses of this reader will do the actual registration.
*/
@SuppressWarnings("restriction")
public class JaxRSClientEndpointDescriptionReader extends EndpointDescriptionReader {

@Override
public EndpointDescription[] readEndpointDescriptions(InputStream input, Map<String, Object> overrideProperties)
throws IOException {

/*
* Need to copy input so that we can pass an unconsumed input to superclass
* method. Otherwise input is consumed when retrieving base endpoint properties.
*/
byte[] inputAsByteArray = getByteArrayFromInput(input);

Map<String, Object> baseProperties = getBaseEndpointProperties(new ByteArrayInputStream(inputAsByteArray));
addDefaultsToOverridesIfNeeded(baseProperties, overrideProperties);

return super.readEndpointDescriptions(new ByteArrayInputStream(inputAsByteArray), overrideProperties);
}

/**
* Add default property values for JAX-RS clients. Classes that override this
* method should call the superclass method.
*/
protected void addDefaultsToOverridesIfNeeded(Map<String, Object> baseProperties,
Map<String, Object> overrideProperties) {
/* JAX-RS defaults */
setDefaultPropertyIfNecessary(baseProperties, overrideProperties,
org.eclipse.ecf.osgi.services.remoteserviceadmin.RemoteConstants.ENDPOINT_CONTAINER_ID_NAMESPACE,
JaxRSNamespace.NAME);
setDefaultPropertyIfNecessary(baseProperties, overrideProperties, "remote.intents.supported",
new String[] { "passByValue", "exactlyOnce", "ordered", "jaxrs" });
setDefaultPropertyIfNecessary(baseProperties, overrideProperties,
org.eclipse.ecf.remoteservice.Constants.OSGI_SERVICE_INTENTS,
new String[] { org.eclipse.ecf.remoteservice.Constants.OSGI_ASYNC_INTENT });

/* Defaults that perhaps shouldn't be required for JAX-RS clients */
setDefaultPropertyIfNecessary(baseProperties, overrideProperties,
org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID, UUID.randomUUID().toString());
setDefaultPropertyIfNecessary(baseProperties, overrideProperties,
org.eclipse.ecf.remoteservice.Constants.SERVICE_ID, Long.valueOf(0));
setDefaultPropertyIfNecessary(baseProperties, overrideProperties,
org.eclipse.ecf.remoteservice.Constants.ENDPOINT_REMOTESERVICE_FILTER, "(objectClass=*)");
}

protected void setDefaultPropertyIfNecessary(Map<String, Object> baseProperties,
Map<String, Object> overrideProperties, String key, Object defaultValue) {
if (!baseProperties.containsKey(key) && !overrideProperties.containsKey(key)) {
overrideProperties.put(key, defaultValue);
}
}

private Map<String, Object> getBaseEndpointProperties(InputStream input) throws IOException {
EndpointDescriptionParser parser = new EndpointDescriptionParser();
parser.parse(input);

List<EndpointDescriptionParser.EndpointDescription> endpointDescriptions = parser.getEndpointDescriptions();

/*
* Returning properties for first endpoint if multiple. Not sure how to apply
* defaults if more than one endpoint in file
*/
if (endpointDescriptions.size() > 0) {
return endpointDescriptions.get(0).getProperties();
}
return new HashMap<String, Object>();
}

private byte[] getByteArrayFromInput(InputStream input) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int n = 0;
while ((n = input.read(buffer)) >= 0)
baos.write(buffer, 0, n);
input.close();

return baos.toByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Import-Package: com.fasterxml.jackson.core;version="2.9.2",
javax.ws.rs.client;version="2.0.1",
javax.ws.rs.core;version="2.0.1",
javax.ws.rs.ext;version="2.0.1",
org.eclipse.ecf.osgi.services.remoteserviceadmin;version="1.3.0",
org.eclipse.ecf.provider.jaxrs;version="1.0.0",
org.eclipse.ecf.provider.jaxrs.client;version="1.1.0",
org.eclipse.ecf.remoteservice;version="7.2.0",
Expand All @@ -25,10 +26,12 @@ Import-Package: com.fasterxml.jackson.core;version="2.9.2",
org.glassfish.jersey.client;version="2.14.0",
org.glassfish.jersey.jackson;version="2.14.0",
org.osgi.framework,
org.osgi.service.component.annotations;version="1.2.0";resolution:=optional
org.osgi.service.component.annotations;version="1.2.0";resolution:=optional,
org.osgi.service.remoteserviceadmin;version="1.1.0"
Require-Bundle: org.eclipse.ecf,
org.eclipse.equinox.common
Provide-Capability: osgi.remoteserviceadmin.distribution; configs:List<String>="ecf.jaxrs.jersey.client"; version:Version=1.0
Export-Package: org.eclipse.ecf.provider.jersey.client;version="1.1.0"
Automatic-Module-Name: org.eclipse.ecf.provider.jersey.client
Service-Component: OSGI-INF/org.eclipse.ecf.provider.jersey.client.JerseyClientDistributionProvider.xml
Service-Component: OSGI-INF/org.eclipse.ecf.provider.jersey.client.JerseyClientDistributionProvider.xml,
OSGI-INF/org.eclipse.ecf.provider.jersey.client.JerseyClientEndpointDescriptionReader.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.ecf.provider.jersey.client.JerseyClientEndpointDescriptionReader">
<service>
<provide interface="org.eclipse.ecf.osgi.services.remoteserviceadmin.IEndpointDescriptionReader"/>
</service>
<implementation class="org.eclipse.ecf.provider.jersey.client.JerseyClientEndpointDescriptionReader"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2020 Patrick Paulin 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:
* Patrick Paulin - initial implementation
******************************************************************************/
package org.eclipse.ecf.provider.jersey.client;

import java.util.Map;

import org.eclipse.ecf.osgi.services.remoteserviceadmin.IEndpointDescriptionReader;
import org.eclipse.ecf.provider.jaxrs.client.JaxRSClientEndpointDescriptionReader;
import org.osgi.service.component.annotations.Component;

/**
* This extension adds defaults specific to Jersey and also registers the reader
* as an OSGi service. This reader should be picked up first by the ECF
* framework because the default reader is registered with the minimum service
* ranking.
*/
@Component(service = IEndpointDescriptionReader.class)
public class JerseyClientEndpointDescriptionReader extends JaxRSClientEndpointDescriptionReader {

protected void addDefaultsToOverridesIfNeeded(Map<String, Object> baseProperties,
Map<String, Object> overrideProperties) {
super.addDefaultsToOverridesIfNeeded(baseProperties, overrideProperties);
setDefaultPropertyIfNecessary(baseProperties, overrideProperties, "service.imported.configs",
new String[] { JerseyClientDistributionProvider.SERVER_PROVIDER_NAME });
}
}