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 *
036 * updated according to https://issues.apache.org/jira/browse/JSPWIKI-1195
037 */
038
039public class SampleAjaxPlugin implements Plugin, WikiAjaxServlet {
040    
041    private static final String SERVLET_MAPPING = "SampleAjaxPlugin";
042
043    @Override
044    public String execute( final Context context, final Map<String, String> params) throws PluginException {
045            var id = Long.toString(System.currentTimeMillis());
046
047        var url = "/" + SERVLET_MAPPING + "/ajaxAction";
048
049        var ajaxParams = params.get("params");
050
051        var js = String.format("$('result%s').value='Loading…';"+
052            "var token = Wiki.CsrfProtection;"+
053            "new Request.HTML({"+
054                "url: Wiki.JsonUrl + '%s', "+
055                "update:$('result%s'),"+
056                "onSuccess: function(data){console.log('Success',data);},"+
057                "onError: function(err){console.log('Error',err);}"+
058            "}).post('params=%s&X-XSRF-TOKEN='+token)", id, url, id, ajaxParams);
059
060        return String.format("<div onclick='%s' style='color: blue; cursor: pointer'>Press Me</div><div id='result%s'></div>", js, id);
061    }
062
063    @Override
064    public String getServletMapping() {
065        return SERVLET_MAPPING;
066    }
067
068    @Override
069    public void service( final HttpServletRequest request, final HttpServletResponse response, final String actionName, final List< String > params )
070            throws ServletException, IOException {
071        try {
072            Thread.sleep( 5000 ); // Wait 5 seconds
073        } catch( final Exception e ) {
074        }
075        response.getWriter().print( "You called! actionName=" + actionName + " params=" + params );
076    }
077
078}