예제 #1
0
  /**
   * Loads the specified entity parameter according to the method signature override.
   *
   * @param signatureOverride The signature override.
   * @return The resource entity parameter.
   */
  protected ResourceEntityParameter loadEntityParameter(ResourceMethodSignature signatureOverride) {
    try {
      Class<?> entityType = signatureOverride.input();
      if (entityType != ResourceMethodSignature.NONE.class) {
        AnnotationProcessorEnvironment env = net.sf.jelly.apt.Context.getCurrentEnvironment();
        TypeDeclaration type = env.getTypeDeclaration(entityType.getName());
        return new ResourceEntityParameter(type, env.getTypeUtils().getDeclaredType(type));
      }
    } catch (MirroredTypeException e) {
      DecoratedTypeMirror typeMirror =
          (DecoratedTypeMirror) TypeMirrorDecorator.decorate(e.getTypeMirror());
      if (typeMirror.isDeclared()) {
        if (typeMirror.isInstanceOf(ResourceMethodSignature.class.getName() + ".NONE")) {
          return null;
        } else {
          return new ResourceEntityParameter(
              ((DeclaredType) typeMirror).getDeclaration(), typeMirror);
        }
      } else {
        throw new ValidationException(
            getPosition(), "Illegal input type (must be a declared type): " + typeMirror);
      }
    }

    return null;
  }
예제 #2
0
  /**
   * Loads the explicit output payload.
   *
   * @param signatureOverride The method signature override.
   * @return The output payload (explicit in the signature override.
   */
  protected ResourceRepresentationMetadata loadOutputPayload(
      ResourceMethodSignature signatureOverride) {
    DecoratedTypeMirror returnType = (DecoratedTypeMirror) getReturnType();

    try {
      Class<?> outputType = signatureOverride.output();
      if (outputType != ResourceMethodSignature.NONE.class) {
        AnnotationProcessorEnvironment env = net.sf.jelly.apt.Context.getCurrentEnvironment();
        TypeDeclaration type = env.getTypeDeclaration(outputType.getName());
        return new ResourceRepresentationMetadata(
            env.getTypeUtils().getDeclaredType(type), returnType.getDocValue());
      }
    } catch (MirroredTypeException e) {
      DecoratedTypeMirror typeMirror =
          (DecoratedTypeMirror) TypeMirrorDecorator.decorate(e.getTypeMirror());
      if (typeMirror.isDeclared()) {
        if (typeMirror.isInstanceOf(ResourceMethodSignature.class.getName() + ".NONE")) {
          return null;
        }
        return new ResourceRepresentationMetadata(typeMirror, returnType.getDocValue());
      } else {
        throw new ValidationException(
            getPosition(), "Illegal output type (must be a declared type): " + typeMirror);
      }
    }

    return null;
  }
 private T getResultInstance(APIResult.Status status, String message) {
   try {
     Constructor<T> constructor = clazz.getConstructor(APIResult.Status.class, String.class);
     return constructor.newInstance(status, message);
   } catch (Exception e) {
     throw new FalconRuntimException("Unable to consolidate result.", e);
   }
 }
예제 #4
0
  private Map<String, String> makeDisplayData(
      final PwmApplication pwmApplication, final PwmSession pwmSession, final String bundleName) {
    Class displayClass = LocaleHelper.classForShortName(bundleName);
    displayClass = displayClass == null ? Display.class : displayClass;

    final Locale userLocale = pwmSession.getSessionStateBean().getLocale();
    final Configuration config = pwmApplication.getConfig();
    final TreeMap<String, String> displayStrings = new TreeMap<>();
    final ResourceBundle bundle = ResourceBundle.getBundle(displayClass.getName());
    try {
      final MacroMachine macroMachine =
          pwmSession.getSessionManager().getMacroMachine(pwmApplication);
      for (final String key : new TreeSet<>(Collections.list(bundle.getKeys()))) {
        String displayValue =
            LocaleHelper.getLocalizedMessage(userLocale, key, config, displayClass);
        displayValue = macroMachine.expandMacros(displayValue);
        displayStrings.put(key, displayValue);
      }
    } catch (Exception e) {
      LOGGER.error(pwmSession, "error expanding macro display value: " + e.getMessage());
    }
    return displayStrings;
  }
예제 #5
0
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String returnData(@PathParam("param") String msg) throws Exception {

    PreparedStatement query = null;
    String myString = null;
    String returnString = null;
    Connection conn = null;

    try {
      Class.forName(driver);
      conn = DriverManager.getConnection(url, userName, passwrod);
      query = conn.prepareStatement("select * from WATCH_DATA where _id = " + msg);
      ResultSet rs = query.executeQuery();

      while (rs.next()) {
        myString =
            "<p> id : "
                + rs.getInt(1)
                + " brand : "
                + rs.getString(2)
                + " model : "
                + rs.getString(3)
                + " color : "
                + rs.getString(4)
                + " gender : "
                + rs.getString(5)
                + " price : "
                + rs.getString(6)
                + " warrenty : "
                + rs.getString(7)
                + " image : "
                + rs.getString(8)
                + "</p>";
      }

      query.close();

      returnString = "<p>Database search</p>" + "<p>Date return: </p>" + myString;

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (conn != null) conn.close();
    }

    return returnString;
  }
예제 #6
0
  // TODO remove suppression
  @SuppressWarnings({"unchecked", "rawtypes"})
  private List<AnnotationResolver> getAnnotationResolvers(
      final GeneratorContext context, final TreeLogger logger) {
    java.lang.reflect.Method m = null;
    ArrayList args = new ArrayList();
    ArrayList types = new ArrayList();

    types.add(GeneratorContext.class);
    args.add(context);
    types.add(TreeLogger.class);
    args.add(logger);

    Object[] argValues = args.toArray();
    Class[] argtypes = (Class[]) types.toArray(new Class[argValues.length]);

    try {
      m = BINDING_DEFAULTS.getMethod("getAnnotationResolvers", argtypes);
    } catch (SecurityException e) {
      throw new RuntimeException(
          "could not call method `getAnnotationResolvers´ on " + BINDING_DEFAULTS, e);
    } catch (NoSuchMethodException e) {
      throw new RuntimeException(
          "could not resolve method `getAnnotationResolvers´ on " + BINDING_DEFAULTS, e);
    }

    List<AnnotationResolver> l = new ArrayList<AnnotationResolver>();
    try {
      l = (List<AnnotationResolver>) m.invoke(null, context, logger);
    } catch (IllegalArgumentException e) {
      throw new RuntimeException(
          "could not call method `getAnnotationResolvers´ on " + BINDING_DEFAULTS, e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(
          "could not call method `getAnnotationResolvers´ on " + BINDING_DEFAULTS, e);
    } catch (InvocationTargetException e) {
      throw new RuntimeException(
          "could not call method `getAnnotationResolvers´ on " + BINDING_DEFAULTS, e);
    }

    return l;
  }
예제 #7
0
  public static void main(String[] args) throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.append("insert into popedom (id, id_parent, code, name, menu, idx");
    for (int i = 0; i < ACTIONS; i++) {
      sb.append(",action" + i);
      sb.append(",action" + i + "_name");
    }
    sb.append(") values (?, null, ?, ?, true, ?");
    for (int i = 0; i < ACTIONS; i++) {
      sb.append(",?");
      sb.append(",?");
    }
    sb.append(")");

    Class.forName("org.postgresql.Driver");
    Connection conn = null;
    try {
      conn =
          DriverManager.getConnection(
              "jdbc:postgresql://192.168.2.250:5432/child", "postgres", "1234");
      conn.createStatement().executeUpdate("delete from popedom ");
      PreparedStatement ps = conn.prepareStatement(sb.toString());

      WinkApplication wa = new WinkApplication();
      int i = 0;
      for (Class c : wa.getClasses()) {
        Path path = (Path) c.getAnnotation(Path.class);
        ps.setLong(1, i + 1);
        ps.setString(2, path.value());

        if (c.isAnnotationPresent(Description.class)) {
          Description description = (Description) c.getAnnotation(Description.class);
          ps.setString(3, description.value());
        } else {
          ps.setString(3, path.value());
        }

        ps.setLong(4, i + 1);

        System.out.println(path.value());

        Map<String, String> methodMap = new TreeMap<String, String>();
        for (Method method : c.getDeclaredMethods()) {
          String code = null;
          if (method.isAnnotationPresent(GET.class)) {
            code = "GET";
          } else if (method.isAnnotationPresent(POST.class)) {
            code = "POST";
          } else if (method.isAnnotationPresent(PUT.class)) {
            code = "PUT";
          } else if (method.isAnnotationPresent(DELETE.class)) {
            code = "DELETE";
          } else {
            continue;
          }
          boolean isAnn = method.isAnnotationPresent(Path.class);
          if (isAnn) {
            code += " " + method.getAnnotation(Path.class).value();
          }

          String name = null;

          if (method.isAnnotationPresent(Description.class)) {
            Description description = method.getAnnotation(Description.class);
            name = description.value();
          } else {
            name = code;
          }
          methodMap.put(code, name);
        }

        int j = 0;
        for (Map.Entry<String, String> entry : methodMap.entrySet()) {
          ps.setString(5 + j * 2, entry.getKey());
          ps.setString(5 + j * 2 + 1, entry.getValue());
          j++;
        }
        for (int k = j; k < ACTIONS; k++) {
          ps.setString(5 + k * 2, null);
          ps.setString(5 + k * 2 + 1, null);
        }
        ps.executeUpdate();
        i++;
      }
      ps.close();
    } finally {
      try {
        conn.close();
      } finally {
        conn = null;
      }
    }
  }
예제 #8
0
  public ResourceMethod(MethodDeclaration delegate, Resource parent) {
    super(delegate);

    Set<String> httpMethods = new TreeSet<String>();
    Collection<AnnotationMirror> mirrors = delegate.getAnnotationMirrors();
    for (AnnotationMirror mirror : mirrors) {
      AnnotationTypeDeclaration annotationDeclaration = mirror.getAnnotationType().getDeclaration();
      HttpMethod httpMethodInfo = annotationDeclaration.getAnnotation(HttpMethod.class);
      if (httpMethodInfo != null) {
        // request method designator found.
        httpMethods.add(httpMethodInfo.value());
      }
    }

    if (httpMethods.isEmpty()) {
      throw new IllegalStateException(
          "A resource method must specify an HTTP method by using a request method designator annotation.");
    }

    this.httpMethods = httpMethods;

    Set<String> consumes;
    Consumes consumesInfo = delegate.getAnnotation(Consumes.class);
    if (consumesInfo != null) {
      consumes = new TreeSet<String>(Arrays.asList(JAXRSUtils.value(consumesInfo)));
    } else {
      consumes = new TreeSet<String>(parent.getConsumesMime());
    }
    this.consumesMime = consumes;

    Set<String> produces;
    Produces producesInfo = delegate.getAnnotation(Produces.class);
    if (producesInfo != null) {
      produces = new TreeSet<String>(Arrays.asList(JAXRSUtils.value(producesInfo)));
    } else {
      produces = new TreeSet<String>(parent.getProducesMime());
    }
    this.producesMime = produces;

    String subpath = null;
    Path pathInfo = delegate.getAnnotation(Path.class);
    if (pathInfo != null) {
      subpath = pathInfo.value();
    }

    ResourceEntityParameter entityParameter;
    List<ResourceEntityParameter> declaredEntityParameters =
        new ArrayList<ResourceEntityParameter>();
    List<ResourceParameter> resourceParameters;
    ResourceRepresentationMetadata outputPayload;
    ResourceMethodSignature signatureOverride =
        delegate.getAnnotation(ResourceMethodSignature.class);
    if (signatureOverride == null) {
      entityParameter = null;
      resourceParameters = new ArrayList<ResourceParameter>();
      // if we're not overriding the signature, assume we use the real method signature.
      for (ParameterDeclaration parameterDeclaration : getParameters()) {
        if (ResourceParameter.isResourceParameter(parameterDeclaration)) {
          resourceParameters.add(new ResourceParameter(parameterDeclaration));
        } else if (ResourceParameter.isFormBeanParameter(parameterDeclaration)) {
          resourceParameters.addAll(ResourceParameter.getFormBeanParameters(parameterDeclaration));
        } else if (parameterDeclaration.getAnnotation(Context.class) == null) {
          entityParameter = new ResourceEntityParameter(this, parameterDeclaration);
          declaredEntityParameters.add(entityParameter);
        }
      }

      DecoratedTypeMirror returnTypeMirror;
      TypeHint hintInfo = getAnnotation(TypeHint.class);
      if (hintInfo != null) {
        try {
          Class hint = hintInfo.value();
          AnnotationProcessorEnvironment env = net.sf.jelly.apt.Context.getCurrentEnvironment();
          if (TypeHint.NO_CONTENT.class.equals(hint)) {
            returnTypeMirror =
                (DecoratedTypeMirror)
                    TypeMirrorDecorator.decorate(env.getTypeUtils().getVoidType());
          } else {
            String hintName = hint.getName();

            if (TypeHint.NONE.class.equals(hint)) {
              hintName = hintInfo.qualifiedName();
            }

            if (!"##NONE".equals(hintName)) {
              TypeDeclaration type = env.getTypeDeclaration(hintName);
              returnTypeMirror =
                  (DecoratedTypeMirror)
                      TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type));
            } else {
              returnTypeMirror = (DecoratedTypeMirror) getReturnType();
            }
          }
        } catch (MirroredTypeException e) {
          returnTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(e.getTypeMirror());
        }
        returnTypeMirror.setDocComment(((DecoratedTypeMirror) getReturnType()).getDocComment());
      } else {
        returnTypeMirror = (DecoratedTypeMirror) getReturnType();

        if (getJavaDoc().get("returnWrapped")
            != null) { // support jax-doclets. see http://jira.codehaus.org/browse/ENUNCIATE-690
          String fqn = getJavaDoc().get("returnWrapped").get(0);
          AnnotationProcessorEnvironment env = net.sf.jelly.apt.Context.getCurrentEnvironment();
          TypeDeclaration type = env.getTypeDeclaration(fqn);
          if (type != null) {
            returnTypeMirror =
                (DecoratedTypeMirror)
                    TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type));
          }
        }

        // in the case where the return type is com.sun.jersey.api.JResponse,
        // we can use the type argument to get the entity type
        if (returnTypeMirror.isClass()
            && returnTypeMirror.isInstanceOf("com.sun.jersey.api.JResponse")) {
          DecoratedClassType jresponse = (DecoratedClassType) returnTypeMirror;
          if (!jresponse.getActualTypeArguments().isEmpty()) {
            DecoratedTypeMirror responseType =
                (DecoratedTypeMirror)
                    TypeMirrorDecorator.decorate(
                        jresponse.getActualTypeArguments().iterator().next());
            if (responseType.isDeclared()) {
              responseType.setDocComment(returnTypeMirror.getDocComment());
              returnTypeMirror = responseType;
            }
          }
        }
      }

      outputPayload =
          returnTypeMirror.isVoid() ? null : new ResourceRepresentationMetadata(returnTypeMirror);
    } else {
      entityParameter = loadEntityParameter(signatureOverride);
      declaredEntityParameters.add(entityParameter);
      resourceParameters = loadResourceParameters(signatureOverride);
      outputPayload = loadOutputPayload(signatureOverride);
    }

    JavaDoc.JavaDocTagList doclets =
        getJavaDoc()
            .get(
                "RequestHeader"); // support jax-doclets. see
                                  // http://jira.codehaus.org/browse/ENUNCIATE-690
    if (doclets != null) {
      for (String doclet : doclets) {
        int firstspace = doclet.indexOf(' ');
        String header = firstspace > 0 ? doclet.substring(0, firstspace) : doclet;
        String doc =
            ((firstspace > 0) && (firstspace + 1 < doclet.length()))
                ? doclet.substring(firstspace + 1)
                : "";
        resourceParameters.add(
            new ExplicitResourceParameter(this, doc, header, ResourceParameterType.HEADER));
      }
    }

    ArrayList<ResponseCode> statusCodes = new ArrayList<ResponseCode>();
    ArrayList<ResponseCode> warnings = new ArrayList<ResponseCode>();
    StatusCodes codes = getAnnotation(StatusCodes.class);
    if (codes != null) {
      for (org.codehaus.enunciate.jaxrs.ResponseCode code : codes.value()) {
        ResponseCode rc = new ResponseCode();
        rc.setCode(code.code());
        rc.setCondition(code.condition());
        statusCodes.add(rc);
      }
    }

    doclets =
        getJavaDoc()
            .get("HTTP"); // support jax-doclets. see http://jira.codehaus.org/browse/ENUNCIATE-690
    if (doclets != null) {
      for (String doclet : doclets) {
        int firstspace = doclet.indexOf(' ');
        String code = firstspace > 0 ? doclet.substring(0, firstspace) : doclet;
        String doc =
            ((firstspace > 0) && (firstspace + 1 < doclet.length()))
                ? doclet.substring(firstspace + 1)
                : "";
        try {
          ResponseCode rc = new ResponseCode();
          rc.setCode(Integer.parseInt(code));
          rc.setCondition(doc);
          statusCodes.add(rc);
        } catch (NumberFormatException e) {
          // fall through...
        }
      }
    }

    Warnings warningInfo = getAnnotation(Warnings.class);
    if (warningInfo != null) {
      for (org.codehaus.enunciate.jaxrs.ResponseCode code : warningInfo.value()) {
        ResponseCode rc = new ResponseCode();
        rc.setCode(code.code());
        rc.setCondition(code.condition());
        warnings.add(rc);
      }
    }

    codes = parent.getAnnotation(StatusCodes.class);
    if (codes != null) {
      for (org.codehaus.enunciate.jaxrs.ResponseCode code : codes.value()) {
        ResponseCode rc = new ResponseCode();
        rc.setCode(code.code());
        rc.setCondition(code.condition());
        statusCodes.add(rc);
      }
    }

    warningInfo = parent.getAnnotation(Warnings.class);
    if (warningInfo != null) {
      for (org.codehaus.enunciate.jaxrs.ResponseCode code : warningInfo.value()) {
        ResponseCode rc = new ResponseCode();
        rc.setCode(code.code());
        rc.setCondition(code.condition());
        warnings.add(rc);
      }
    }

    ResponseHeaders responseHeaders = parent.getAnnotation(ResponseHeaders.class);
    if (responseHeaders != null) {
      for (ResponseHeader header : responseHeaders.value()) {
        this.responseHeaders.put(header.name(), header.description());
      }
    }

    responseHeaders = getAnnotation(ResponseHeaders.class);
    if (responseHeaders != null) {
      for (ResponseHeader header : responseHeaders.value()) {
        this.responseHeaders.put(header.name(), header.description());
      }
    }

    doclets =
        getJavaDoc()
            .get(
                "ResponseHeader"); // support jax-doclets. see
                                   // http://jira.codehaus.org/browse/ENUNCIATE-690
    if (doclets != null) {
      for (String doclet : doclets) {
        int firstspace = doclet.indexOf(' ');
        String header = firstspace > 0 ? doclet.substring(0, firstspace) : doclet;
        String doc =
            ((firstspace > 0) && (firstspace + 1 < doclet.length()))
                ? doclet.substring(firstspace + 1)
                : "";
        this.responseHeaders.put(header, doc);
      }
    }

    this.entityParameter = entityParameter;
    this.resourceParameters = resourceParameters;
    this.subpath = subpath;
    this.parent = parent;
    this.statusCodes = statusCodes;
    this.warnings = warnings;
    this.representationMetadata = outputPayload;
    this.declaredEntityParameters = declaredEntityParameters;
  }