Package com.getperka.flatpack.client.dto

Examples of com.getperka.flatpack.client.dto.EndpointDescription


                  return e1.getPath().compareTo(e2.getPath());
                }
              });
              Iterator<EndpointDescription> iter = sortedEndpoints.iterator();
              while (iter.hasNext()) {
                EndpointDescription ed = iter.next();
                if (ed.getPath() != null && ed.getPath().contains("{path:.*}")) {
                  iter.remove();
                }
              }
              return sortedEndpoints;
            }
            else if ("flatpackEndpoints".equals(propertyName)) {
              List<EndpointDescription> sortedEndpoints = new ArrayList<EndpointDescription>(
                  apiDescription.getEndpoints());
              Collections.sort(sortedEndpoints, new Comparator<EndpointDescription>() {
                @Override
                public int compare(EndpointDescription e1, EndpointDescription e2) {
                  return e1.getPath().compareTo(e2.getPath());
                }
              });
              Iterator<EndpointDescription> iter = sortedEndpoints.iterator();
              while (iter.hasNext()) {
                if (!hasCustomRequestBuilderClass(iter.next())) {
                  iter.remove();
                }
              }
              return sortedEndpoints;
            }
            else if ("requireNames".equals(propertyName)) {
              return entityRequires;
            }
            return super.getProperty(interp, self, o, property, propertyName);
          }
        });

    group.registerModelAdaptor(EndpointDescription.class,
        new ObjectModelAdaptor() {
          @Override
          public Object getProperty(Interpreter interp, ST self, Object o,
              Object property, String propertyName)
              throws STNoSuchPropertyException {
            EndpointDescription end = (EndpointDescription) o;
            if ("docString".equals(propertyName)) {
              return jsDocString(end.getDocString());
            }
            if ("methodName".equals(propertyName)) {

              String path = end.getPath();
              String[] parts = path.split(Pattern.quote("/"));
              StringBuilder sb = new StringBuilder();
              sb.append(end.getMethod().toLowerCase());
              for (int i = 3, j = parts.length; i < j; i++) {
                String part = parts[i];
                if (part.length() == 0) continue;
                if (!part.startsWith("{") && !part.endsWith("}")) {
                  if (part.contains(".")) {
                    String[] dotPart = part.split(Pattern.quote("."));
                    for (String dot : dotPart) {
                      sb.append(upcase(dot));
                    }
                  }
                  else {
                    sb.append(upcase(part));
                  }
                }
                else {
                  sb.append(upcase(part.substring(1, part.length() - 1)));
                }
              }

              return sb.toString();
            }

            else if ("methodParameterList".equals(propertyName)) {
              String path = end.getPath();
              String[] parts = path.split(Pattern.quote("/"));
              StringBuilder sb = new StringBuilder();
              int paramCount = 0;
              for (int i = 3, j = parts.length; i < j; i++) {
                String part = parts[i];
                if (part.length() == 0) continue;

                if (part.startsWith("{") && part.endsWith("}")) {
                  String name = part.substring(1, part.length() - 1);
                  sb.append(name);
                  paramCount++;
                  if (end.getPathParameters() != null
                    && paramCount < end.getPathParameters().size()) {
                    sb.append(", ");
                  }
                }
              }
              if (end.getEntity() != null) {
                if (paramCount > 0) {
                  sb.append(", ");
                }
                sb.append(getNameForType(end.getEntity()));
              }

              return sb.toString();
            }
            else if ("entityName".equals(propertyName)) {
              return getNameForType(end.getEntity());
            }
            else if ("requestBuilderClassName".equals(propertyName)) {

              if (hasCustomRequestBuilderClass(end)) {
                return getBuilderReturnType(end);
              }
              else {
                return "com.getperka.flatpack.client.JsonRequest";
              }
            }

            else if ("requestBuilderBlockName".equals(propertyName)) {
              return getBuilderReturnType(end) + "Block";
            }

            else if ("pathDecoded".equals(propertyName)) {
              // URL-decode the path in the endpoint description
              try {
                String decoded = URLDecoder.decode(end.getPath(), "UTF8");
                return decoded;
              } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
              }
            }
View Full Code Here


        new ObjectModelAdaptor() {
          @Override
          public Object getProperty(Interpreter interp, ST self, Object o,
              Object property, String propertyName)
              throws STNoSuchPropertyException {
            EndpointDescription end = (EndpointDescription) o;
            if ("docString".equals(propertyName)) {
              return doxygenDocString(end.getDocString());
            }
            else if ("methodName".equals(propertyName)) {
              StringBuilder sb = new StringBuilder();

              sb.append("- (" + getBuilderReturnType(end) + " *)");

              sb.append(getMethodizedPath(end));

              if (end.getEntity() != null) {
                if (end.getPathParameters() != null && end.getPathParameters().size() > 0) {
                  sb.append(" entity");
                }
                String type = objcTypeForType(end.getEntity());
                sb.append(":(" + type + " *)");
                String paramName = type;
                if (type.startsWith(classPrefix)) {
                  paramName = downcase(type.substring(classPrefix.length()));
                }
                sb.append(paramName);
              }

              return sb.toString();
            }

            else if ("requestBuilderClassName".equals(propertyName)) {
              return getBuilderReturnType(end);
            }

            else if ("requestBuilderBlockName".equals(propertyName)) {
              return getBuilderReturnType(end) + "Block";
            }

            else if ("entityReturnType".equals(propertyName)) {
              String type = objcFlatpackReturnType(end.getReturnType());
              return type.equals("void") ? null : type;
            }

            else if ("entityReturnName".equals(propertyName)) {
              if (end.getReturnType() == null) return null;

              String name = "result";
              if (end.getReturnType().getName() != null) {
                name = end.getReturnType().getName();
              }

              if (end.getReturnType().getListElement() != null) {
                if (end.getReturnType().getListElement().getName() != null) {
                  name = end.getReturnType().getListElement().getName();
                }
                name = pluralOf(name);
              }

              return name;
            }

            else if ("pathDecoded".equals(propertyName)) {
              // URL-decode the path in the endpoint description
              try {
                String decoded = URLDecoder.decode(end.getPath(), "UTF8");
                return decoded;
              } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
              }
            }
View Full Code Here

    // Extract API endpoints
    Set<EndpointDescription> endpoints = new LinkedHashSet<EndpointDescription>();

    for (Method method : apiMethods) {
      EndpointDescription desc = describeEndpoint(method);
      if (desc != null) {
        endpoints.add(desc);
      }
    }
    description.setEndpoints(new ArrayList<EndpointDescription>(endpoints));
View Full Code Here

    // This path has special characters URL-escaped, so we'll undo the escaping
    String path = builder.build().toString();
    path = URLDecoder.decode(path, "UTF8");

    // Build the EndpointDescription
    EndpointDescription desc = new EndpointDescription(methodName, path);
    List<ParameterDescription> pathParams = new ArrayList<ParameterDescription>();
    List<ParameterDescription> queryParams = new ArrayList<ParameterDescription>();
    Annotation[][] annotations = method.getParameterAnnotations();
    java.lang.reflect.Type[] parameters = method.getGenericParameterTypes();
    for (int i = 0, j = parameters.length; i < j; i++) {
      Type paramType = reference(parameters[i]);
      if (annotations[i].length == 0) {
        // Assume that an un-annotated parameter is the main entity type
        desc.setEntity(paramType);
      } else {
        for (Annotation annotation : annotations[i]) {
          if (PathParam.class.equals(annotation.annotationType())) {
            PathParam pathParam = (PathParam) annotation;
            ParameterDescription param = new ParameterDescription(desc, pathParam.value(),
                paramType);
            String docString = getDocStrings(declaringClass).get(methodKey + "[" + i + "]");
            param.setDocString(replaceLinks(docString));
            pathParams.add(param);
          } else if (QueryParam.class.equals(annotation.annotationType())) {
            QueryParam queryParam = (QueryParam) annotation;
            ParameterDescription param = new ParameterDescription(desc, queryParam.value(),
                paramType);
            String docString = getDocStrings(declaringClass).get(methodKey + "[" + i + "]");
            param.setDocString(replaceLinks(docString));
            queryParams.add(param);
          }
        }
      }
    }

    // If the returned entity type is described, extract the information
    FlatPackResponse responseAnnotation = method.getAnnotation(FlatPackResponse.class);
    if (responseAnnotation != null) {
      java.lang.reflect.Type reflectType = FlatPackTypes.createType(responseAnnotation.value());
      Type returnType = reference(reflectType);
      desc.setReturnDocString(
          responseAnnotation.description().isEmpty() ? null : responseAnnotation.description());
      desc.setReturnType(returnType);
      desc.setTraversalMode(responseAnnotation.traversalMode());

      List<TypeDescription> extraTypeDescriptions = new ArrayList<TypeDescription>();
      for (ExtraEntity extra : responseAnnotation.extra()) {
        TypeDescription typeDescription = new TypeDescription();
        typeDescription.setDocString(extra.description());
        typeDescription.setType(reference(FlatPackTypes.createType(extra.type())));
        extraTypeDescriptions.add(typeDescription);
      }
      desc.setExtraReturnData(extraTypeDescriptions.isEmpty() ? null : extraTypeDescriptions);
    } else if (HasUuid.class.isAssignableFrom(method.getReturnType())) {
      Type returnType = reference(method.getReturnType());
      desc.setReturnType(returnType);
      desc.setTraversalMode(TraversalMode.SIMPLE);
    }

    String docString = getDocStrings(declaringClass).get(methodKey);
    desc.setDocString(replaceLinks(docString));
    desc.setPathParameters(pathParams.isEmpty() ? null : pathParams);
    desc.setQueryParameters(queryParams.isEmpty() ? null : queryParams);
    return desc;
  }
View Full Code Here

        new ObjectModelAdaptor() {
          @Override
          public Object getProperty(Interpreter interp, ST self, Object o,
              Object property, String propertyName)
              throws STNoSuchPropertyException {
            EndpointDescription end = (EndpointDescription) o;
            if ("className".equals(propertyName) || "methodName".equals(propertyName)) {
              // Convert a path like /api/2/foo/bar/{}/baz to FooBarBazMethod
              String path = end.getPath();
              String[] parts = path.split(Pattern.quote("/"));
              StringBuilder sb = new StringBuilder();
              for (int i = 3, j = parts.length; i < j; i++) {
                try {
                  String part = parts[i];
                  if (part.length() == 0) {
                    continue;
                  }
                  StringBuilder decodedPart = new StringBuilder(URLDecoder
                      .decode(part, "UTF8"));
                  // Trim characters that aren't legal
                  for (int k = decodedPart.length() - 1; k >= 0; k--) {
                    if (!Character.isJavaIdentifierPart(decodedPart.charAt(k))) {
                      decodedPart.deleteCharAt(k);
                    }
                  }
                  // Append the new name part, using camel-cased names
                  String newPart = decodedPart.toString();
                  if (sb.length() > 0) {
                    newPart = upcase(newPart);
                  }
                  sb.append(newPart);
                } catch (UnsupportedEncodingException e) {
                  throw new RuntimeException(e);
                }
              }
              sb.append(upcase(end.getMethod().toLowerCase()));
              String name = sb.toString();

              return "className".equals(propertyName) ? upcase(name) : camelCaseToUnderscore(name);
            } else if ("pathDecoded".equals(propertyName)) {
              // URL-decode the path in the endpoint description
              try {
                String decoded = URLDecoder.decode(end.getPath(), "UTF8");
                return decoded;
              } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
              }
            }
View Full Code Here

    group.registerModelAdaptor(EndpointDescription.class, new ObjectModelAdaptor() {
      @Override
      public Object getProperty(Interpreter interp, ST self, Object o, Object property,
          String propertyName) throws STNoSuchPropertyException {
        EndpointDescription end = (EndpointDescription) o;
        if ("combinedArgs".equals(propertyName)) {
          // Return the path and query parameters together
          List<ParameterDescription> toReturn = new ArrayList<ParameterDescription>();
          if (end.getPathParameters() != null) {
            toReturn.addAll(end.getPathParameters());
          }
          if (end.getQueryParameters() != null) {
            toReturn.addAll(end.getQueryParameters());
          }
          return toReturn;
        } else if ("javaName".equals(propertyName) || "javaNameUpcase".equals(propertyName)) {
          // Convert a path like /api/2/foo/bar/{}/baz to fooBarBazMethod
          String path = end.getPath();
          String[] parts = path.split(Pattern.quote("/"));
          StringBuilder sb = new StringBuilder();
          for (int i = stripPathSegments, j = parts.length; i < j; i++) {
            try {
              String part = parts[i];
              if (part.length() == 0) {
                continue;
              }
              StringBuilder decodedPart = new StringBuilder(URLDecoder.decode(part, "UTF8"));
              // Trim characters that aren't legal
              for (int k = decodedPart.length() - 1; k >= 0; k--) {
                if (!Character.isJavaIdentifierPart(decodedPart.charAt(k))) {
                  decodedPart.deleteCharAt(k);
                }
              }
              // Append the new name part, using camel-cased names
              String newPart = decodedPart.toString();
              if (sb.length() > 0) {
                newPart = upcase(newPart);
              }
              sb.append(newPart);
            } catch (UnsupportedEncodingException e) {
              throw new RuntimeException(e);
            }
          }
          sb.append(upcase(end.getMethod().toLowerCase()));
          String javaName = sb.toString();
          if ("javaNameUpcase".equals(propertyName)) {
            javaName = upcase(javaName);
          }
          return javaName;
        } else if ("pathDecoded".equals(propertyName)) {
          // URL-decode the path in the endpoint description
          try {
            String decoded = URLDecoder.decode(end.getPath(), "UTF8");
            return decoded;
          } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
          }
        } else if ("hasPayload".equals(propertyName)) {
          return end.getEntity() != null;
        }
        return super.getProperty(interp, self, o, property, propertyName);
      }
    });
    group.registerModelAdaptor(EntityDescription.class, new ObjectModelAdaptor() {
View Full Code Here

        new ObjectModelAdaptor() {
          @Override
          public Object getProperty(Interpreter interp, ST self, Object o,
              Object property, String propertyName)
              throws STNoSuchPropertyException {
            EndpointDescription end = (EndpointDescription) o;
            if ("docString".equals(propertyName)) {
              return doxygenDocString(end.getDocString());
            }
            else if ("methodName".equals(propertyName)) {
              StringBuilder sb = new StringBuilder();

              sb.append("public function " + getMethodizedPath(end));

              if (end.getEntity() != null) {
                if (end.getPathParameters() != null && end.getPathParameters().size() > 0) {
                  sb.append(" entity");
                }
                String type = phpTypeForType(end.getEntity());
                String paramName = type;
                if (type.startsWith(classPrefix)) {
                  paramName = downcase(type.substring(classPrefix.length()));
                }
                sb.append("($" + paramName + ")");
              }

              /*
               * if (end.getEntity() != null) { if (end.getPathParameters() != null &&
               * end.getPathParameters().size() > 0) { sb.append(" entity"); } String type =
               * phpTypeForType(end.getEntity()); sb.append(":(" + type + " *)"); String paramName =
               * type; if (type.startsWith(classPrefix)) { paramName =
               * downcase(type.substring(classPrefix.length())); } sb.append(paramName); }
               */

              return sb.toString();
            }

            else if ("requestBuilderClassName".equals(propertyName)) {
              return getBuilderReturnType(end);
            }

            else if ("requestBuilderBlockName".equals(propertyName)) {
              return getBuilderReturnType(end) + "Block";
            }

            else if ("entityReturnType".equals(propertyName)) {
              String type = phpFlatpackReturnType(end.getReturnType());
              return type.equals("void") ? null : type;
            }

            else if ("entityReturnName".equals(propertyName)) {
              if (end.getReturnType() == null) return null;

              String name = "result";
              if (end.getReturnType().getName() != null) {
                name = end.getReturnType().getName();
              }

              if (end.getReturnType().getListElement() != null) {
                if (end.getReturnType().getListElement().getName() != null) {
                  name = end.getReturnType().getListElement().getName();
                }
                name = pluralOf(name);
              }

              return name;
            }

            else if ("pathDecoded".equals(propertyName)) {
              // URL-decode the path in the endpoint description
              try {
                String decoded = URLDecoder.decode(end.getPath(), "UTF8");
                return decoded;
              } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
              }
            }
View Full Code Here

TOP

Related Classes of com.getperka.flatpack.client.dto.EndpointDescription

Copyright © 2018 www.massapicom. 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.