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 * @param input the object to be converted to JSON 039 * @return the JSON string of the object 040 */ 041 public static String toJson(Object input) { 042 String result = ""; 043 if (input != null) { 044 result = gson.toJson(input); 045 } 046 return result; 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 060 */ 061 public static String getNextPathPart(String path, String lastPart) throws ServletException { 062 String result = null; 063 if (StringUtils.isBlank(path)) { 064 return result; 065 } 066 if (!lastPart.endsWith("/")) { 067 lastPart += "/"; 068 } 069 int index = path.indexOf(lastPart); 070 if (index<0) { 071 lastPart = lastPart.substring(0,lastPart.length()-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+lastPart.length()); 078 index = path.indexOf("/"); 079 if (index == -1) { 080 index = path.indexOf("#"); 081 if (index == -1) { 082 index = path.indexOf("?"); 083 } 084 } 085 if (index == -1) { 086 result = path; 087 } 088 else { 089 result = path.substring(0,index); 090 } 091 092 return result; 093 } 094 095}