Java tutorial
/* * Copyright 2011 Marcus Geiger. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.antbear.jee.wicket; import org.apache.wicket.Component; import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.markup.html.IHeaderResponse; import org.apache.wicket.request.IRequestParameters; import org.apache.wicket.request.Request; import org.apache.wicket.request.cycle.RequestCycle; import org.apache.wicket.request.resource.PackageResourceReference; import org.apache.wicket.util.string.StringValue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Provides geographic location information via HTML5 Geolocation services. * * @author Marcus Geiger */ public abstract class GeolocationAjaxBehavior extends AbstractDefaultAjaxBehavior { private static final long serialVersionUID = 1L; private static Logger log = LoggerFactory.getLogger(GeolocationAjaxBehavior.class); protected abstract void onLocation(AjaxRequestTarget target, double latitude, double longitude); protected abstract void onError(AjaxRequestTarget target, String message); // TODO provide enum for kind of error @Override public void renderHead(Component component, IHeaderResponse response) { super.renderHead(component, response); // Create javascript function that calls wicket to provide geolocation information CharSequence url = getCallbackUrl(); response.renderJavaScript("function geolocationCallWicket(status, message, position) {" + "var args = '';" + "if (status) {" + "var lat = position.coords.latitude;" + "var lon = position.coords.longitude;" + "args = '&status=1&lat='+lat+'&lon='+lon;" + "} else {" + "args = '&status=0&msg=' + message;" + "}" + "var wcall = wicketAjaxGet('" + url + "' + args, function() {}, function() {});" + "}", null); // Include the geolocation javascript file response.renderJavaScriptReference(new PackageResourceReference(GeolocationAjaxBehavior.class, GeolocationAjaxBehavior.class.getSimpleName() + ".js")); // Initialize geolocation response.renderOnDomReadyJavaScript("initGeolocationAjaxBehavior()"); } @Override protected void respond(AjaxRequestTarget target) { log.debug("respond"); Request request = RequestCycle.get().getRequest(); IRequestParameters requestParameters = request.getRequestParameters(); StringValue svStatus = requestParameters.getParameterValue("status"); if (svStatus == null || svStatus.isNull() || svStatus.isEmpty()) throw new RuntimeException("Invariant violation: status is a required parameter"); Integer status = svStatus.toInteger(); if (status == 0) { StringValue svMessage = requestParameters.getParameterValue("msg"); if (svMessage == null || svMessage.isNull() || svMessage.isEmpty()) throw new RuntimeException("Invariant violation: message is a required parameter"); log.debug("Geolocation failed: " + svMessage.toString()); onError(target, svMessage.toString()); } else { StringValue svLatitude = requestParameters.getParameterValue("lat"); if (svLatitude == null || svLatitude.isNull() || svLatitude.isEmpty()) throw new RuntimeException("Invariant violation: message is a required parameter"); StringValue svLongitude = requestParameters.getParameterValue("lon"); if (svLongitude == null || svLongitude.isNull() || svLongitude.isEmpty()) throw new RuntimeException("Invariant violation: message is a required parameter"); log.debug("Geolocation received: lat " + svLatitude.toDouble() + ", lon " + svLongitude.toDouble()); onLocation(target, svLatitude.toDouble(), svLongitude.toDouble()); } } }