Java Reference
In-Depth Information
};
context.setAuthenticator(auth);
Endpoint endpoint = Endpoint.create(new TempVerter-
Impl());
endpoint.publish(context);
server.start();
}
}
The
main()
methodfirstcreatesan
HTTPServer
instancethatdescribesanHTTP
server connected to port 9901 of the local host. This method next creates the
/Tem-
pVerter
context, and returns the resulting
HttpContext
subclass object.
Continuing, the abstract
com.sun.net.httpserver.BasicAuthenticator
class is anonymously
subclassed to describe a server side implementation of HTTP basic authentication; its
boolean checkCredentials(String username, String password)
methodiscalledtoverifythegivennameandpasswordinthecontextofthebasicau-
thenticator's realm. Thismethodreturnstrueforvalidcredentials, andfalsewhenthey
are invalid.
After passing the
BasicAuthenticator
instance to
HttpContext
's
Authenticator setAuthenticator(Authenticator auth)
method,
Endpoint
's
Endpoint create(Object implementor)
method is called to
createan
Endpoint
instancewiththespecified
TempVerterImpl
instanceas
im-
plementor
's argument. This method's
void publish(Object serverCon-
text)
methodisthencalledwiththepreviouscontext,andthe
HttpServer
instance
is started.
If you were to run
TempVerterPublisher
and
TempVerterClient
, you
wouldobserve
98.6
followedby
100.0
ontwosuccessivelinesofoutput.However,
ifyoumodified
TempVerterClient
'scredentials,youwouldobserveathrownex-
ceptioninregardtonotbeingabletoaccesstheWSDLwhen
Service service =
Service.create(url, qname);
attemptstoexecute;theWSDLisnotaccess-
ible because authentication has failed.
Note
Learn more about JAX-WS and basic authentication by checking out Illya
Yalovyy's “HTTP basic authentication with JAX-WS (Client)” blog post at
ht-