2

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

PlayMa256
  • 6,603
  • 2
  • 34
  • 54
  • You may take a look http://stackoverflow.com/questions/4340653/file-path-to-resource-in-our-war-web-inf-folder – dsp_user Dec 31 '15 at 18:16
  • @dsp_user the IDE doesnt recognise the ServletContext class. – PlayMa256 Dec 31 '15 at 18:20
  • Use import javax.servlet.ServletContext; – dsp_user Dec 31 '15 at 18:29
  • @dsp_user i've imported it from servelt-api and i made an edit. Try to see look at it. – PlayMa256 Dec 31 '15 at 20:43
  • What exactly is wrong with that code? What kind of exception are you getting? – dsp_user Jan 01 '16 at 10:01
  • i'm not getting any exception, i'm not getting any result on my get request. Testing locally using a grizzly server and referencing the / of my project, i can get what i want. But in my server as .war and putting the ctx.getResourceAsStream("WEB-INF/folderiwant/fileiwant") i dont get anything as output. – PlayMa256 Jan 01 '16 at 14:14
  • Try this code String fullPath = context.getRealPath("/WEB-INF/catalogos"); String xmlFile = String.valueOf(ano)+"_"+String.valueOf(curso)+".xml"; File file = new File(fullPath, xmlFile); – dsp_user Jan 01 '16 at 14:53
  • What you have inside war file is not files. Don't try to use file IO to access resources inside a war file. Just use ServletContext.getResource[AsStream()]. And read its javadoc. The first sentence is: *The path must begin with a `/`*. – JB Nizet Jan 01 '16 at 14:54
  • still failing with both alternatives, JAXB still saying the same error as the first image. – PlayMa256 Jan 01 '16 at 15:54
  • We can't help if you don't post your code and the error you get. – JB Nizet Jan 01 '16 at 15:57
  • @JBNizet well, the errors are the ones on the images i've posted. I will post the link to the project on my dropbox. And the main codes are already on the question. – PlayMa256 Jan 01 '16 at 16:05
  • @JBNizet Code already in the main question. – PlayMa256 Jan 01 '16 at 16:06
  • You were told to NOT use file IO and to use ServletContext.getResourceAsStream() instead. Staring at the code won't change anything. Update the code as instructed, and post the updated, supposed to be fixed, version of the code. Post it in the question itself. If you still have an error, post the stack trace **as text** in the question. – JB Nizet Jan 01 '16 at 16:07
  • What's not clear in "post the error stack trace **as text** in the question". Does it mean the same thing as "post the error as a a link pointing to an image at imgur in a comment"? Images are not searchable. We can't copy and paste from an image. – JB Nizet Jan 01 '16 at 16:14
  • I hurried to read your comment, really sorry. http://pastebin.com/s7zBRvSd . It was too long for a comment. – PlayMa256 Jan 01 '16 at 16:19

1 Answers1

2

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"); 
        // ...
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255