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        if( !lastPart.endsWith( "/" ) ) {
066            lastPart += "/";
067        }
068        final int lastPartLength =  lastPart.length();
069        int index = path.indexOf( lastPart );
070        if( index < 0 ) {
071            lastPart = lastPart.substring( 0, lastPartLength - 1 );
072            index = path.indexOf( lastPart );
073            if( index < 0 ) {
074                throw new ServletException( "Invalid path provided " + path + " does not contain '" + lastPart + "'" );
075            }
076        }
077        path = path.substring( index + lastPartLength );
078        index = path.indexOf( "/" );
079        if( index == -1 ) {
080            index = path.indexOf( "#" );
081            if( index == -1 ) {
082                index = path.indexOf( "?" );
083            }
084        }
085
086        final String result;
087        if( index == -1 ) {
088            result = path;
089        } else {
090            result = path.substring( 0, index );
091        }
092
093        return result;
094    }
095    
096}