Better Faster Lighter Java [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Better Faster Lighter Java [Electronic resources] - نسخه متنی

Justin Gehtland; Bruce A. Tate

نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
تنظیمات قلم


اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
لیست موضوعات
افزودن یادداشت
افزودن یادداشت جدید

9.9 The Web Service Interface

The web service interface is even simpler

to code than the console interface. The
web service can provide two different access points: one for
searching and one for indexing. The search method needs to return its
entire result-set in a serializable format so that it can be returned
across the wire via SOAP. The index method doesn't
have to return any value at all; it only needs to accept the starting
URL from the user to get the process rolling.

We must define this interface
in WSDL in order for it
to be available as a web service. WSDL (Web Services Description
Language) files simply define the access methods and data types
necessary for communicating with a given web service. WSDL files can
contain much more: since we have narrowed our requirements down so
much, we don't require most of those extra services.

The full WSDL file looks like this:

<definitions xmlns=""
<xs:complexType name="queryType">
<xs:element name="seachString" type="xs:string"/>
<xs:element name="threshold" type="xs:float"/>
<xs:complexType name="responseType">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="url" type="xs:anyURI"/>
<xs:element name="score" type="xs:float"/>
<xs:element name="query" type="tns:queryType"/>
<xs:element name="queryResponse" type="tns:responseType" maxOccurs="unbounded"/>
<xs:element name="index" type="xs:string"/>
<message name="queryRequest">
<part name="request" element="tns:query"/>
<message name="queryResponse">
<part name="response" element="tns:queryResponse"/>
<message name="doIndex">
<part name="request" element="tns:index"/>
<portType name="SiteSearch">
<operation name="searchContents">
<input message="tns:queryRequest" name="queryRequest"/>
<output message="tns:queryResponse" name="queryResponse"/>
<portType name="SiteSearch">
<operation name="doIndex">
<input message="tns:doIndex" name="doIndex"/>
<binding name="SiteSearchSoapHttp" type="tns:SiteSearch">
<soap:binding style="document" transport=""/>
<operation name="searchContents">
<soap:operation soapAction="searchContents"/>
<input name="queryRequest">
<soap:body use="literal"/>
<output name="queryResponse">
<soap:body use="literal"/>
<operation name="doIndex">
<soap:operation soapAction="doIndex"/>
<input name="doIndex">
<soap:body use="literal"/>
<service name="SiteSearchService">
<port name="SiteSearchSoap" binding="tns:SiteSearchSoapHttp">
<soap:address location="http://localhost:8080/axis/services/SiteSearchSoap"/>

The <types> section defines any datatypes
that need to be exchanged by clients and servers; the
<queryType> wraps the two inputs into a
search query (search term and threshold for limiting results based on
relative rank). <queryResponse> defines the
sequence of individual results of a search operation.

After the datatypes, the individual messages are defined. Messages
represent inputs to and outputs from individual web service
endpoints. Three are defined here:
<queryRequest> and
<queryResponse> are the input message and
output results of the search service, and
<doIndex> is the input message to a
return-less index service access point. After all these definitions,
map the individual messages and datatypes to the methods of the
implementation class. Note that the mapping of
doIndex includes an input type but no output

The implementation is even simpler; it only defines methods that
match the WSDL (one for searchContents and one for

public ResponseType[] searchContents(QueryType request) throws RemoteException {
try {
ConfigBean config = new ConfigBean( );
ServletContext context = getServletContext( );
if (context == null) {
throw new Error("null servlet context");
QueryBean query = new QueryBean(config.getCurIndexPath( ),
request.getSeachString( ));
query.execute( );
HitBean[] fullResults = query.getResults( );
ArrayList result = new ArrayList( );
for (int n=0; n<fullResults.length; n++) {
HitBean hit = fullResults[n];
if (hit.getScore( ) >= request.getThreshold( )) {
ResponseType rt = new ResponseType( );
rt.setScore(hit.getScore( ));
rt.setUrl(new URI(hit.getUrl( )));
return (ResponseType[]) result.toArray(new ResponseType[result.size( )]);
} catch (Exception e) {
getServletContext( ).log(e, "fail");
throw new AxisFault(e.getMessage( ));
public void doIndex(String indexUrl)
ConfigBean config = new ConfigBean( );
String nextIndex;
nextIndex = config.getNextIndexPath( );
catch(Exception ex)
IndexLinks lts = new IndexLinks(nextIndex,
config.getMaxLinks( ), config.getSkippedLinksFile( ));
lts.initFollowPrefixesFromSystemProperties( );
lts.initAvoidPrefixesFromSystemProperties( );
config.flipIndexPath( );
catch(Exception e)
//System.out.print(e.getStackTrace( ));

These methods are similar to the methods defined in the console
application, with minor differences in the types of exceptions
thrown, as well as the creation of a the return value for

9.9.1 Principles in Action

Keep it simple: ignore the greater part of the web services stack; if
you can't read it, don't automate
it (the WSDL for this service was written by hand)

Choose the right tools: Axis, JUnit, HTTPUnit

Do one thing, and do it well: just invoke search and return response

Strive for transparency: web service

the ultimate transparent layer to
end users

Allow for extension: none

/ 111