Search in sources :

Example 1 with RestService

use of org.opencastproject.util.doc.rest.RestService in project opencast by opencast.

the class Activator method writeServiceDocumentation.

private void writeServiceDocumentation(final String docPath, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    ServiceReference reference = null;
    for (ServiceReference ref : getRestEndpointServices()) {
        String alias = (String) ref.getProperty(SERVICE_PATH_PROPERTY);
        if (docPath.equalsIgnoreCase(alias)) {
            reference = ref;
            break;
        }
    }
    final StringBuilder docs = new StringBuilder();
    if (reference == null) {
        docs.append("REST docs unavailable for ");
        docs.append(docPath);
    } else {
        final Object restService = bundleContext.getService(reference);
        findRestAnnotation(restService.getClass()).fold(new Option.Match<RestService, Void>() {

            @Override
            public Void some(RestService annotation) {
                globalMacro.put("SERVICE_CLASS_SIMPLE_NAME", restService.getClass().getSimpleName());
                RestDocData data = new RestDocData(annotation.name(), annotation.title(), docPath, annotation.notes(), restService, globalMacro);
                data.setAbstract(annotation.abstractText());
                for (Method m : restService.getClass().getMethods()) {
                    RestQuery rq = (RestQuery) m.getAnnotation(RestQuery.class);
                    String httpMethodString = null;
                    for (Annotation a : m.getAnnotations()) {
                        HttpMethod httpMethod = (HttpMethod) a.annotationType().getAnnotation(HttpMethod.class);
                        if (httpMethod != null) {
                            httpMethodString = httpMethod.value();
                        }
                    }
                    Produces produces = (Produces) m.getAnnotation(Produces.class);
                    Path path = (Path) m.getAnnotation(Path.class);
                    Class<?> returnType = m.getReturnType();
                    if ((rq != null) && (httpMethodString != null) && (path != null)) {
                        data.addEndpoint(rq, returnType, produces, httpMethodString, path);
                    }
                }
                String template = DocUtil.loadTemplate("/ui/restdocs/template.xhtml");
                docs.append(DocUtil.generate(data, template));
                return null;
            }

            @Override
            public Void none() {
                docs.append("No documentation has been found for ").append(restService.getClass().getSimpleName());
                return null;
            }
        });
    }
    resp.setContentType("text/html");
    resp.getWriter().write(docs.toString());
}
Also used : Path(javax.ws.rs.Path) RestDocData(org.opencastproject.runtimeinfo.rest.RestDocData) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) ServiceReference(org.osgi.framework.ServiceReference) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery) Option(org.opencastproject.util.data.Option) RestService(org.opencastproject.util.doc.rest.RestService) HttpMethod(javax.ws.rs.HttpMethod)

Example 2 with RestService

use of org.opencastproject.util.doc.rest.RestService in project opencast by opencast.

the class RestDocsAnnotationTest method testRestServiceDocs.

/**
 * This tests the functionality of @RestService annotation type.
 */
@Test
public void testRestServiceDocs() {
    RestService restServiceAnnotation = TestServletSample.class.getAnnotation(RestService.class);
    // name, title and abstract text
    assertEquals("ingestservice", restServiceAnnotation.name());
    assertEquals("Ingest Service", restServiceAnnotation.title());
    assertEquals("This service creates and augments Opencast media packages that include media tracks, metadata catalogs and attachments.", restServiceAnnotation.abstractText());
    // notes
    assertEquals(2, restServiceAnnotation.notes().length);
    assertEquals("All paths above are relative to the REST endpoint base (something like http://your.server/files)", restServiceAnnotation.notes()[0]);
    assertEquals("If the service is down or not working it will return a status 503, this means the the underlying service is not working and is either restarting or has failed", restServiceAnnotation.notes()[1]);
}
Also used : RestService(org.opencastproject.util.doc.rest.RestService) Test(org.junit.Test)

Aggregations

RestService (org.opencastproject.util.doc.rest.RestService)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 HttpMethod (javax.ws.rs.HttpMethod)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Test (org.junit.Test)1 RestDocData (org.opencastproject.runtimeinfo.rest.RestDocData)1 Option (org.opencastproject.util.data.Option)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1 ServiceReference (org.osgi.framework.ServiceReference)1