Jersey - getting HOST IP and PORT programmatically - java

I have a jersey service up and running. On server start up, i want the ip address and the port on which this service is deployed. This has to be done programmatically. Any ideas ??
Thanks.
Regards,

Check this one:
How can I get a the host name (with port) that a servlet is at
Since Jersey is a Servlet implementation, it may be of help.
i want the ip address and the port on which this service is deployed
In fact the question is quite incorrect. Speaking about the IP addresses, your application may be deployed on some host with several network interfaces and therefore is available on several IPs. Which one is required for you then? Method getServerName returns the address, to which the request was made, as I remember.

Related

Change localhost to something like localhost.dev in dynamic web project

I am creating a dynamic web project in java. I want to deploy it in a application server. By default we have hostname as localhost:port/appname. I want to change it to localhost.com/myapp. How can i do that? Do i need to change something in my project or tomcat or hostfile. Please elaborate.
I fear there may be some confusion about how web servers work.
In your browser the URL http://localhost:8080/application_name will resolve internally to 127.0.0.1 (localhost) as this is a built in address. 127.0.0.1 is a loop back address pointing to the machine you're on. If you're hosting the server on the same machine as the you're trying to navigate to this is why the localhost address works.
If you were on another machine you would need to either directly input the IP address of the server or use a DNS record to point the IP address to a URL you own and control.
Browser are also programmed to default to port 80 for http and port 443 for https so if you don't want to have to provide a port have your server bind to port 80. This way you'll only need to navigate to http://localhost/application_name
When deploying the application to a server you want to be externally accessible you'll purchase a domain, register the DNS record and point it at servers external IP address (You may need to configure firewall and network rules).

How do I determine the hostname of a web server from an app server in Java/Apache

I'm running a Java application on Apache. I'm trying to find a way to allow my app server code to know the hostname of the web server. Is there a way to do this?
Running
InetAddress addr = InetAddress.getLocalHost();
String hostname = addr.getHostName();
or
(HttpServletResponse)response.getHeader("Host");
both predictably yield the app server's hostname.
Much depends on the configuration of your webservers, but I found that with Apache in front of JBoss, getRemoteHost returned the address of the Web Server.
Might be worth a go
I have handled this in a few ways:
Explicitly tell the app what host name it should be using via system properties
Have a rewrite rule in Apache that adds a header to the request, such as X-originally-For
Read the X-Forwarded-For header from the request
Note: I don't particularly like #3. Since the XFF header is passed host to host, you can't trust the addresses added by hosts outside your control.

My Server sees my router's IP instead of client's

I have a web-server that is behind a router. One of the servlets on that server relies on the IP address of the user, however, i often get the IP of my router (i.e. 192.168.1.1) instead of the real IP.
How can i fix that?
It sounds like your "router" is actually acting like a reverse proxy.
It is unlikely that you will be able to solve this in Java ... or even on your server machine. It sounds like a router configuration problem. You might be better off asking for help on "superuser" or "serverfault".
One thing to bear in mind is that identifying clients by IP address is always a bit dodgy. Even once you've sorted out the current problem, the real client IP address may not be available for various reasons:
The client's IP may be NATed.
The client may be talking to your webserver via a series of HTTP proxies.
The client may be spoofing its IP address.
I would suggest that access http://wwww.whatismyip.com to get you IP adddress in case you have not tried that way.You will more idea from this link.
Getting the 'external' IP address in Java
Grab your global IP from http://www.whatismyip.com/ , I think you might also need to configure port forwarding on router of user that is having servlet.

'Port Forward' a url

I am using a hosting service to have my minecraft server run.
I was wondering if it was possible to "forward a port" to a url. ie:
change 25561 to 25565 so that the minecraft client reads the url and the users don't have to put in the port of the website.
A record: play.mydomain.tk 94.23.16.94
I want it so that when users put in the url on the minecraft client(which, if not defined, automatically selects port 25565) don't have to put in the custom port.
Not sure if there is a way to do this.
I am aware Apache can use VHosts to host on the same server with the same port, but there must be some way to do this with other servers.
What you need is a SRV record to forward the minecraft 'service' to the right port on your hosting server. I'm doing this with mc.jgsw.me (which forwards to port 25569) and it took me flipping ages to work out.
If, as stated above, some other piece of software is already using port 25565 on your IP address, then it will be impossible to use that port on your Minecraft server without using a different IP address.
You could set up some sort of gateway that redirects traffic to server.example.com:25565 through to your server at x.x.x.x:25561 and vice versa. This could be in the form of an SRV entry on a DNS server. You would need to own the domain to do this, but some domains can be very cheap. More info here from an SRV provider.

Java: How do I get the IP of the local interface that can reach a remote IP?

I have a Java application that registers a server component in a service provider, and then sends the service name to a client. The client uses the service name to get an address out of the service provider to the server. However, the server has several interfaces only 1 of which the client get get at, so the service must be registered with the right IP. We discovered the client through a broadcast, so I have the client IP and an Enumeration of the computers network interfaces. How do I match the IP to an interface, not knowing what the netmask of the client IP is?
Spontaneously I imagine turning all the addresses to ints and chomping the local ips with their netmask and looking for a 'best match', but I wonder if there is a better way?
(this is an enterprise(tm) solution, so cutting out the service provider isn't an option, at least not w/o a political campaign first ;) )
By my understanding, the service provider has interfaces on multiple distinct networks. Each client is on only one of those networks and thus can only communicate with one of the interfaces. The service needs to discover which of the interfaces is accessible to the client so that it may send the correct address. You (the service developer) have no control over the network environment into which the service is deployed and thus cannot employ network-level solutions such as DNS.
In many network setups routing is symmetric. That is, the route from host A to host B is the same as that from host B to host A. If you know this to be the case in all environments to which your service will be deployed then you can safely assume that the address used to connect to the client is accessible to the client. That address could be discovered, for example, by connecting a DatagramSocket to the client and then calling its getLocalAddress() method.
If routing is asymmetric I know of no way to determine whether a particular interface is accessible to the client using only the java.net API. If you control the client implementation you might be able to have it include the broadcast source address (which should be the server interface visible to it) in its broadcast response packet. Beyond that, you haven't included enough information about the scenario to give specific advice. For example, it would be useful to know what protocol is used for the broadcast advertisement and client response thereto.
This depends on how you are going about this.
Sample code much?
There is a a ".getHostAddress()" method for InetAddress which might be useful.
This is usually done using DNS. The interface should be listed in DNS so you can reach it by name rather than address servicehost.example.com rather than 192.0.2.42.
It is more a network administration issue than the application.
Consider the server (hosting the java application) is multi-homed.
it can be three address:
1. 99.88.77.66 (Public IP)
2. 10.10.10.10 (Private, Internally assigned IP)
Which IP will your potential client can get access depends on the network configuration.
The client may access from a public IP, but it will access your private IP due to NAT.
Another example, may be your client IP is 192.168.10.10, through NAT, it access your 10.10.10.10 IP.
You cannot calculated the correct IP (to publish your service) by any kind of prefix matching.
You have mentioned that, it is an enterprise solution, and complex network configuration is common in such kind of environment.
With DNS, your server host may be myapp.company.com
It is is up to the DNS facility to resolve the correct IP for the client.
You may need to get coordinate with networking guy, with proper support in DNS,
the solution should be trivial and more robust.

Categories

Resources