001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.plugin; 020 021import org.apache.wiki.ajax.WikiAjaxServlet; 022import org.apache.wiki.api.core.Context; 023import org.apache.wiki.api.exceptions.PluginException; 024import org.apache.wiki.api.plugin.Plugin; 025 026import javax.servlet.ServletException; 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029import java.io.IOException; 030import java.util.List; 031import java.util.Map; 032 033/** 034 * @since 2.10.2-svn10 035 */ 036public class SampleAjaxPlugin implements Plugin, WikiAjaxServlet { 037 038 private static final String SERVLET_MAPPING = "SampleAjaxPlugin"; 039 040 @Override 041 public String execute( final Context context, final Map<String, String> params) throws PluginException { 042 final String id = Integer.toString(this.hashCode()); 043 return "<div onclick='Wiki.ajaxHtmlCall(\"/"+SERVLET_MAPPING+"/ajaxAction\",[12,45],\"result"+id+"\",\"Loading...\")' style='color: blue; cursor: pointer'>Press Me</div>\n"+ 044 "<div id='result"+id+"'></div>"; 045 } 046 047 @Override 048 public String getServletMapping() { 049 return SERVLET_MAPPING; 050 } 051 052 @Override 053 public void service( final HttpServletRequest request, final HttpServletResponse response, final String actionName, final List< String > params ) 054 throws ServletException, IOException { 055 try { 056 Thread.sleep( 5000 ); // Wait 5 seconds 057 } catch( final Exception e ) { 058 } 059 response.getWriter().print( "You called! actionName=" + actionName + " params=" + params ); 060 } 061 062}