public void unregister(HttpServletRequest request, HttpServletResponse response)
     throws Exception {
   String serverId = ServletRequestUtils.getStringParameter(request, "serverId");
   if (!StringUtils.isEmpty(serverId)) {
     redirectionDao.deleteRedirectionsByServerId(serverId);
   }
 }
  public void register(HttpServletRequest request, HttpServletResponse response) throws Exception {

    String redirectFrom =
        StringUtils.lowerCase(
            ServletRequestUtils.getRequiredStringParameter(request, "redirectFrom"));
    String licenseHolder =
        StringUtils.lowerCase(
            StringUtils.trimToNull(
                ServletRequestUtils.getStringParameter(request, "licenseHolder")));
    String serverId = ServletRequestUtils.getRequiredStringParameter(request, "serverId");
    String host = ServletRequestUtils.getStringParameter(request, "host", request.getRemoteAddr());
    int port = ServletRequestUtils.getRequiredIntParameter(request, "port");
    Integer localPort = ServletRequestUtils.getIntParameter(request, "localPort");
    String localIp = ServletRequestUtils.getStringParameter(request, "localIp");
    String contextPath = ServletRequestUtils.getRequiredStringParameter(request, "contextPath");
    boolean trial = ServletRequestUtils.getBooleanParameter(request, "trial", false);

    Date now = new Date();
    Date trialExpires = null;
    if (trial) {
      trialExpires =
          new Date(ServletRequestUtils.getRequiredLongParameter(request, "trialExpires"));
    } else if (licenseHolder == null) {
      sendError(response, "Invalid license.");
    }

    if (RESERVED_REDIRECTS.containsKey(redirectFrom)) {
      sendError(response, "\"" + redirectFrom + "\" is a reserved address. Please select another.");
      return;
    }

    if (!redirectFrom.matches("(\\w|\\-)+")) {
      sendError(
          response,
          "Illegal characters present in \"" + redirectFrom + "\". Please select another.");
      return;
    }

    URL url = new URL("http", host, port, "/" + contextPath);
    String redirectTo = url.toExternalForm();

    String localRedirectTo = null;
    if (localIp != null && localPort != null) {
      URL localUrl = new URL("http", localIp, localPort, "/" + contextPath);
      localRedirectTo = localUrl.toExternalForm();
    }

    Redirection redirection = redirectionDao.getRedirection(redirectFrom);
    if (redirection == null) {

      // Delete other redirects for same server ID.
      redirectionDao.deleteRedirectionsByServerId(serverId);

      redirection =
          new Redirection(
              0,
              licenseHolder,
              serverId,
              redirectFrom,
              redirectTo,
              localRedirectTo,
              trial,
              trialExpires,
              now,
              null,
              0);
      redirectionDao.createRedirection(redirection);
      LOG.info("Created " + redirection);

    } else {

      boolean sameServerId = serverId.equals(redirection.getServerId());
      boolean sameLicenseHolder =
          licenseHolder != null && licenseHolder.equalsIgnoreCase(redirection.getLicenseHolder());

      // Note: A licensed user can take over any expired trial domain.
      boolean existingTrialExpired =
          redirection.getTrialExpires() != null && redirection.getTrialExpires().before(now);

      if (sameServerId || sameLicenseHolder || (existingTrialExpired && !trial)) {
        redirection.setLicenseHolder(licenseHolder);
        redirection.setServerId(serverId);
        redirection.setRedirectFrom(redirectFrom);
        redirection.setRedirectTo(redirectTo);
        redirection.setLocalRedirectTo(localRedirectTo);
        redirection.setTrial(trial);
        redirection.setTrialExpires(trialExpires);
        redirection.setLastUpdated(now);
        redirectionDao.updateRedirection(redirection);
        LOG.info("Updated " + redirection);
      } else {
        sendError(
            response,
            "The web address \"" + redirectFrom + "\" is already in use. Please select another.");
      }
    }
  }