As @StephenOstermiller indicated in his comment, there could be hundreds of ways to implement the desired behavior.
In any way, assuming a simple approach, using the standard Java Servlet API, you could try something like the following.
- First, implement and register, either programmatically or using a
web.xml
file, an HttpServlet for processing the URL shortener requests. You can map you servlet to the '/go' servlet path.
- Analyze the URI you received using the different methods provided by
HttpServletRequest
, for example using getPathInfo
and getRequestURI
. The idea is being able to extract the fragment of the URI that identifies the actual service to which your request should be redirected to. The methods provided by the String
class could be of help in this step.
- Obtain the mapping between the fragment of the URI obtained as result in the previous step with the actual service, probably querying a database or any other mean. In a simple use case, an in-memory
Map
could do the trick.
- Once identified, issue a
302
HTTP redirect response with a Location
header corresponding to the actual service. If necessary, use the information obtained in the step 2
to append query parameters or any other information you consider appropriate to build the actual service URI. HttpServletResponse
provides the straightforward sendRedirect
method for performing this temporary redirection. If you prefer using a permanent redirection, i.e., by using a 301
HTTP status code, you need to explicitly provide this status code and the corresponding Location
header; please, see this related SO question.
For example:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GoServlet extends HttpServlet {
private static final Map<String, String> redirectionMap = new HashMap<>();
static {
redirectionMap.put("google", "https://www.google.com");
redirectionMap.put("acc", "https://www.accenture.com");
}
@Override
public void doGet(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
String redirectionKey = request.getPathInfo();
if (redirectionKey != null && redirectionKey.startsWith("/")) {
redirectionKey = redirectionKey.substring(1);
}
if (!redirectionMap.containsKey(redirectionKey)) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String redirectionUrl = redirectionMap.get(redirectionKey);
response.sendRedirect(redirectionUrl);
}
}
If, as it seems for your previous questions, you are using other libraries such as Spring and Spring MVC, you can define a simple controller for the same purpose. It could be defined similar to this:
@RequestMapping(path = "/go/{redirectKey}", method = RequestMethod.GET)
public void expandUrl(@PathVariable("redirectKey") final String redirectKey, HttpServletResponse response) throws IOException {
// where redirectionMap has been defined as above
if (!redirectionMap.containsKey(redirectionKey)) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String redirectionUrl = redirectionMap.get(redirectionKey);
response.sendRedirect(redirectionUrl);
}
Please, forgive me for the simple code, they are only basic examples of a simple redirection but they exemplify the tasks that need to be performed in order to implement the desired functionality.
I think this kind of redirection mechanism will not be implemented in this way, using an ad hoc application, but probably some kind of L7 network appliance or similar stuff.