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.ajax;
020
021import javax.servlet.ServletException;
022import javax.servlet.http.HttpServlet;
023
024import org.apache.commons.lang.StringUtils;
025
026import com.google.gson.Gson;
027
028/**
029 * Helpful utilities for the Ajax functions.
030 * 
031 * @since 2.10.2-svn12
032 */
033public class AjaxUtil extends HttpServlet {
034    private static final long serialVersionUID = 3170439306358345408L;
035    private static Gson gson = new Gson();
036
037    /**
038     * Uses Google Gson (https://code.google.com/p/google-gson/) to convert to JSON
039     * @param input the object to be converted to JSON
040     * @return the JSON string of the object
041     */
042    public static String toJson(Object input) {
043        String result = "";
044        if (input != null) {
045            result = gson.toJson(input);
046        }
047        return result;
048    }
049    
050    /**
051     * Given a requestUri path, find the next uri "fragment" after the "/lastPart/" one.
052     * E.g. given url "/test/abc/travel", and lastPart "abc", this will return "travel". Given lastPart "test" will return "abc".
053     * 
054     * This could be done better using a <a href="http://en.wikipedia.org/wiki/URL_Template">URITemplate</a>
055     * (as <a href="https://tools.ietf.org/html/rfc6570">RFC6570</a>) 
056     * 
057     * @param path the RequestURI to search usually done by calling request.getRequestUri().
058     * @param lastPart the previousPart of the path to search after.
059     * @return the next part of the path.
060     * @throws ServletException
061     */
062    public static String getNextPathPart(String path, String lastPart) throws ServletException {
063        String result = null;
064        if (StringUtils.isBlank(path)) {
065            return result;
066        }
067        if (!lastPart.endsWith("/")) {
068            lastPart += "/";
069        }
070        int index = path.indexOf(lastPart);
071        if (index<0) {
072            lastPart = lastPart.substring(0,lastPart.length()-1);
073            index = path.indexOf(lastPart);
074            if (index<0) {
075                throw new ServletException("Invalid path provided " + path + " does not contain '" + lastPart + "'");
076            }
077        }
078        path = path.substring(index+lastPart.length());
079        index = path.indexOf("/");
080        if (index == -1) {
081            index = path.indexOf("#");
082            if (index == -1) {
083                index = path.indexOf("?");
084            }
085        }
086        if (index == -1) {
087            result = path;
088        }
089        else {
090            result = path.substring(0,index);
091        }
092
093        return result;
094    }
095    
096}