Marcanpilami
Marcanpilami

Reputation: 584

OSGi HTTP whiteboard static welcome document

Inside an OSGi standard module (jar, not a wab), I am exposing both a few web services (with the OSGi JAX-RS whiteboard) and an index.html static file using @HttpWhiteboardResource (which itself works with the OSGi HTTP whiteboard).

Everything works quite well except one stupid thing: I cannot serve my index.html file from the root. (I want to do http://domain, not http://domain/index.html).

Usually the solution to this would be to declare a "welcome page". Alas, the OSGi spec for the HTTP whiteboard does not seem to allow this. An other (desperate) solution would be to bind root with @HttpWhiteboardResource.pattern, but it does not work (seems logical, the root is owned by the whiteboard).

It seems pax web has a workaround for this with a non-standard extension. However, I'm using Apache Felix http and I cannot find a single reference to welcome pages in their code or documentation. And I would prefer not to change it, since it is would require a lot of testing in a very complex solution.

So would anyone know of a way to solve my issue with either standard OSGi stuff or Felix specific stuff? Thanks!

Upvotes: 0

Views: 421

Answers (2)

Stefan Bischof
Stefan Bischof

Reputation: 1

just use a ServletContextHelper and override getResource.

@Component(service = ServletContextHelper.class, scope = 
ServiceScope.SINGLETON)

@HttpWhiteboardContext(name = WebResServletContextHelper.NAME, path = "/")
public class MyServletContextHelper extends ServletContextHelper {

public static final String NAME = "My";

@Override
public URL getResource(String name) {
             
  // name= "/" give your index.html uri back

  if("/".equals(name){
     return super.getResource("index.html")
     }
      return super.getResource(name); 
  }
}

and

@HttpWhiteboardResource(pattern = { "/*" }, prefix = "/")
@HttpWhiteboardContextSelect("(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "="
    + MyServletContextHelper.NAME + ")")
@Component(service = MyService.class)
public class MyService {

}

this should work and is 100% spec

Upvotes: 0

Grzegorz Grzybek
Grzegorz Grzybek

Reputation: 6237

Indeed, welcome files (and jsps, SCIs, login configs, ...) are not covered by OSGi specifications (HttpService, Whiteboard and WAB specs from OSGi CMPN).

That's why Pax Web was created in the first place. Recently, Pax Web 8 was released with lots of specification and stabilization improvements, so I recommend you to give it a try. See for example WhiteboardWelcomeFilesTest.java.

Upvotes: 0

Related Questions