2
votes

I'm trying to access content on the WEB-INF folder and use the file into my JAXB unmarshalling function, but i've tried everything and no success. First of all, i used a jersey and grizzly server to test it locally, but now i want to move it to a tomcat server and i deployed it using a .war file which had the WEB-INF folder and a index.html.

I have 3 packages (Resources, Models and the main package). Which one is where i have my resources.
In one of them (Models), I have a classe that i'm using this unmarshalling function to a file inside the WEB-INF folder, and this class is beeing called by a class inside Resources.

How can i access the files inside WEB-INF?

EDIT - It's not built into a servlet class, let me explain a little bit more. I'm creating a restful api using jaxrs and jersey.

Catalogo Class (its a resource mapped to a url).

@Path("catalogo")
public class CatalogoResource {
    @Context ServletContext context;


    @Path("{id}/{ano}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Catalogo retornaCatalogo(@PathParam("id") int id, @PathParam("ano") int ano) throws URISyntaxException, MalformedURLException{
        //buscar no arquivo, dar unmarshall pra ca.
        Catalogo cat = new CatalogoDAO().open(ano, id);
        //pensar em como retornar o xml;
        return cat;
    }

}

and than, the class CatalogoDAO that is used inside the catalogo, it is on the models package.

    public class CatalogoDAO{
    private Catalogo catalog = new Catalogo();
    ServletContext context;

    public Catalogo open(int ano, int curso) throws URISyntaxException {
        try {           
            InputStream resourceAsStream = context.getResourceAsStream("/WEB-INF/catalogos/2012_36.xml"); //line of the null pointer.
            JAXBContext jaxbContext = JAXBContext.newInstance(Catalogo.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            catalog = (Catalogo) jaxbUnmarshaller.unmarshal(resourceAsStream);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
     return catalog;
    }
}

But still no success.

Adding images of my get request and the folder structure. The first image shows the exception saying the file doesnt exist, but as you can see, the second image proves it exists and shows the folder structure inside my tomcat server before the .war expansion.

type Exception report

message java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.NullPointerException br.unicamp.ft.courseviewer.modelo.CatalogoDAO.open(CatalogoDAO.java:22) br.unicamp.ft.courseviewer.resource.CatalogoResource.retornaCatalogo(CatalogoResource.java:24) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151) org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171) org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195) org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104) org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:402) org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:349) org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106) org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259) org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) org.glassfish.jersey.internal.Errors.process(Errors.java:315) org.glassfish.jersey.internal.Errors.process(Errors.java:297) org.glassfish.jersey.internal.Errors.process(Errors.java:267) org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318) org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236) org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010) org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

enter image description here

1
@dsp_user the IDE doesnt recognise the ServletContext class.PlayMa256
Use import javax.servlet.ServletContext;dsp_user
@dsp_user i've imported it from servelt-api and i made an edit. Try to see look at it.PlayMa256
What exactly is wrong with that code? What kind of exception are you getting?dsp_user

1 Answers

2
votes

OK. So the stack trace says:

 java.lang.NullPointerException  
 br.unicamp.ft.courseviewer.modelo.CatalogoDAO.open(CatalogoDAO.java:22)

So, at line 22, which is

InputStream resourceAsStream = context.getResourceAsStream("/WEB-INF/catalogos/2012_36.xml");

you get a NullPointerException.

That means that context is null.

Why is it null?

Because you have never initialized it. It's just a field of your object, which is never initialized by anybody.

How could you obtain a reference to the ServletContext?

You're already doing it, in the CatalogoResource class:

@Context ServletContext context;

This tells JAX-RS that, after instantiating the class, it should inject the ServletContext. So, just pass this context to your DAO constructor or method:

 public Catalogo open(ServletContext context, int ano, int curso) throws URISyntaxException {
    try {           
        InputStream resourceAsStream = context.getResourceAsStream("/WEB-INF/catalogos/2012_36.xml"); 
        // ...