<commit_before>/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.api.core.websocket.impl;

import com.google.inject.Injector;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

/**
 * Allows inject Guice instances on WEB SOCKET endpoint creation.
 *
 * @author <NAME>
 */
public class GuiceInjectorEndpointConfigurator extends ServerEndpointConfig.Configurator {
  @Inject private static Injector injector;

  public <T> T getEndpointInstance(Class<T> endpointClass) {
    return injector.getInstance(endpointClass);
  }

  @Override
  public void modifyHandshake(
      ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
    HttpSession httpSession = (HttpSession) request.getHttpSession();
    Object sessionSubject = httpSession.getAttribute("che_subject");
    if (sessionSubject != null) {
      sec.getUserProperties().put("che_subject", sessionSubject);
    }
  }
}<commit_msg>Fix putting Che Subject into ServerEndpointConfig if http session is null (#10993)<commit_after>/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.api.core.websocket.impl;

import com.google.inject.Injector;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

/**
 * Allows inject Guice instances on WEB SOCKET endpoint creation.
 *
 * @author <NAME>
 */
public class GuiceInjectorEndpointConfigurator extends ServerEndpointConfig.Configurator {
  @Inject private static Injector injector;

  public <T> T getEndpointInstance(Class<T> endpointClass) {
    return injector.getInstance(endpointClass);
  }

  @Override
  public void modifyHandshake(
      ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
    HttpSession httpSession = (HttpSession) request.getHttpSession();
    if (httpSession != null) {
      Object sessionSubject = httpSession.getAttribute("che_subject");
      if (sessionSubject != null) {
        sec.getUserProperties().put("che_subject", sessionSubject);
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.09709744298548721, 'fraction_numerical': 0.011402902557014512, 'mean_word_length': 4.5248091603053435, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4160267', 'n_tokens_mistral': 836, 'n_tokens_neox': 811, 'n_words': 222}