0

I know how to design and implement URL shortening service but I want to know how can I design below

  • when I wrote go/google ; it takes me to google.com
  • When I wrote go/acc, it takes me to Accenture.com

Right side of / will be configured in URL shortner service but how can I design the left part so that when I wrote go/ in address bar, it hits by service to pick the actual URL mapped with google or acc and redirect

AngryJS
  • 955
  • 5
  • 23
  • 47
  • 4
    I would vote to close this question because there could be hundreds of ways to implement this; too many to enumerate here. Furthermore, a full answer to any one of them could require many pages or even a full book. Rather than asking for an approach, [edit] your question to focus on a more specific problem you encounter after you have chosen a method and attempted to implement it. – Stephen Ostermiller Oct 07 '22 at 14:21

1 Answers1

1

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.

jccampanero
  • 50,989
  • 3
  • 20
  • 49