0

I have a Rest service using Resteasy (running on top of Appengine), with the following psuedo code:

@Path("/service")
public class MyService {
  @GET
  @Path("/start")
  public Response startService() {
     // Need to read properties file here.
     // like: servletContext.getResourceAsStream("/WEB-INF/config.properties")
  }
}

However its obvious that the servlet context cannot be accessed here.

And code like:

 InputStream inputStream = this.getClass().getClassLoader()
                .getResourceAsStream("/WEB-INF/config.properties");  

Can't be executed within the Appengine environment.

EDIT:

I have tried doing it with Spring like:

appContext.xml

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="/WEB-INF/auth.properties"/>
</bean>

Then, put this on the actual class fields:

@Path("/service")
public MyService{
    @Autowired
    @Value("${myservice.userid}")
    private String username;
    @Autowired
    @Value("${myservice.passwd}")
    private String password;
 // Code omitted
}

However, part of the code of the MyService complains because the username and password was not "injected", I mean its empty although its on the auth.properties file

quarks
  • 33,478
  • 73
  • 290
  • 513

2 Answers2

2

This should work if you put the file in /WEB-INF/classes/ (which, importantly, is on the classpath), specifying config.properties as a file at the top-level.

this.getClass().getClassLoader().getResourceAsStream("/config.properties");

See this similar question: How to load properties file in Google App Engine?

Edit: Now you've edited, I'll respond & answer the Spring-related question. So, put the auth.properties into /WEB-INF/classes/ , and then specify classpath as follows.

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:auth.properties"/>
</bean>
Community
  • 1
  • 1
laher
  • 8,860
  • 3
  • 29
  • 39
  • I can't put the properties file in WEB-INF folder? – quarks Apr 30 '12 at 08:14
  • 1
    No, not with that method, because WEB-INF isn't on the classpath. That method, `getResourceAsStream(..)`, looks on the classpath. If you're stuck as to how to get it into your classpath, you could always put it in your source folder, and let `javac` send it there for you. – laher Apr 30 '12 at 08:25
  • Same problem, the username and password is not injected – quarks Apr 30 '12 at 09:25
  • hmm, a couple of wild guesses: do you have a in your Spring config? If you don't it might not be processing the annotations. Or, perhaps the properties processing fails before it gets to the correct point. This seems like a Spring problem - suggest you rephrase your question with more info, into a new question. The scope has changed a lot since my first answer! Good luck – laher Apr 30 '12 at 10:00
2

In RESTEasy you can easily inject Servlet context via @Context annotation: http://docs.jboss.org/resteasy/docs/2.3.1.GA/userguide/html_single/index.html#_Context

Examples can be found here: Rest easy and init params - how to access?

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154