Validating URLs that don't have http - java

I want to validate the website address user input in java side. I have used org.apache.commons.validator.routines.UrlValidator for this, but it needs URLs to start with http://, https://, or ftp://.
However, in my case i want to accept the address just starting with www., like www.stackoverflow.com. How can I do this?

Create an URI object and check if getScheme() is empty or null. If so, preprend "http://"

Related

JLDAP for Authentication

I'm working on an application that requires the use of LDAP for authentication. Every student at my University has a MyID. There is a wiki on one of the University's pages that states:
To form the full DN (distinguished name) of a MyID, use this format:
cn=MyID,ou=users,o=uga (where MyID is replaced by the user's MyID).
In LDAP, a NULL password or username is considered an anonymous bind
attempt (bind is the LDAP word for authentication) and will always
succeed. Your application should either filter out NULL password
strings or validate the successful bind attempt. To validate a bind
attempt, have your application attempt to read the attribute
ugaAuthCheck. The attribute should have the value of 'y' (the letter y
without the quotes).
I am using JLDAP to handle connecting to the LDAP server over SSL. I am able to search the directory once connected, but I'm at a loss as to:
To validate a bind attempt, have your application attempt to read the
attribute ugaAuthCheck.
I bind to the server with this JLDAP method:
lc.bind( ldapVersion, loginDN, password.getBytes("UTF8") );
How does one read an attribute off of a bind in JLDAP? I've been digging through the JLDAP source but I'm not really seeing what an attribute is....so perhaps its actually called something different?
There are JLDAP Samples provided by Novell.
You might want to look at the "CompareAttrs.java" or "Search.java" code for some ideas.
-jim

How to generate a navigable URL for a particular slotted place?

How do I generate the URL to navigate to a particular place in slotted? I know that to navigate the current user to a particular place, it would just be (assuming slottedController is defined and pointing to a valid SlottedController instance):
slottedController.goTo(new MyDesiredPlace(param1, param2));
However, my use case is to generate a URL to be inserted into an e-mail, and when the recipient receives the e-mail, they'll click the URL and should be navigated to the specified place. How would I generate such a URL in slotted?
SlottedController has two method for helping create Urls: createToken() and createUrl(). The createToken() will create a history token that appears after the #, which can be used with GWT Hyperlink. The createUrl() is the same as the token method, but prepends the current URI, which can be used with Anchor or external links like an email.
As a note, SlottedController can only be run on the client, so there is currently no way to create the URL on the Server side. The URL must be created on the client and passed back to the server.

Detect proxy in Java/JSP

I'm working on project using Java, in which IP address will be identity of the client/user. So I'm facing one problem: where user can spoof their host identity, that can lead to false identity of the user. So, anyone know, how to detect whether the host is using proxy or not?
InetAddress thisIp = InetAddress.getLocalHost();
I'm using above code to detect the host IP address.
You cannot 100% reliably check this, but to cover the most proxies, you could check the presence of the X-Forwarded-For request header.
if (request.getHeader("X-Forwarded-For") != null) {
// Client is likely using a proxy.
}
There is no standard for this. Each proxy may have its own specific set of additional/custom headers. You could log the retrieved request headers and examine which headers are been set by certain proxies and then alter the code accordingly. Again, you cannot reliably check this. Some proxies may even have no additional headers at all. You'd need to maintain a list of IP addresses of "well known" proxies so that you can check getRemoteAddr() against it.
Unrelated to the concrete problem, as you tagged this with jsp, I would only add that writing Java code inside a JSP is a poor practice. You'd normally do this in a normal Java class like a servlet or a filter.

parameter of passing cookie

Do a browser pass the cookie name and cookie value saperately or both in one string ? Do it also pass domain or not??? I want to know what happens in the case of a java browser.
Cookies are sent as HTTP header, which is a pair of strings.
What kind of java browser are you talking about?
If you want to make a HTTP request in java, I'd suggest you use HTTPClient.

Get domain name in url with JSTL?

I am trying to get the domain name from the url with JSTL. The 2 methods I know return the wrong information. I need exactly what is in the URL.
When I do:
${pageContext.request.remoteHost}
I get my server's IP.
When I do:
${pageContext.request.serverName}
I normally get the right domain name, but on a server we have on amazon its returning "server1" instead of the correct domain name, probably because of the way it handles multiple domains.
Anyone know how I can get the current domain name in the URL?
I may need to get the URL and then parse it. How would I do that?
You should be using ServletRequest#getLocalName() instead. It returns the real hostname of the server. The ServletRequest#getServerName() indeed returns the value as set in Host header.
${pageContext.request.localName}
That's all. The solutions suggested in other answers are plain clumsy and hacky.
By the way, the ServletRequest#getRemoteHost() doesn't return the server's hostname, but the client's one (or the IP when the hostname is not immediately resolveable). It's obviously the same as the server's one whenever you run both the webserver and webbrowser at physically the same machine. If you're interested in the server's IP, use ServletRequest#getLocalAddr(). The terms "local" and "remote" must be interpreted from server's perspective, not from the client's. It's after all the server there where all that Java code runs.
You can parse domain name from URL
OR
public static String getDomainName(String url)
{
URL u;
try {
u = new URL(url);
}
catch (Exception e) {
return "";
}
return u.getHost();
}
You can use HttpServletRequest.getRequestUrl() to:
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
this would return a String like Get domain name in url with JSTL?
It should then be trivial to parse this to find the string that comes after the scheme (http, https, etc) and before the requestURI.
${pageContext.request.contextPath}

Categories

Resources