private void clearSession() {
   if (admin != null) {
     R66Session lsession = sessions.remove(admin.value());
     admin = null;
     if (lsession != null) {
       lsession.setStatus(75);
       lsession.clear();
     }
   }
 }
 private void checkSession(Channel channel) {
   String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
   if (cookieString != null) {
     Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
     if (!cookies.isEmpty()) {
       for (Cookie elt : cookies) {
         if (elt.name().equalsIgnoreCase(R66SESSION + Configuration.configuration.getHOST_ID())) {
           logger.debug("Found session: " + elt);
           admin = elt;
           R66Session session = sessions.get(admin.value());
           if (session != null) {
             authentHttp = session;
             authentHttp.setStatus(73);
           } else {
             admin = null;
             continue;
           }
         } else if (elt.name().equalsIgnoreCase(I18NEXT)) {
           logger.debug("Found i18next: " + elt);
           lang = elt.value();
         }
       }
     }
   }
   if (admin == null) {
     logger.debug("NoSession: " + uriRequest + ":{}", admin);
   }
 }
 private String readFileHeader(String filename) {
   String value;
   try {
     value = WaarpStringUtils.readFileException(filename);
   } catch (InvalidArgumentException e) {
     logger.error("Error while trying to open: " + filename, e);
     return "";
   } catch (FileTransferException e) {
     logger.error("Error while trying to read: " + filename, e);
     return "";
   }
   StringBuilder builder = new StringBuilder(value);
   WaarpStringUtils.replace(
       builder,
       REPLACEMENT.XXXLOCALXXX.toString(),
       Integer.toString(Configuration.configuration.getLocalTransaction().getNumberLocalChannel())
           + " "
           + Thread.activeCount());
   WaarpStringUtils.replace(
       builder,
       REPLACEMENT.XXXNETWORKXXX.toString(),
       Integer.toString(
           Configuration.configuration.getLocalTransaction().getNumberLocalChannel()));
   WaarpStringUtils.replace(
       builder, REPLACEMENT.XXXHOSTIDXXX.toString(), Configuration.configuration.getHOST_ID());
   if (authentHttp.isAuthenticated()) {
     WaarpStringUtils.replace(
         builder,
         REPLACEMENT.XXXADMINXXX.toString(),
         Messages.getString("HttpSslHandler.1")); // $NON-NLS-1$
   } else {
     WaarpStringUtils.replace(
         builder,
         REPLACEMENT.XXXADMINXXX.toString(),
         Messages.getString("HttpSslHandler.0")); // $NON-NLS-1$
   }
   TrafficCounter trafficCounter =
       Configuration.configuration.getGlobalTrafficShapingHandler().trafficCounter();
   WaarpStringUtils.replace(
       builder,
       REPLACEMENT.XXXBANDWIDTHXXX.toString(),
       Messages.getString("HttpSslHandler.IN")
           + (trafficCounter.lastReadThroughput() >> 20)
           + //$NON-NLS-1$
           Messages.getString("HttpSslHandler.MOPS")
           + //$NON-NLS-1$
           Messages.getString("HttpSslHandler.OUT")
           + //$NON-NLS-1$
           (trafficCounter.lastWriteThroughput() >> 20)
           + Messages.getString("HttpSslHandler.MOPS")); // $NON-NLS-1$
   WaarpStringUtils.replace(builder, REPLACEMENT.XXXLANGXXX.toString(), lang);
   return builder.toString();
 }
 @Override
 protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
   FullHttpRequest request = this.request = msg;
   QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
   uriRequest = queryStringDecoder.path();
   logger.debug("Msg: " + uriRequest);
   if (uriRequest.contains("gre/")
       || uriRequest.contains("img/")
       || uriRequest.contains("res/")
       || uriRequest.contains("favicon.ico")) {
     HttpWriteCacheEnable.writeFile(
         request,
         ctx,
         Configuration.configuration.getHttpBasePath() + uriRequest,
         R66SESSION + Configuration.configuration.getHOST_ID());
     ctx.flush();
     return;
   }
   checkSession(ctx.channel());
   if (!authentHttp.isAuthenticated()) {
     logger.debug("Not Authent: " + uriRequest + ":{}", authentHttp);
     checkAuthent(ctx);
     return;
   }
   String find = uriRequest;
   if (uriRequest.charAt(0) == '/') {
     find = uriRequest.substring(1);
   }
   find = find.substring(0, find.indexOf("."));
   REQUEST req = REQUEST.index;
   try {
     req = REQUEST.valueOf(find);
   } catch (IllegalArgumentException e1) {
     req = REQUEST.index;
     logger.debug("NotFound: " + find + ":" + uriRequest);
   }
   switch (req) {
     case index:
       responseContent.append(index());
       break;
     case Logon:
       responseContent.append(index());
       break;
     case System:
       responseContent.append(System());
       break;
     default:
       responseContent.append(index());
       break;
   }
   writeResponse(ctx);
 }
 private void checkAuthent(ChannelHandlerContext ctx) {
   newSession = true;
   if (request.method() == HttpMethod.GET) {
     String logon = Logon();
     logon = logon.replaceAll(REPLACEMENT.XXXERRORMESGXXX.toString(), "");
     responseContent.append(logon);
     clearSession();
     writeResponse(ctx);
     return;
   } else if (request.method() == HttpMethod.POST) {
     getParams();
     if (params == null) {
       String logon = Logon();
       logon =
           logon.replaceAll(
               REPLACEMENT.XXXERRORMESGXXX.toString(),
               Messages.getString("HttpSslHandler.EmptyLogin"));
       responseContent.append(logon);
       clearSession();
       writeResponse(ctx);
       return;
     }
   }
   boolean getMenu = false;
   if (params.containsKey("Logon")) {
     String name = null, password = null;
     List<String> values = null;
     if (!params.isEmpty()) {
       // get values
       if (params.containsKey("name")) {
         values = params.get("name");
         if (values != null) {
           name = values.get(0);
           if (name == null || name.isEmpty()) {
             getMenu = true;
           }
         }
       } else {
         getMenu = true;
       }
       // search the nb param
       if ((!getMenu) && params.containsKey("passwd")) {
         values = params.get("passwd");
         if (values != null) {
           password = values.get(0);
           if (password == null || password.isEmpty()) {
             getMenu = true;
           } else {
             getMenu = false;
           }
         } else {
           getMenu = true;
         }
       } else {
         getMenu = true;
       }
     } else {
       getMenu = true;
     }
     if (!getMenu) {
       logger.debug(
           "Name="
               + name
               + " vs "
               + name.equals(Configuration.configuration.getADMINNAME())
               + " Passwd vs "
               + Arrays.equals(
                   password.getBytes(WaarpStringUtils.UTF8),
                   Configuration.configuration.getSERVERADMINKEY()));
       if (name.equals(Configuration.configuration.getADMINNAME())
           && Arrays.equals(
               password.getBytes(WaarpStringUtils.UTF8),
               Configuration.configuration.getSERVERADMINKEY())) {
         authentHttp
             .getAuth()
             .specialNoSessionAuth(true, Configuration.configuration.getHOST_ID());
         authentHttp.setStatus(70);
       } else {
         getMenu = true;
       }
       if (!authentHttp.isAuthenticated()) {
         authentHttp.setStatus(71);
         logger.debug("Still not authenticated: {}", authentHttp);
         getMenu = true;
       }
     }
   } else {
     getMenu = true;
   }
   if (getMenu) {
     String logon = Logon();
     logon =
         logon.replaceAll(
             REPLACEMENT.XXXERRORMESGXXX.toString(),
             Messages.getString("HttpSslHandler.BadLogin"));
     responseContent.append(logon);
     clearSession();
     writeResponse(ctx);
   } else {
     String index = index();
     responseContent.append(index);
     clearSession();
     admin =
         new DefaultCookie(
             R66SESSION + Configuration.configuration.getHOST_ID(),
             Configuration.configuration.getHOST_ID() + Long.toHexString(random.nextLong()));
     sessions.put(admin.value(), this.authentHttp);
     authentHttp.setStatus(72);
     logger.debug("CreateSession: " + uriRequest + ":{}", admin);
     writeResponse(ctx);
   }
 }