예제 #1
0
 public void redirect() throws IllegalStateException {
   String referer = getReferer();
   try {
     ResourceMethod method = router.parse(referer, HttpMethod.GET, request);
     executeMethod(method, result.use(logic()).redirectTo(method.getResource().getType()));
   } catch (ResourceNotFoundException e) {
     result.use(page()).redirect(referer);
   } catch (MethodNotAllowedException e) {
     result.use(page()).redirect(referer);
   }
 }
  @Override
  public boolean accepts(ResourceMethod method) {
    if (method.containsAnnotation(NivelAutenticacaoParaAcessar.class)) {
      AutenticacaoType liberadoPara =
          method.getMethod().getAnnotation(NivelAutenticacaoParaAcessar.class).value();
      if (liberadoPara == AutenticacaoType.TODOS) return false;
      if (usuarioWeb.isAutenticado() && liberadoPara.ehLiberadoParaO(usuarioWeb.getUsuario()))
        return false;
    } else if (usuarioWeb.isAutenticado()
        && usuarioWeb.getUsuario().getTipoAutenticacao() == AutenticacaoType.ADMIN) {
      return false;
    }

    return true;
  }
  public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance) {
    if (usuarioSession.getUsuario() != null) {
      Permission methodPermission = method.getMethod().getAnnotation(Permission.class);
      Permission controllerPermission =
          method.getResource().getType().getAnnotation(Permission.class);
      if (this.hasAccess(methodPermission) && this.hasAccess(controllerPermission)) {
        stack.next(method, resourceInstance);
      } else {
        result.redirectTo(LoginController.class).acessoNegado();
      }

    } else {
      result.redirectTo(LoginController.class).login();
    }
  }
예제 #4
0
  public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance)
      throws InterceptionException {
    Consumes consumesAnnotation = method.getMethod().getAnnotation(Consumes.class);
    List<String> supported = Arrays.asList(consumesAnnotation.value());

    String contentType = request.getContentType();
    if (!supported.isEmpty() && !supported.contains(contentType)) {
      unsupported(
          String.format(
              "Request with media type [%s]. Expecting one of %s.", contentType, supported));
      return;
    }

    try {
      Deserializer deserializer = deserializers.deserializerFor(contentType, container);
      if (deserializer == null) {
        unsupported(
            String.format("Unable to handle media type [%s]: no deserializer found.", contentType));
        return;
      }

      Object[] deserialized = deserializer.deserialize(request.getInputStream(), method);
      Object[] parameters = methodInfo.getParameters();

      for (int i = 0; i < deserialized.length; i++) {
        if (deserialized[i] != null) {
          parameters[i] = deserialized[i];
        }
      }

      stack.next(method, resourceInstance);
    } catch (IOException e) {
      throw new InterceptionException(e);
    }
  }
예제 #5
0
  @Override
  public Object[] deserialize(InputStream inputStream, ResourceMethod method) {
    Method jMethod = method.getMethod();
    Class<?>[] types = jMethod.getParameterTypes();
    if (types.length == 0) {
      throw new IllegalArgumentException(
          "Methods that consumes representations must receive just one argument");
    }

    Gson gson = getGson();
    Object[] params = new Object[types.length];
    String[] parameterNames = paramNameProvider.parameterNamesFor(jMethod);

    try {
      String content = getContentOfStream(inputStream);
      logger.debug("json retrieved: " + content);

      JsonParser parser = new JsonParser();
      JsonObject root = (JsonObject) parser.parse(content);

      for (int i = 0; i < types.length; i++) {
        String name = parameterNames[i];
        JsonElement node = root.get(name);
        if (node != null) {
          params[i] = gson.fromJson(node, types[i]);
        }
      }
    } catch (Exception e) {
      throw new ResultException("Unable to deserialize data", e);
    }

    return params;
  }
예제 #6
0
 private void executeMethod(ResourceMethod method, Object instance) {
   new Mirror()
       .on(instance)
       .invoke()
       .method(method.getMethod())
       .withArgs(
           provider.getParametersFor(method, new ArrayList<Message>(), localization.getBundle()));
 }
  /**
   * Melhoria aplicada ao método, caso o objeto (parâmetro) que foi informado não foi encontrado na
   * consulta do banco de dados, somente irá retornar 404 (Result.nothing()) se o usuário colocou o
   * parâmetro como <code>required=true</code>.
   *
   * @param stack
   * @param method
   * @param resourceInstance
   * @throws InterceptionException
   */
  public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance)
      throws InterceptionException {
    Annotation[][] annotations = method.getMethod().getParameterAnnotations();

    String[] names = provider.parameterNamesFor(method.getMethod());

    Class<?>[] types = method.getMethod().getParameterTypes();

    Object[] args = flash.consumeParameters(method);

    for (int i = 0; i < names.length; i++) {
      Iterable<LoadObject> loads = Iterables.filter(asList(annotations[i]), LoadObject.class);

      if (!isEmpty(loads)) {
        Object loaded = this.load(names[i], types[i]);

        if (loaded == null) {
          // Caso tenha a anotação LoadObject, o fluxo da aplicação será definido de acordo
          // com as definições feitas na anotação.
          LoadObject next = loads.iterator().next();

          if (next != null && next.required()) {
            if (next.redirectToWhenObjectNotFound() != null
                && !next.redirectToWhenObjectNotFound().isEmpty()) {
              logger.info(
                  "Entity not found, the page will be redirected to "
                      + next.redirectToWhenObjectNotFound());
              result.redirectTo(next.redirectToWhenObjectNotFound());
            } else {
              logger.info(
                  "Entity not found, the redirect URL was not specified, then i will be return nothing.");
              result.nothing();
              return;
            }
          }
        }

        if (args != null) args[i] = loaded;
        else request.setAttribute(names[i], loaded);
      }
    }
    flash.includeParameters(method, args);

    stack.next(method, resourceInstance);
  }
 @Override
 public boolean accepts(ResourceMethod method) {
   return !method.getResource().getType().isAnnotationPresent(Open.class)
       && !method.containsAnnotation(Open.class);
 }
 /**
  * Aceita todos os métodos que os seus parâmetros contenham uma anotação.
  *
  * @param method
  * @return
  */
 public boolean accepts(ResourceMethod method) {
   return any(asList(method.getMethod().getParameterAnnotations()), hasLoadAnnotation());
 }
예제 #10
0
 public boolean accepts(ResourceMethod method) {
   return method.containsAnnotation(Consumes.class);
 }
 @Override
 public boolean accepts(ResourceMethod method) {
   return !usuario.isLogado() && method.containsAnnotation(Restrito.class);
 }
 public boolean accepts(ResourceMethod method) {
   return !(method.getMethod().isAnnotationPresent(Public.class)
       || method.getResource().getType().isAnnotationPresent(Public.class));
   // return false;
 }