Package com.storedobject.common
Class IPLocation
java.lang.Object
com.storedobject.common.IPLocation
The IPLocation class fetches location and network-related information of a given IP address
by querying the external API provided by `ipinfo.io`. If no IP address is provided, the class
defaults to retrieving information for the IP address of the local machine or network.
- Author:
- Syam
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an instance of the IPLocation class with the local machine's IP address.IPLocation(String ip) Constructs an instance of the IPLocation class with a specified IP address. -
Method Summary
Modifier and TypeMethodDescriptiongetCity()Retrieves the city associated with the current IP address.Retrieves the country associated with the current IP address.Retrieves the hostname associated with the current IPLocation instance.getIP()Retrieves the IP address associated with the current IPLocation instance.Retrieves the geographical location associated with the current or specified IP address.Retrieves the organization associated with the current IP address.Retrieves the region associated with the current IP address.toString()
-
Constructor Details
-
IPLocation
public IPLocation()Constructs an instance of the IPLocation class with the local machine's IP address. This constructor initializes the `IPLocation` object to fetch location and network-related information for the default IP address, which corresponds to the machine or network being used to execute the application. -
IPLocation
Constructs an instance of the IPLocation class with a specified IP address. This constructor initializes the `IPLocation` object to fetch location and network-related information for the given IP address. If the provided IP address is invalid or empty, it will default to retrieving information based on the local machine's IP address.- Parameters:
ip- the IP address for which location and network-related information will be fetched. If an empty string is provided, the local machine's IP address will be used.
-
-
Method Details
-
getLocation
Retrieves the geographical location associated with the current or specified IP address. The location is parsed from a comma-separated string containing latitude and longitude values. If the location cannot be determined, this method returns null.- Returns:
- a Geolocation object representing the latitude and longitude of the IP address, or null if the location is unavailable or cannot be parsed.
-
getIP
Retrieves the IP address associated with the current IPLocation instance. If an error occurs or the IP address is unavailable, this method returns null.- Returns:
- the IP address as a String, or null if the IP address is unavailable or an error occurs.
-
getHostName
Retrieves the hostname associated with the current IPLocation instance. If an error occurs or the hostname is unavailable, this method returns null.- Returns:
- the hostname as a String, or null if the hostname is unavailable or an error occurs.
-
getCity
Retrieves the city associated with the current IP address. If an error occurs or the city information is unavailable, this method returns null.- Returns:
- the city as a String, or null if the city information is unavailable or an error occurs.
-
getRegion
Retrieves the region associated with the current IP address. If an error occurs or the region information is unavailable, this method returns null.- Returns:
- the region as a String, or null if the region information is unavailable or an error occurs.
-
getCountry
Retrieves the country associated with the current IP address. If an error occurs or the country information is unavailable, this method returns null.- Returns:
- the country as a String, or null if the country information is unavailable or an error occurs.
-
getOrganization
Retrieves the organization associated with the current IP address. If an error occurs or the organization information is unavailable, this method returns null.- Returns:
- the organization as a String, or null if the organization information is unavailable or an error occurs.
-
toString
-