Examples of SSEvent


Examples of ch.ralscha.extdirectspring.bean.SSEvent

  public void sse(@PathVariable("beanName") String beanName, @PathVariable("method") String method,
      HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {

    MethodInfo methodInfo = MethodInfoCache.INSTANCE.get(beanName, method);

    SSEvent result = null;

    if (methodInfo != null) {

      try {

        Object[] parameters = prepareParameters(request, response, locale, methodInfo);
        Object methodReturnValue = null;

        if (configuration.isSynchronizeOnSession() || methodInfo.isSynchronizeOnSession()) {
          HttpSession session = request.getSession(false);
          if (session != null) {
            Object mutex = WebUtils.getSessionMutex(session);
            synchronized (mutex) {
              methodReturnValue = ExtDirectSpringUtil.invoke(context, beanName, methodInfo, parameters);
            }
          } else {
            methodReturnValue = ExtDirectSpringUtil.invoke(context, beanName, methodInfo, parameters);
          }
        } else {
          methodReturnValue = ExtDirectSpringUtil.invoke(context, beanName, methodInfo, parameters);
        }

        if (methodReturnValue instanceof SSEvent) {
          result = (SSEvent) methodReturnValue;
        } else {
          result = new SSEvent();
          if (methodReturnValue != null) {
            result.setData(methodReturnValue.toString());
          }
        }

      } catch (Exception e) {
        log.error("Error polling method '" + beanName + "." + method + "'", e.getCause() != null ? e.getCause()
            : e);

        Throwable cause;
        if (e.getCause() != null) {
          cause = e.getCause();
        } else {
          cause = e;
        }

        result = new SSEvent();
        result.setEvent("error");
        result.setData(configuration.getMessage(cause));

        if (configuration.isSendStacktrace()) {
          result.setComment(ExtDirectSpringUtil.getStackTrace(cause));
        }
      }
    } else {
      log.error("Error invoking method '" + beanName + "." + method + "'. Method or Bean not found");

      result = new SSEvent();
      result.setEvent("error");
      result.setData(configuration.getDefaultExceptionMessage());

      if (configuration.isSendStacktrace()) {
        result.setComment("Bean or Method '" + beanName + "." + method + "' not found");
      }
    }

    response.setContentType(EVENT_STREAM.toString());
    response.setCharacterEncoding(EVENT_STREAM.getCharSet().name());

    StringBuilder sb = new StringBuilder(32);

    if (StringUtils.hasText(result.getComment())) {
      for (String line : result.getComment().split("\\r?\\n|\\r")) {
        sb.append(":").append(line).append("\n");
      }
    }

    if (StringUtils.hasText(result.getId())) {
      sb.append("id:").append(result.getId()).append("\n");
    }

    if (StringUtils.hasText(result.getEvent())) {
      sb.append("event:").append(result.getEvent()).append("\n");
    }

    if (StringUtils.hasText(result.getData())) {
      for (String line : result.getData().split("\\r?\\n|\\r")) {
        sb.append("data:").append(line).append("\n");
      }
    }

    if (result.getRetry() != null) {
      sb.append("retry:").append(result.getRetry()).append("\n");
    }

    sb.append("\n");
    FileCopyUtils.copy(sb.toString().getBytes(UTF8_CHARSET), response.getOutputStream());
  }
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

  public void sse(@PathVariable("beanName") String beanName, @PathVariable("method") String method,
      HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {

    MethodInfo methodInfo = MethodInfoCache.INSTANCE.get(beanName, method);

    SSEvent result = null;

    if (methodInfo != null) {

      try {

        Object[] parameters = prepareParameters(request, response, locale, methodInfo);
        Object methodReturnValue = null;

        if (configuration.isSynchronizeOnSession() || methodInfo.isSynchronizeOnSession()) {
          HttpSession session = request.getSession(false);
          if (session != null) {
            Object mutex = WebUtils.getSessionMutex(session);
            synchronized (mutex) {
              methodReturnValue = ExtDirectSpringUtil.invoke(context, beanName, methodInfo, parameters);
            }
          } else {
            methodReturnValue = ExtDirectSpringUtil.invoke(context, beanName, methodInfo, parameters);
          }
        } else {
          methodReturnValue = ExtDirectSpringUtil.invoke(context, beanName, methodInfo, parameters);
        }

        if (methodReturnValue instanceof SSEvent) {
          result = (SSEvent) methodReturnValue;
        } else {
          result = new SSEvent();
          if (methodReturnValue != null) {
            result.setData(methodReturnValue.toString());
          }
        }

      } catch (Exception e) {
        log.error("Error polling method '" + beanName + "." + method + "'", e.getCause() != null ? e.getCause()
            : e);

        Throwable cause;
        if (e.getCause() != null) {
          cause = e.getCause();
        } else {
          cause = e;
        }

        result = new SSEvent();
        result.setEvent("error");
        result.setData(configuration.getMessage(cause));

        if (configuration.isSendStacktrace()) {
          result.setComment(ExtDirectSpringUtil.getStackTrace(cause));
        }
      }
    } else {
      log.error("Error invoking method '" + beanName + "." + method + "'. Method or Bean not found");

      result = new SSEvent();
      result.setEvent("error");
      result.setData(configuration.getDefaultExceptionMessage());

      if (configuration.isSendStacktrace()) {
        result.setComment("Bean or Method '" + beanName + "." + method + "' not found");
      }
    }

    response.setContentType(EVENT_STREAM.toString());
    response.setCharacterEncoding(EVENT_STREAM.getCharSet().name());

    StringBuilder sb = new StringBuilder(32);

    if (StringUtils.hasText(result.getComment())) {
      for (String line : result.getComment().split("\\r?\\n|\\r")) {
        sb.append(":").append(line).append("\n");
      }
    }

    if (StringUtils.hasText(result.getId())) {
      sb.append("id:").append(result.getId()).append("\n");
    }

    if (StringUtils.hasText(result.getEvent())) {
      sb.append("event:").append(result.getEvent()).append("\n");
    }

    if (StringUtils.hasText(result.getData())) {
      for (String line : result.getData().split("\\r?\\n|\\r")) {
        sb.append("data:").append(line).append("\n");
      }
    }

    if (result.getRetry() != null) {
      sb.append("retry:").append(result.getRetry()).append("\n");
    }

    sb.append("\n");
    FileCopyUtils.copy(sb.toString().getBytes(UTF8_CHARSET), response.getOutputStream());
  }
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

  public void handle(String beanName, String method, HttpServletRequest request, HttpServletResponse response,
      Locale locale) throws Exception {

    MethodInfo methodInfo = MethodInfoCache.INSTANCE.get(beanName, method);

    SSEvent result = null;
    SSEWriter sseWriter = new SSEWriter(response);

    if (methodInfo != null) {
      try {

        Object[] parameters = configurationService.getParametersResolver().prepareParameters(request, response,
            locale, methodInfo, sseWriter);
        Object methodReturnValue;

        if (configurationService.getConfiguration().isSynchronizeOnSession()
            || methodInfo.isSynchronizeOnSession()) {
          HttpSession session = request.getSession(false);
          if (session != null) {
            Object mutex = WebUtils.getSessionMutex(session);
            synchronized (mutex) {
              methodReturnValue = ExtDirectSpringUtil.invoke(
                  configurationService.getApplicationContext(), beanName, methodInfo, parameters);
            }
          } else {
            methodReturnValue = ExtDirectSpringUtil.invoke(configurationService.getApplicationContext(),
                beanName, methodInfo, parameters);
          }
        } else {
          methodReturnValue = ExtDirectSpringUtil.invoke(configurationService.getApplicationContext(),
              beanName, methodInfo, parameters);
        }

        if (methodReturnValue instanceof SSEvent) {
          result = (SSEvent) methodReturnValue;
        } else if (methodReturnValue != null) {
          result = new SSEvent();
          result.setData(methodReturnValue.toString());
        }

      } catch (Exception e) {
        log.error("Error polling method '" + beanName + "." + method + "'", e.getCause() != null ? e.getCause()
            : e);

        Throwable cause;
        if (e.getCause() != null) {
          cause = e.getCause();
        } else {
          cause = e;
        }

        result = new SSEvent();
        result.setEvent("error");
        result.setData(configurationService.getConfiguration().getMessage(cause));

        if (configurationService.getConfiguration().isSendStacktrace()) {
          result.setComment(ExtDirectSpringUtil.getStackTrace(cause));
        }
      }
    } else {
      log.error("Error invoking method '" + beanName + "." + method + "'. Method or Bean not found");

      result = new SSEvent();
      result.setEvent("error");
      result.setData(configurationService.getConfiguration().getDefaultExceptionMessage());

      if (configurationService.getConfiguration().isSendStacktrace()) {
        result.setComment("Bean or Method '" + beanName + "." + method + "' not found");
      }
    }

    if (result != null) {
      sseWriter.write(result);
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

    cookies.add(new Cookie("last", "lastCookie"));

    List<SSEvent> events = ControllerUtil.performSseRequest(mockMvc, "sseProvider",
        "messageCookieValue5", null, headers, cookies);
    assertThat(events).hasSize(1);
    SSEvent event = events.get(0);

    assertThat(event.getEvent()).isEqualTo("messageCookieValue5");
    assertThat(event.getComment()).isEqualTo("comment of message null");
    assertThat(event.getData()).isEqualTo(
        "aRequestHeader;null;default1;default2;lastCookie");
    assertThat(event.getId()).isEqualTo("122");
    assertThat(event.getRetry()).isNull();
  }
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

    cookies.add(new Cookie("cookie2", "2ndCookie"));

    List<SSEvent> events = ControllerUtil.performSseRequest(mockMvc, "sseProvider",
        "messageCookieValue5", params, headers, cookies);
    assertThat(events).hasSize(1);
    SSEvent event = events.get(0);

    assertThat(event.getEvent()).isEqualTo("messageCookieValue5");
    assertThat(event.getComment()).isEqualTo("comment of message 33");
    assertThat(event.getData()).isEqualTo(
        "aRequestHeader;33;default1;2ndCookie;lastCookie");
    assertThat(event.getId()).isEqualTo("122");
    assertThat(event.getRetry()).isNull();
  }
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

    cookies.add(new Cookie("cookie2", "2nd"));

    List<SSEvent> events = ControllerUtil.performSseRequest(mockMvc, "sseProvider",
        "messageCookieValue5", params, headers, cookies);
    assertThat(events).hasSize(1);
    SSEvent event = events.get(0);

    assertThat(event.getEvent()).isEqualTo("messageCookieValue5");
    assertThat(event.getComment()).isEqualTo("comment of message 44");
    assertThat(event.getData()).isEqualTo("aRequestHeader;44;1st;2nd;last");
    assertThat(event.getId()).isEqualTo("122");
    assertThat(event.getRetry()).isNull();
  }
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

    cookies.add(new Cookie("booleanCookie", "true"));

    List<SSEvent> events = ControllerUtil.performSseRequest(mockMvc, "sseProvider",
        "messageCookieValue6", null, null, cookies);
    assertThat(events).hasSize(1);
    SSEvent event = events.get(0);

    assertThat(event.getEvent()).isEqualTo("messageCookieValue6");
    assertThat(event.getComment()).isEqualTo("comment");
    assertThat(event.getData()).isEqualTo("theHeader;2;true");
    assertThat(event.getId()).isEqualTo("123");
    assertThat(event.getRetry()).isEqualTo(10000);
  }
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

    assertThat(locale).isEqualTo(Locale.ENGLISH);

    Date now = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd 'at' hh:mm:ss");

    SSEvent event = new SSEvent();
    event.setRetry(200000);
    event.setData("Successfully polled at: " + formatter.format(now));
    return event;
  }
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

  @Test
  public void sseBeanDoesNotExists() throws Exception {
    List<SSEvent> events = ControllerUtil.performSseRequest(mockMvc, "sseProviderXY",
        "message1", null, null, null);
    assertThat(events).hasSize(1);
    SSEvent event = events.get(0);

    assertThat(event.getEvent()).isEqualTo("error");
    assertThat(event.getComment()).isNull();
    assertThat(event.getData()).isEqualTo("Server Error");
    assertThat(event.getId()).isNull();
    assertThat(event.getRetry()).isNull();
  }
View Full Code Here

Examples of ch.ralscha.extdirectspring.bean.SSEvent

    return "Successfully polled at: " + formatter.format(now);
  }

  @ExtDirectMethod(value = ExtDirectMethodType.SSE, group = "group5")
  public String message13(SSEWriter writer) throws IOException {
    SSEvent event = new SSEvent();
    event.setComment("first comment");
    event.setEvent("event");
    event.setId("1");
    event.setRetry(1000);
    event.setData("one");

    writer.write(event);

    event = new SSEvent();
    event.setComment("second comment");
    event.setEvent("event");
    event.setId("2");
    event.setRetry(1000);
    event.setData("two");

    writer.write(event);
    writer.write((String) null);
    writer.write("third");
    writer.write((String) null);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.