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