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

Adds a provider lookup for sitemaps #2162

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
Expand All @@ -32,7 +34,10 @@
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.util.StringUtil;
import org.openhab.core.library.types.StringType;
import org.openhab.core.model.core.EventType;
import org.openhab.core.model.core.ModelRepository;
import org.openhab.core.model.core.ModelRepositoryChangeListener;
import org.openhab.core.model.sitemap.SitemapProvider;
import org.openhab.core.model.sitemap.sitemap.Image;
import org.openhab.core.model.sitemap.sitemap.Sitemap;
import org.openhab.core.model.sitemap.sitemap.Video;
Expand All @@ -43,6 +48,7 @@
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
Expand Down Expand Up @@ -76,7 +82,7 @@
* @author John Cocula - added optional Image/Video item= support; refactored to allow use of later spec servlet
*/
@Component(immediate = true, property = { "service.pid=org.openhab.core.proxy" })
public class ProxyServletService extends HttpServlet {
public class ProxyServletService extends HttpServlet implements ModelRepositoryChangeListener {

/** the alias for this servlet */
public static final String PROXY_ALIAS = "proxy";
Expand All @@ -86,8 +92,19 @@ public class ProxyServletService extends HttpServlet {
public static final String ATTR_URI = ProxyServletService.class.getName() + ".URI";
public static final String ATTR_SERVLET_EXCEPTION = ProxyServletService.class.getName() + ".ProxyServletException";

private static final String SITEMAP_SUFFIX = ".sitemap";
bigbasec marked this conversation as resolved.
Show resolved Hide resolved

private final Logger logger = LoggerFactory.getLogger(ProxyServletService.class);

private final List<SitemapProvider> sitemapProviders = new ArrayList<>();

public interface SitemapSubscriptionCallback {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this interface good for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from testing. Removed.


void onEvent(Sitemap event);

void onRelease(String subscriptionId);
}

private static final long serialVersionUID = -4716754591953017793L;

private Servlet impl;
Expand All @@ -96,6 +113,28 @@ public class ProxyServletService extends HttpServlet {
protected ItemUIRegistry itemUIRegistry;
protected ModelRepository modelRepository;
bigbasec marked this conversation as resolved.
Show resolved Hide resolved

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addSitemapProvider(SitemapProvider provider) {
sitemapProviders.add(provider);
provider.addModelChangeListener(this);
}

@Override
public void modelChanged(String modelName, EventType type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the class implement ModelRepositoryChangeListener? This method does not seem to do anything besides doing a debug log statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from testing. Removed.

if (type != EventType.MODIFIED || !modelName.endsWith(SITEMAP_SUFFIX)) {
return; // we process only sitemap modifications here
}
// Don't think we really care if a model is changed, we're only using this for the sitemap lookup.
String changedSitemapName = modelName.substring(0, modelName.length() - SITEMAP_SUFFIX.length());
logger.debug("Model changed : {}", changedSitemapName);

}

protected void removeSitemapProvider(SitemapProvider provider) {
sitemapProviders.remove(provider);
provider.removeModelChangeListener(this);
}

@Reference(policy = ReferencePolicy.DYNAMIC)
protected void setItemUIRegistry(ItemUIRegistry itemUIRegistry) {
this.itemUIRegistry = itemUIRegistry;
Expand Down Expand Up @@ -245,7 +284,14 @@ URI uriFromRequest(HttpServletRequest request) {
"Parameter 'widgetId' must be provided!");
}

Sitemap sitemap = (Sitemap) modelRepository.getModel(sitemapName);
Sitemap sitemap = null;
for (SitemapProvider sitemapProvider : sitemapProviders) {
sitemap = sitemapProvider.getSitemap(sitemapName);
if (sitemap != null) {
break;
}
}

if (sitemap == null) {
throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND,
String.format("Sitemap '%s' could not be found!", sitemapName));
Expand Down