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

  • SEARCH
  • Home
  • 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 9161549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:59:27+00:00 2026-06-17T13:59:27+00:00

I’m new to web services in general and have been doing a lot of

  • 0

I’m new to web services in general and have been doing a lot of research on it, but I’m currently unable to capture request headers I need for authentication purposes.

Here’s the scenario: I have an Android client calling a login() method on my JAX-RS RESTful web service, after adding a username/password combination to the request headers. The web service (which runs on JBoss AS 7.1) should then capture those headers.

The problem is everytime this login() method is called, the request seems to have not being serviced yet, thus leading to an IllegalStateException upon executing WebServiceContext.getMessageContext().

I’ve tried adding @PostConstruct annotation to the method’s header, but to no avail. When I do this the method doesn’t seem to be initialized at all. Instead I have a ClassNotFoundException upon calling it.

How can I get through this issue? I’ve been stuck on this for days and tried multiple different approaches both at the client and the web service to capture those headers, but they would either not apply to the project’s architecture or simply not work as expected.

Here’s the service’s interface:

@ApplicationPath("/apppath")
@Path("/wspath")
public interface LoginService {

    @GET
    @Path("/login")
    @Produces(MediaType.APPLICATION_JSON)
    public String login();
}

Here’s the service’s implementation:

@Stateless
@Local(LoginService.class)
public class LoginServiceImpl implements LoginService {

    @Resource
    WebServiceContext wsContext;

    @Override
    public String login() {

        // This line throws an IllegalStateException.
        MessageContext msgContext = wsContext.getMessageContext();

        // TODO: Capture authentication data from headers.   
        @SuppressWarnings("unchecked")
        Map<String, List<String>> headers = (Map<String, List<String>>) msgContext.get(MessageContext.HTTP_REQUEST_HEADERS);

        // Dummy return for testing purposes.
        return "It works" + headers.toString();
    }
}

And here’s a simple JUnit test code I’m working on to try to fix the problem on client side.

public class LoginServiceTest {

    @Test
    public void test() {

        String mEmail = "a@a.com";
        String mPassword = "aaaa";

        HttpGet request = new HttpGet("http://localhost:8080/apppath/wspath/login"); 

        String auth = Base64.encodeBytes((mEmail + ":" + mPassword).getBytes());
        request.addHeader("Authorization", "Basic " + auth);

        HttpClient httpClient = new DefaultHttpClient();

        try {
            InputStream inputStream = httpClient.execute(request).getEntity().getContent();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));             
            String wsReturn;

            while ((wsReturn = bufferedReader.readLine()) != null) {
                System.out.println(wsReturn);
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 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-17T13:59:28+00:00Added an answer on June 17, 2026 at 1:59 pm

    That’s not the right way to access service context in a JAX-RS servive, the WebServiceContext class is injected into JAX-WS web services. In a JAX-RS service class you can use the @Context annotation to have the HttpServletRequest request object injected and ask it for the headers.
    For example:

    @GET
    @Path("/login")
    @Produces(MediaType.APPLICATION_JSON)
    public String login(@Context HttpServletRequest request);
    

    You can look at this link to get more information about JAX-RS or go through the Java EE 6 Tutorial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been unable to fix a problem with Java Unicode and encoding. The
I have a jquery bug and I've been looking for hours now, I can't
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a view passing on information from a database: def serve_article(request, id): served_article
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.