Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8230463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:08:42+00:00 2026-06-07T17:08:42+00:00

I concocted the following to cache an Object to a class resource location. static

  • 0

I concocted the following to cache an Object to a class resource location.

static private <T> void toSerializedCache(Class<T> cls, T t, String cachecrlstr) {
  try {
    URL crl = cls.getResource(cachecrlstr);
    File crf = new File(crl.getFile());

    JAXBContext jaxbContext = JAXBContext.newInstance(cls);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(t, crf);
  }
  catch (Exception e) {
    System.out.printf("Failed to write %s to cache %s", t.getClass(), cachecrlstr);
  }
}

The problem is cachecrlstr is an initially non-existent file. The file has to be initially created.

Since it is initially non-existent, the class loader would return the url as null and the procedure fails.

I cannot use absolute path because this routine runs on a web service where we need to deduce the absolute path from the classloader.

To solve this problem, I rewrite the routine to

static private <T> void toSerializedCache(Class<T> cls, T t, String cachecrlstr) {
  try {
    File crf = new File(request.getSession().getServletContext().getRealPath("/WEB-INF/class"+cachecrlstr));

    JAXBContext jaxbContext = JAXBContext.newInstance(cls);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(t, file);
  }
  catch (JAXBException e) {
    System.out.printf("Failed to write %s to cache %s", t.getClass(), cachecrlstr);
  }
}

But I am unable to obtain the httpservletrequest object because this routine is inside a jax-rs service implementation. And I am unwilling to write a http listener (those registered in web.xml) to store the request into Threadlocal map. (meaning, don’t want to muck around maintaining Threadlocal objects).

However, (though unwilling to write to it) I am willing to withdraw objects from Threadlocal.

Does anyone know if RestEasy stores any http objects in Threadlocal that I could withdraw to deduce the session context or request?

More important question is – what do you suggest that I do to write objects to a file, where the file path is relative to WEB-INF/class, under the constraints I stated above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:08:45+00:00Added an answer on June 7, 2026 at 5:08 pm

    You can retrieve the HttpServletRequest from ResteasyProviderFactory:

    HttpServletRequest request = (HttpServletRequest) ResteasyProviderFactory.getContextDataMap().get(HttpServletRequest.class);
    

    Or if you are inside a JAX-RS service or interceptor, RESTEasy can inject HttpServletRequest for you.

    @javax.ws.rs.core.Context
    HttpServletRequest request;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the following code is server code in my app: private int serverPort; private Thread
I have created the following test server using java: import java.io.*; import java.net.*; class
I have the following code: http://pastebin.com/U9qYSmET When I try to telnet into my server
I have the following code that gets initialized as a static variable in a
In following code ( fiddle ): <div class=connected> <div class=myDivs>Hello 01</div> <div class=myDivs>Hello 02</div>
Does anyone see a problem with the following Curl call / how the Oauth
wget is helpful in my data mining projects. Today I try to wget the
I need to remove leading zeros from a string field in an Access database
I'm using the following function to generate a list of users connected to a
I have the following scenario. A cube created in SSAS 2008. I can connected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.