@Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    SettingService settingService =
        PortalContainer.getInstance().getComponentInstanceOfType(SettingService.class);
    SoftwareRegistrationService softwareRegistrationService =
        PortalContainer.getInstance().getComponentInstanceOfType(SoftwareRegistrationService.class);

    if (softwareRegistrationService.isSoftwareRegistered()) {
      response.sendRedirect("/");
      return;
    }
    String code = request.getParameter("code");
    if (StringUtils.isEmpty(code)) {
      try {
        getServletContext().getRequestDispatcher(SR_JSP_RESOURCE).forward(request, response);
      } catch (Exception se) {
        if (LOG.isErrorEnabled()) {
          LOG.error(se);
        }
        response.sendRedirect("/");
      }
      return;
    }

    SoftwareRegistration softwareRegistration =
        softwareRegistrationService.registrationPLF(code, getRegistrationURL(request));
    if (softwareRegistration.isPushInfo()) {
      settingService.set(
          Context.GLOBAL,
          Scope.GLOBAL,
          SoftwareRegistrationService.SOFTWARE_REGISTRATION_NODE,
          SettingValue.create("Software registered:" + "true"));
      softwareRegistrationService.checkSoftwareRegistration();
      getServletContext().setAttribute("status", "success");
    } else if (softwareRegistration.isNotReachable()) {
      request.getSession().setAttribute("notReachable", "true");
      getServletContext().getRequestDispatcher(SR_JSP_RESOURCE).forward(request, response);
      return;
    } else {
      getServletContext().setAttribute("status", "failed");
      request.getSession().setAttribute("notReachable", "true");
      response.sendRedirect("/");
      return;
    }
    getServletContext().getRequestDispatcher(SR_JSP_RESOURCE_SUCCESS).forward(request, response);
    return;
  }
  /** {@inheritDoc} */
  @Override
  public SoftwareRegistration registrationPLF(String code, String returnURL) {
    String url = softwareRegistrationHost + "/portal/accessToken";
    SoftwareRegistration softwareRegistration = new SoftwareRegistration();
    try {
      HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost(url);
      List<NameValuePair> urlParameters = new ArrayList<>();
      urlParameters.add(new BasicNameValuePair("grant_type", "authorization_code"));
      urlParameters.add(new BasicNameValuePair("code", code));
      urlParameters.add(new BasicNameValuePair("redirect_uri", returnURL));
      urlParameters.add(new BasicNameValuePair("client_id", "x6iCo6YWmw"));
      urlParameters.add(new BasicNameValuePair("client_secret", "3XNzbpuTSx5HqJsBSwgl"));

      post.setEntity(new UrlEncodedFormEntity(urlParameters));
      HttpResponse response = client.execute(post);
      BufferedReader rd =
          new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      StringBuffer result = new StringBuffer();
      String line = "";
      while ((line = rd.readLine()) != null) {
        result.append(line);
      }

      JSONObject responseData = new JSONObject(result.toString());
      if (response.getStatusLine().getStatusCode() == HTTPStatus.OK) {
        String accessToken = responseData.getString("access_token");
        softwareRegistration.setAccess_token(accessToken);
        boolean pushInfo = sendPlfInformation(accessToken);
        softwareRegistration.setPushInfo(pushInfo);
      } else {
        String errorCode = responseData.getString("error");
        softwareRegistration.setError_code(errorCode);
      }

      return softwareRegistration;
    } catch (Exception ex) {
      softwareRegistration.setNotReachable(true);
    }
    return softwareRegistration;
  }