In Java, how to delete everything after .com or .net - java

I am trying to get just the domain name (http://www.example.com) out of log files that looks like this:
http://maps.google.com/maps?hl=en&tab=wl
http://l.macys.com/simi-valley-ca?cm_mmc=macys_
https://www.google.co.in/
https://www.google.ca/
I want just
http://maps.google.com/
http://l.macys.com/
https://www.google.co.in/
https://www.google.ca/
Any ideas?

How about
URL url = new URL("http://maps.google.com/maps?hl=en&tab=wl");
System.out.println(url.getProtocol()+"://"+url.getHost());
Output
http://maps.google.com

If you don't want to handle it yourself, then a full proof way is following:
URL url = new URL("http://l.macys.com/simi-valley-ca?cm_mmc=macys_");
System.out.println(url.getProtocol() + "://" + url.getHost() + ((url.getPort()==-1)?"" : ":" + url.getPort()) + "/" );
You can skip url.getPort if you are sure that there will never be a port type url!!
Cheers

Related

Several Filters in Cognito ListUsers

I'm trying to filter in my presignup lambda for a few params (in the same call) with Cognito ListUserRequest but it doesn't work.
Somebody knows a workaround without making several calls?
Thanks a lot!
ListUsersRequest listUsersRequest = new ListUsersRequest();
listUsersRequest.withUserPoolId(USER_POOL_ID);
listUsersRequest.withFilter("username = \"" + USER_NAME + "\" and email = \"" + EMAIL + "\""");

Twilio URL on different servers

how can i parse into url of my current application URI.create(String) when trying to call a number? I would like to parse on the test server http://localhost:8089 etc, but address on the production will be different.
I have tried to get the url
((ServletRequestAttributes) RequestContextHolder.
currentRequestAttributes()).getRequest();
String baseEnvLinkURL = "http://" + currentRequest.getLocalName();
if(currentRequest.getLocalPort() != 80) {
baseEnvLinkURL += ":" + currentRequest.getLocalPort();
}
if(!StringUtils.isEmpty(currentRequest.getContextPath())) {
baseEnvLinkURL += currentRequest.getContextPath();
}
but that returns http://0:0:0:0:...:8089/
Is it possible to somehow do it without getting it from different URL? Just parsing some kind of instance of something?
Thanks.

How to clear HttpAuthRequest credentials

I'm doing an app that logs users into websites that have an authentication popup using webView.
The problem comes when logging in and then going back to log in with different credentials, causing to skip HttpAuthRequest and therefore being logged in with the first credentials
What should I do?
The below lines of code is enough for clearing the cookies of Web View
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
CookieSyncManager.getInstance().sync();
Happy Coding! Thanks.
I did the trick logging in like this:
// Prefix http:// or https://
String prefix = url.substring(0, url.indexOf("/") + 2);
// Prefix + username +: + password + # + url without prefix
String finalUrl = prefix + username + ":" + password + "#" + url.substring(url.indexOf("/") + 2);
webView.loadUrl( finalUrl );
Instead of using onReceivedHttpAuthRequest

Java Mail Embed URL

When I'm adding an HTML URL into email body, it is not redirecting to the preferred location. This is the snippet, please tell me what am I doing wrong.
#location variable contains the URL
StringBuffer body = new StringBuffer("<html><body>Hi, <br/><br/>");
body.append("<p>"+cmts+"</p>");
#both the ways are not working, how to construct proper URL
body.append("<br/><br/>" + location + "<br/>");
body.append("<br/><br/>" +location + "<br/>");
#this is working as link only in OUTLOOK, but in other mail client it shows as plain text
body.append("<br/><br/>"+location);
URL:
http://host:port/weebApp/report/viewer.html#%2Fpublic%2FSamples%2FDashboards%2_FSample_report
It looks like a problem with the quotation marks. Try:
body.append("<br/><br/>" + location + "<br/>");
There could be many ways to add href in javamail for example:
1) InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-type", "text/html; charset=UTF-8");
String aHref = "some text\n" + text +
"\n<a href='http://google.com'>google.com</a>";
2) String aHref = "some text\n" + text +
"\n<a href='http://google.com'>google.com</a>";
messageBodyPart.setText(aHref,"UTF-8","html");
UPDATE:
Make sure the content-type is set to html or text/html because text/plain will display it as only text

How to get only part of URL from HttpServletRequest?

From the following URL I need to get (http://localhost:9090/dts) alone.
That is I need to remove (documents/savedoc) (OR)
need to get only - (http://localhost:9090/dts)
http://localhost:9090/dts/documents/savedoc
Is there any method available in request to get the above?
I tried the following and got the result. But still trying.
System.out.println("URL****************"+request.getRequestURL().toString());
System.out.println("URI****************"+request.getRequestURI().toString());
System.out.println("ContextPath****************"+request.getContextPath().toString());
URL****************http://localhost:9090/dts/documents/savedoc
URI****************/dts/documents/savedoc
ContextPath****************/dts
Can anyone please help me in fixing this?
You say you want to get exactly:
http://localhost:9090/dts
In your case, the above string consist of:
scheme: http
server host name: localhost
server port: 9090
context path: dts
(More info about the elements of a request path can be found in the official Oracle Java EE Tutorial: Getting Information from Requests)
##First variant:###
String scheme = request.getScheme();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String contextPath = request.getContextPath(); // includes leading forward slash
String resultPath = scheme + "://" + serverName + ":" + serverPort + contextPath;
System.out.println("Result path: " + resultPath);
##Second variant:##
String scheme = request.getScheme();
String host = request.getHeader("Host"); // includes server name and server port
String contextPath = request.getContextPath(); // includes leading forward slash
String resultPath = scheme + "://" + host + contextPath;
System.out.println("Result path: " + resultPath);
Both variants will give you what you wanted: http://localhost:9090/dts
Of course there are others variants, like others already wrote ...
It's just in your original question you asked about how to get http://localhost:9090/dts, i.e. you want your path to include scheme.
In case you still doesn't need a scheme, the quick way is:
String resultPath = request.getHeader("Host") + request.getContextPath();
And you'll get (in your case): localhost:9090/dts
AFAIK for this there is no API provided method, need to customization.
String serverName = request.getServerName();
int portNumber = request.getServerPort();
String contextPath = request.getContextPath();
// try this
System.out.println(serverName + ":" +portNumber + contextPath );
Just remove URI from URL and then append context path to it. No need to fiddle with loose schemes and ports which is only more tedious when you're dealing with default port 80 which don't need to appear in URL at all.
StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String ctx = request.getContextPath();
String base = url.substring(0, url.length() - uri.length() + ctx.length());
// ...
See also:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP (for the JSP/JSTL variant of composing the base URL)
In my understanding, you need the domain part and Context path only. Based on this understanding, You can use this method to get the required string.
String domain = request.getRequestURL().toString();
String cpath = request.getContextPath().toString();
String tString = domain.subString(0, domain.indexOf(cpath));
tString = tString + cpath;
For those who want to get, in their endpoint, the URL of the front page which targeted the endpoint. You can use this:
request.getHeader("referer")
Usually I have a method like this:
public String getAbsoluteContextPath() throws MalformedURLException {
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) context.getRequest();
URL url = new URL(request.getRequestURL().toString());
return url.getProtocol() + "://" + url.getAuthority() + context.getRequestContextPath();
}
This method will return what you want, with the port number only if it exists in the current request. In your case it will return: http://localhost:9090/dts

Categories