# XFire Webservice + log4j



## y0dA (4. Juni 2007)

Hi!
Kann man bei einem Webservices log4j benutzen? Wenn ja bringe ich es nicht zusammen dass es läuft.


```
package at.pcd.wam.technologie.geo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.jws.WebMethod;
import javax.jws.WebService;

import org.apache.log4j.Logger;

import at.pcd.wam.technologie.model.AddressModel;
import at.pcd.wam.technologie.utilitiy.xml.XMLStringParserSAX;
import at.pcd.wam.technologie.utility.service.ServiceHelper;

@WebService
public class GeocodeServiceImpl implements IGeocodeService {
	
	//------------------------------------------------------------------------------------------------------------------
    // FIELDS
    //------------------------------------------------------------------------------------------------------------------
	
	/** google key constant for http://localhost:8080/ */
	private static final String GOOGLE_KEY = 
		"ABQIAAAAU5IDceLqYFSp4k84FYLxyRTwM0brOpm-All5BF6PoaKBxRWWERSDiAIGyx-hbmMfA3v7tHF4Tvwfaw";
	
	/** first component of the google url constant */
	private static final String FIRST_COMPONENT_URL = "http://maps.google.com/maps/geo?q=";
	
	/** last component of the google url constant */
	private static final String LAST_COMPONENT_URL = "&output=xml&key=";
	
	/** accuracy key constant */
	private static final String ACCURACY_KEY = "accuracy";
	
	/** not available constant */
	private static final String NOT_AVAILABLE = "not available";
	
    /** logger */
	private static final Logger LOG = Logger.getLogger(GeocodeServiceImpl.class.getName());
	
	//------------------------------------------------------------------------------------------------------------------
    // CONSTRUCTOR
    //------------------------------------------------------------------------------------------------------------------
	
	/**
	 * empty constructor
	 */
	public GeocodeServiceImpl() {
		// nothing to do
	}
	
	//------------------------------------------------------------------------------------------------------------------
    // HELPER METHODS
    //------------------------------------------------------------------------------------------------------------------
	
	/**
	 * validate google service response
	 * @param response google service response
	 * @return complex model
	 */
	private AddressModel validate(final String response) {
		/* format service response */
		AddressModel aModel = this.formatServiceResponseModel(response);
		
		if (aModel.getStatusCode().equals("200")) {
			/* valid service response */
			/* overwork house number */
			String houseNumber = ServiceHelper.decomposition4HouseNumber(aModel.getStreet());
			/* overwork street to set the right house number */
			String street = ServiceHelper.overworkStreet(aModel.getStreet());
			/* split coordinates */
			String[] s = ServiceHelper.splitCoordinates(aModel.getCoordinates());
			/* lookup for accurancy */
			Integer geo = ServiceHelper.lookupForAccuracyInteger(response);
			
			/* return model with coordinates */
			return new AddressModel(aModel, s, houseNumber, street, geo);
		} else  {
			/* return model wich only contains status code - reason: service response is invalid*/
			return new AddressModel(aModel.getStatusCode());
		}
	}
	
	/**
	 * geocoding depends on given parameter
	 * 
	 * @param sQuery search address
	 * @return complex data structur <code>AddressModel</code>
	 */
	@WebMethod
	public AddressModel getGeocodeAddressComplex(final AddressModel sQuery) {	
		/* 
		 * overwork parameter to set correct street (without house number) and set house number
		 * then use google geocode service
		 */
		String response = this.doGoogleGeocode(ServiceHelper.overworkParameterModel(sQuery));	
		
		return this.validate(response);
	}
	
	/**
	 * geocoding the given query
	 * 
	 * @param sQuery search query 
	 * @return complex data structur <code>AddressModel</code>
	 */
	@WebMethod
	public AddressModel getGeocodeQueryAddressComplex(final String sQuery) {
		String response = this.doGoogleGeocode(sQuery);
		
		return this.validate(response);
	}
	
	/**
	 * geocoding depends on given parameter 
	 * 
	 * @param sQuery search address
	 * @return rQuery response query from google geocoding
	 */
	 @WebMethod
	public String getGeocodeAddress(final AddressModel sQuery) {
		/* 
		 * overwork parameter to set correct street (without house number) and set house number
		 * then use google geocode service
		 */
		 return this.doGoogleGeocode(ServiceHelper.overworkParameterModel(sQuery));	
	}

	/**
	 * geocoding the given query 
	 *  
	 * @param sQuery search query 
	 * @return rQuery response query from google geocoding
	 */
	 @WebMethod
	public String getGeocodeQuery(final String sQuery) {
		return this.doGoogleGeocode(sQuery);
	}
	
	/**
	 * execute google geocoding
	 * 
	 * @param sQuery search query - format: street[house number],zip,city,country
	 * @return response xml as string
	 */
	private String doGoogleGeocode(final String sQuery) {
		StringBuffer sb = new StringBuffer();

		try {
			/* need following format: street, [house number], zip, [locality], cc */
			URL url = new URL(GeocodeServiceImpl.FIRST_COMPONENT_URL + sQuery
					+ GeocodeServiceImpl.LAST_COMPONENT_URL
					+ GeocodeServiceImpl.GOOGLE_KEY);
			
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;
			
			while ((inputLine = in.readLine()) != null) {
				sb.append(inputLine);
			}
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();			
		}
		
		return sb.toString();
	}
	
	/**
	 * format service response
	 * 
	 * @param serviceResponse unformatted service response
	 * @return formatted response model
	 */
	public AddressModel formatServiceResponseModel(final String serviceResponse) {
		XMLStringParserSAX parser = new XMLStringParserSAX();
		HashMap<String, String> map = (HashMap<String, String>) parser.doParse(serviceResponse);
		return new AddressModel(map);
	}
	
	/**
	 * format the service response and set the accurancy depends on response
	 * 
	 * @param serviceResponse service response
	 * @return formatted service response as map
	 */
	@Deprecated
	public Map<String, String> formatServiceResponseMap(final String serviceResponse) {
		 XMLStringParserSAX parser = new XMLStringParserSAX();
		 HashMap<String, String> map = (HashMap<String, String>) parser.doParse(serviceResponse);
		 
		 /* add geocode accuracy to map */
		 String accuracy = ServiceHelper.searchForAccuracyString(serviceResponse);
		 if (accuracy != null) {
			 map.put(GeocodeServiceImpl.ACCURACY_KEY, accuracy);
		 } else {
			 map.put(GeocodeServiceImpl.ACCURACY_KEY, GeocodeServiceImpl.NOT_AVAILABLE);
		 }			 
		 return map;
	}
}
```


```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>

<!--  Konsolen Ausgabe -->
<appender name= "CONSOLE" class= "org.apache.log4j.ConsoleAppender" >
	<layout class= "org.apache.log4j.PatternLayout" >
		<param name= "ConversionPattern" value="%d{ABSOLUTE} - %p %c - %m%n"/>
	</layout>
</appender>

<!-- File Ausgabe -->
<appender name= "TESTLOGFILE" class= "org.apache.log4j.FileAppender" >
	<param name= "File" value="C:/Programme/Apache Software Foundation/Tomcat 5.0/logs/testLog4JLog.log" />
	<layout class= "org.apache.log4j.PatternLayout" >
		<param name= "ConversionPattern" value= "%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
	</layout>
</appender>

<root>
	<priority value="info"/>
	<appender-ref ref= "CONSOLE" />
	<appender-ref ref= "TESTLOGFILE" />
</root>

</log4j:configuration>
```


```
Exception in thread "main" org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Unexpected close tag </body>; expected </HR>.
 at [row,col {unknown-source}]: [30,187]
org.codehaus.xfire.fault.XFireFault: Unexpected close tag </body>; expected </HR>.
 at [row,col {unknown-source}]: [30,187]
	at org.codehaus.xfire.fault.XFireFault.createFault(XFireFault.java:89)
	at org.codehaus.xfire.client.Client.onReceive(Client.java:410)
	at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139)
	at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
	at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
	at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
	at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
	at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
	at org.codehaus.xfire.client.Client.invoke(Client.java:336)
	at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
	at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
	at $Proxy12.getGeocodeAddressComplex(Unknown Source)
	at at.pcd.wam.technologie.test.TestGeocodeService.main(TestGeocodeService.java:41)
Caused by: com.ctc.wstx.exc.WstxParsingException: Unexpected close tag </body>; expected </HR>.
 at [row,col {unknown-source}]: [30,187]
	at com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:605)
	at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:461)
	at com.ctc.wstx.sr.BasicStreamReader.reportWrongEndElem(BasicStreamReader.java:3256)
	at com.ctc.wstx.sr.BasicStreamReader.readEndElem(BasicStreamReader.java:3198)
	at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2830)
	at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1019)
	at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:44)
	at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
	at org.codehaus.xfire.client.Client.onReceive(Client.java:406)
	... 11 more
```

mfg


----------

