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 java.io.IOException;
022import java.util.List;
023import java.util.Map;
024
025import javax.servlet.ServletException;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.apache.wiki.WikiContext;
030import org.apache.wiki.ajax.WikiAjaxServlet;
031import org.apache.wiki.api.exceptions.PluginException;
032import org.apache.wiki.api.plugin.WikiPlugin;
033
034/**
035 * @since 2.10.2-svn10
036 */
037public class SampleAjaxPlugin implements WikiPlugin, WikiAjaxServlet {
038    
039    private static final String SERVLET_MAPPING = "SampleAjaxPlugin";
040
041    @Override
042    public String execute(WikiContext context, Map<String, String> params) throws PluginException {
043        String id = Integer.toString(this.hashCode());
044        String html= "<div onclick='Wiki.ajaxHtmlCall(\"/"+SERVLET_MAPPING+"/ajaxAction\",[12,45],\"result"+id+"\",\"Loading...\")' style='color: blue; cursor: pointer'>Press Me</div>\n"+
045                        "<div id='result"+id+"'></div>";
046        return html;
047    }
048
049    @Override
050    public String getServletMapping() {
051        return SERVLET_MAPPING;
052    }
053    
054    @Override
055    public void service(HttpServletRequest request, HttpServletResponse response, String actionName, List<String> params) throws ServletException, IOException {
056        try {
057            Thread.sleep(5000); // Wait 5 seconds
058        } catch (Exception e) {}
059        response.getWriter().print("You called! actionName="+actionName+" params="+params);     
060    }
061
062
063
064}