Skip to content

Commit

Permalink
moved the swagger servlet registration to its own component (openhab#156
Browse files Browse the repository at this point in the history
)

This should avoid potential circular dependencies at startup as the dashboard tile does not depend on HttpService anymore.

Signed-off-by: Kai Kreuzer <[email protected]>
  • Loading branch information
kaikreuzer authored Jun 9, 2017
1 parent 83d1217 commit 8797846
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 28 deletions.
3 changes: 1 addition & 2 deletions bundles/org.openhab.io.rest.docs/OSGI-INF/dashboardtile.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2015-2016 by the respective copyright holders.
Copyright (c) 2015-2017 by the respective copyright holders.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -14,5 +14,4 @@
<service>
<provide interface="org.openhab.ui.dashboard.DashboardTile"/>
</service>
<reference bind="setHttpService" cardinality="1..1" interface="org.osgi.service.http.HttpService" name="HttpService" policy="static" unbind="unsetHttpService"/>
</scr:component>
15 changes: 15 additions & 0 deletions bundles/org.openhab.io.rest.docs/OSGI-INF/swaggerservice.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2015-2017 by the respective copyright holders.
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
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.openhab.io.rest.docs.swaggerservice">
<implementation class="org.openhab.io.rest.docs.internal.SwaggerService"/>
<reference bind="setHttpService" cardinality="1..1" interface="org.osgi.service.http.HttpService" name="HttpService" policy="static" unbind="unsetHttpService"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2015-2016 by the respective copyright holders.
* Copyright (c) 2015-2017 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -9,44 +9,19 @@
package org.openhab.io.rest.docs.internal;

import org.openhab.ui.dashboard.DashboardTile;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The dashboard tile for the REST API,
* also registers the Swagger UI as a web resource on the HTTP service
*
* @author Kai Kreuzer
*
*/
public class RESTDashboardTile implements DashboardTile {

private static final String ALIAS = "/doc";

private final Logger logger = LoggerFactory.getLogger(this.getClass());

private HttpService httpService;

protected void setHttpService(HttpService httpService) {
this.httpService = httpService;
}

protected void unsetHttpService(HttpService httpService) {
this.httpService = null;
}

protected void activate() {
try {
httpService.registerResources(ALIAS, "swagger", httpService.createDefaultHttpContext());
} catch (NamespaceException e) {
logger.error("Could not start up REST documentation service: {}", e.getMessage());
}
}

protected void deactivate() {
httpService.unregister(ALIAS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2015-2017 by the respective copyright holders.
*
* 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
*/
package org.openhab.io.rest.docs.internal;

import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This service registers the Swagger UI as a web resource on the HTTP service.
*
* @author Kai Kreuzer
*
*/
public class SwaggerService {

private static final String ALIAS = "/doc";

private final Logger logger = LoggerFactory.getLogger(this.getClass());

private HttpService httpService;

protected void setHttpService(HttpService httpService) {
this.httpService = httpService;
}

protected void unsetHttpService(HttpService httpService) {
this.httpService = null;
}

protected void activate() {
try {
httpService.registerResources(ALIAS, "swagger", httpService.createDefaultHttpContext());
} catch (NamespaceException e) {
logger.error("Could not start up REST documentation service: {}", e.getMessage());
}
}

protected void deactivate() {
httpService.unregister(ALIAS);
}

}

0 comments on commit 8797846

Please sign in to comment.