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