Reputation: 17326
I have the following possible values in a variable: URL, in JAVA/JSP.
URL="http://a.b.com/Something?IDToken1=AppUser1"
URL="http://a.b.com/Something?IDToken1=AppUser1&variable1=value1"
URL="http://a.b.com/Something?IDToken1=AppUser1variable1=value1&variable2=value2"
URL="http://a.b.com/Something?variable1=value1&IDToken1=AppUser1"
URL="http://a.b.com/Something?variable1=value1&IDToken1=AppUser1&variable2=value2"
I'm trying to find what code can I write (one-liner or 2-5 lines) using string or related functions that can give me a variable called: "user=AppUser1" and reConstructedURL=whatever value is the above link (except after removing IDToken1=AppUser1 from it if IDToken1 is the only parameter passed to the URL) otherwise, keep "?" character and all other parameters (+ exclude first & character as well i.e. no & character/value in reConstructedURL variable as the end character). I tried substring and index of functions but I couldn't get it to work.
For ex:
reConstructedURL="http://a.b.com/Something"
if URL was:
URL="http://a.b.com/Something?IDToken1=AppUser1"
reConstructedURL="http://a.b.com/Something?variable1=value1"
if URL was:
URL="http://a.b.com/Something?IDToken1=AppUser1&variable1=value1"
reConstructedURL="http://a.b.com/Something?variable1=value1&variable2=value2"
if URL was:
URL="http://a.b.com/Something?IDToken1=AppUser1&variable1=value1&variable2=value2"
reConstructedURL="http://a.b.com/Something?variable1=value1"
if URL was:
URL="http://a.b.com/Something?variable1=value1&IDToken1=AppUser1"
reConstructedURL="http://a.b.com/Something?variable1=value1&variable2=value2"
if URL was:
URL="http://a.b.com/Something?variable1=value1&IDToken1=AppUser1&variable2=value2"
THIS is what I have tried so far.
public final String USER_VAR = "IDToken1";
public String[] extractNewURL(String currentGoToURL)
{
String[] userAndRetURL = new String[2];
//Sample incoming
//http://a.b.com/Something?IDToken1=Apptester2
//http://a.b.com/Something?IDToken1=Apptester2&variable2=100
//http://a.b.com/Something?variable1=Giga&IDToken1=Apptester2&variable2=100
try
{
String extractedUsername = "";
String[] stringArray = currentGoToURL.split(USER_VAR+"=");
String usernamePlusPlus = stringArray[1];
//Go up to the &
int firstAmp = usernamePlusPlus.indexOf('&');
if(firstAmp != -1)
{
extractedUsername = usernamePlusPlus.substring(0, firstAmp);
}
else
{
extractedUsername = usernamePlusPlus;
}
userAndRetURL[0] = extractedUsername;
//2nd part to reconstruct the URL
if(!"".equals(extractedUsername))
{
//TODO this needs to be smarter to pass along extra params
String[] splitURL = currentGoToURL.split("\\?");
String beginningURL = splitURL[0];
//System.err.println("BEG:"+beginningURL);
userAndRetURL[1] = beginningURL;
}
else
{
userAndRetURL[1] = currentGoToURL;
}
} catch (Exception e) {
//Ends up in catalina.out
System.err.println("EXCEPTION OCCURRED IN LOGIN.JSP: "+e.toString());
e.printStackTrace();
}
return userAndRetURL;
}
%>
<%
boolean weHaveUser = false;
String userNameCarryFwd = "DummyUser";
//App_USERNAME info is present in Headers
userNameCarryFwd = response.getHeader("App_USERNAME");
if (userNameCarryFwd == null)
{
userNameCarryFwd = request.getParameter("App_USERNAME");
if(userNameCarryFwd == null)
{
userNameCarryFwd = request.getParameter("IDToken1");
if(userNameCarryFwd == null)
{
//This is crazy but we are passing it on the URL to get around header not fwding
String somethingToParse = request.getParameter("goto");
String[] uAndURL = extractNewURL(somethingToParse);
userNameCarryFwd = uAndURL[0];
String nonEncoded = uAndURL[1];
//This will be after reconstructive surgery
gotoURL = viewBean.getEncodedInputValue(nonEncoded);
if(userNameCarryFwd == null)
userNameCarryFwd = "";
}
}
}
weHaveUser = (userNameCarryFwd == null || userNameCarryFwd.length() < 1) ? false : true;
I think the best way to get it logically is: 1. From the current Go To URL string, if I can just strip out IDToken=< value > part out and make sure the resultant "redirected URL" string's end character is not ending with - either a ? or & i.e. remove ? or & if that's the last character and after stripping the above variable=value part, if ?& or && consecutive characters exists, then replace it with ? or & character, that would do it.
Upvotes: 0
Views: 236
Reputation: 26
below code can help you.
public class URLResonstruction {
public static String reconstructURL(String url) {
int beginTknInx = url.indexOf("IDToken1");
// If there is no IDToken
if (beginTknInx < 0)
return url;
// If IDToken is only query parameter or IDToken is at end
int endTknInx = url.substring(beginTknInx).indexOf("&");
if (endTknInx < 0)
return url.substring(0, beginTknInx - 1);
// If IDToken is at beginning or in middle
String part1 = url.substring(0, beginTknInx);
String part2 = url.substring(beginTknInx + endTknInx);
return part1 + part2.substring(1, part2.length());
}
}
Upvotes: 1