/**
  * Retrieves a list of arguments from the Ds3Request, including converting the required parameters
  * to arguments, and retrieving the arguments from the request header info
  */
 public static ImmutableList<Arguments> getRequiredArgs(final Ds3Request ds3Request) {
   final ImmutableList.Builder<Arguments> requiredArgs = ImmutableList.builder();
   requiredArgs.addAll(RequestConverterUtil.getRequiredArgsFromRequestHeader(ds3Request));
   requiredArgs.addAll(getArgsFromParamList(ds3Request.getRequiredQueryParams()));
   if (ds3Request.getIncludeInPath() && isResourceNotification(ds3Request.getResource())) {
     requiredArgs.add(new Arguments("Guid", "NotificationId"));
   }
   return requiredArgs.build();
 }
  /** Creates the C# request path code for a Ds3 request */
  public static String toRequestPath(final Ds3Request ds3Request) {
    final StringBuilder builder = new StringBuilder();

    if (ds3Request.getClassification() == Classification.amazons3) {
      builder.append(getAmazonS3RequestPath(ds3Request));
    } else if (ds3Request.getClassification() == Classification.spectrads3) {
      builder.append(getSpectraDs3RequestPath(ds3Request));
    }

    return builder.toString();
  }
  /**
   * Creates the Java request path code for a SpectraS3 request
   *
   * @param ds3Request A request
   * @return The Java request path code for an SpectraS3 request
   */
  protected static String getSpectraDs3RequestPath(final Ds3Request ds3Request) {
    final StringBuilder builder = new StringBuilder();
    if (ds3Request.getClassification() != Classification.spectrads3
        && ds3Request.getClassification() != Classification.spectrainternal) {
      return builder.toString();
    }
    if (ds3Request.getResource() == null) {
      return builder.append("\"/_rest_/\"").toString();
    }

    builder.append("\"/_rest_/").append(ds3Request.getResource().toString().toLowerCase());
    if (isNotificationRequest(ds3Request)
        && ds3Request.getIncludeInPath()
        && (getNotificationType(ds3Request) == NotificationType.DELETE
            || getNotificationType(ds3Request) == NotificationType.GET)) {
      builder.append("/\"").append(" + this.getNotificationId().toString()");
    } else if (hasBucketNameInPath(ds3Request)) {
      builder.append("/\"").append(" + this.bucketName");
    } else if (isResourceAnArg(ds3Request.getResource(), ds3Request.getIncludeInPath())) {
      final Arguments resourceArg = getArgFromResource(ds3Request.getResource());
      builder.append("/\"").append(" + ").append(resourceArgToString(resourceArg));
    } else {
      builder.append("\"");
    }
    return builder.toString();
  }
 /**
  * Creates the Java request path code for an AmazonS3 request
  *
  * @param ds3Request A request
  * @return The Java request path code for an AmazonS3 request
  */
 protected static String getAmazonS3RequestPath(final Ds3Request ds3Request) {
   final StringBuilder builder = new StringBuilder();
   if (ds3Request.getClassification() != Classification.amazons3) {
     return builder.toString();
   }
   builder.append("\"/\"");
   if (ds3Request.getBucketRequirement() == Requirement.REQUIRED) {
     builder.append(" + this.bucketName");
   }
   if (ds3Request.getObjectRequirement() == Requirement.REQUIRED) {
     builder.append(" + \"/\" + this.objectName");
   }
   return builder.toString();
 }
 /**
  * Creates the Java code for the Java SDK request path
  *
  * @param ds3Request A request
  * @return The Java code for the request path
  */
 protected static String getRequestPath(final Ds3Request ds3Request) {
   if (ds3Request.getClassification() == Classification.amazons3) {
     return getAmazonS3RequestPath(ds3Request);
   }
   // Spectra internal requests have the same path construction as SpectraDs3
   return getSpectraDs3RequestPath(ds3Request);
 }
 /**
  * Gets the list of Arguments needed to create the request constructor. This includes all non-void
  * required parameters, and arguments described within the request header.
  */
 @Override
 public ImmutableList<Arguments> toConstructorArgumentsList(final Ds3Request ds3Request) {
   final ImmutableList.Builder<Arguments> builder = ImmutableList.builder();
   builder.addAll(getRequiredArgsFromRequestHeader(ds3Request));
   builder.addAll(removeVoidArguments(toArgumentsList(ds3Request.getRequiredQueryParams())));
   return builder.build();
 }
  /**
   * Gets all the required imports that the Request will need in order to properly generate the Java
   * request code
   *
   * @param ds3Request A Ds3Request
   * @return The list of all imports that the Request requires for generating the Java request code
   */
  @Override
  public ImmutableList<String> getAllImports(
      final Ds3Request ds3Request, final String packageName) {
    final ImmutableSet.Builder<String> builder = ImmutableSet.builder();

    builder.add(getParentImport(ds3Request));
    builder.addAll(getImportsFromParamList(ds3Request.getRequiredQueryParams()));
    builder.addAll(getImportsFromParamList(ds3Request.getOptionalQueryParams()));

    if (isResourceAnArg(ds3Request.getResource(), ds3Request.getIncludeInPath())) {
      if (RequestConverterUtil.isResourceId(ds3Request.getResource())) {
        builder.add("java.util.UUID");
      }
      builder.add("com.google.common.net.UrlEscapers");
    }

    return builder.build().asList();
  }
 /**
  * Gets all the class variables to properly generate the variables and their getter functions.
  * This consists of all constructor arguments and optional arguments being converted into
  * variables.
  */
 @Override
 public ImmutableList<Variable> toClassVariableArguments(final Ds3Request ds3Request) {
   final ImmutableList.Builder<Variable> builder = ImmutableList.builder();
   for (final Arguments arg : toConstructorArgumentsList(ds3Request)) {
     builder.add(new Variable(arg.getName(), arg.getType(), true));
   }
   for (final Arguments arg : toOptionalArgumentsList(ds3Request.getOptionalQueryParams())) {
     builder.add(new Variable(arg.getName(), arg.getType(), false));
   }
   return builder.build();
 }
  @Override
  public Request generate(
      final Ds3Request ds3Request, final String packageName, final Ds3DocSpec docSpec) {
    final Ds3Request updatedRequest = updateDs3RequestParamTypes(ds3Request);
    final String requestName = removePath(updatedRequest.getName());
    final String requestPath = getRequestPath(updatedRequest);
    final String parentClass = getParentClass(ds3Request);

    final ImmutableList<Arguments> optionalArguments =
        splitAllUuidOptionalArguments(
            toOptionalArgumentsList(updatedRequest.getOptionalQueryParams()));

    final ImmutableList<RequestConstructor> constructors =
        splitAllUuidConstructors(toConstructorList(updatedRequest, requestName, docSpec));

    final ImmutableList<Variable> classVariableArguments =
        convertAllUuidClassVariables(toClassVariableArguments(updatedRequest));

    final ImmutableList<String> imports = getAllImports(updatedRequest, packageName);

    final ImmutableList<String> withConstructors =
        toWithConstructorList(optionalArguments, requestName, docSpec);

    return new Request(
        packageName,
        requestName,
        parentClass,
        updatedRequest.getHttpVerb(),
        requestPath,
        updatedRequest.getOperation(),
        updatedRequest.getAction(),
        optionalArguments,
        constructors,
        classVariableArguments,
        imports,
        withConstructors);
  }
  @Test
  public void SingleRequestHandler() throws IOException {
    final Ds3SpecParser parser = new Ds3SpecParserImpl();
    final Ds3ApiSpec spec =
        parser.getSpec(
            Ds3SpecParserImpl_Test.class.getResourceAsStream("/specs/singleRequestHandler.xml"));
    assertThat(spec, is(notNullValue()));
    assertThat(spec.getRequests().size(), is(1));

    final Ds3Request ds3Request = spec.getRequests().get(0);
    assertThat(
        ds3Request.getName(),
        is("com.spectralogic.s3.server.handler.reqhandler.amazons3.GetObjectRequest"));
    assertThat(ds3Request.getHttpVerb(), is(HttpVerb.GET));
    assertThat(ds3Request.getClassification(), is(Classification.amazons3));
    assertThat(ds3Request.getBucketRequirement(), is(Requirement.REQUIRED));
    assertThat(ds3Request.getObjectRequirement(), is(Requirement.REQUIRED));
    assertThat(ds3Request.getAction(), is(nullValue()));
    assertThat(ds3Request.getResource(), is(nullValue()));
    assertThat(ds3Request.getResourceType(), is(nullValue()));
    assertThat(ds3Request.getOperation(), is(nullValue()));

    assertThat(ds3Request.getDs3ResponseCodes().size(), is(1));
    assertThat(ds3Request.getDs3ResponseCodes().get(0).getCode(), is(200));
    assertThat(ds3Request.getDs3ResponseCodes().get(0).getDs3ResponseTypes().size(), is(1));
    assertThat(
        ds3Request.getDs3ResponseCodes().get(0).getDs3ResponseTypes().get(0).getType(),
        is("java.lang.String"));
    assertThat(
        ds3Request.getDs3ResponseCodes().get(0).getDs3ResponseTypes().get(0).getComponentType(),
        is(nullValue()));

    assertThat(ds3Request.getOptionalQueryParams().size(), is(0));
    assertThat(ds3Request.getRequiredQueryParams().size(), is(1));
    assertThat(ds3Request.getRequiredQueryParams().get(0).getName(), is("Id"));
    assertThat(ds3Request.getRequiredQueryParams().get(0).getType(), is("java.util.UUID"));
  }
 /**
  * Gets the list of required Arguments from a Ds3Request
  *
  * @param ds3Request A Ds3Request
  * @return A list of required Arguments
  */
 @Override
 public ImmutableList<Arguments> toRequiredArgumentsList(final Ds3Request ds3Request) {
   final ImmutableList.Builder<Arguments> requiredArgs = ImmutableList.builder();
   requiredArgs.addAll(toArgumentsList(ds3Request.getRequiredQueryParams()));
   return requiredArgs.build();
 }
 /**
  * Updates the Ds3Request's parameter: BucketId is changed from type UUID to String if said
  * parameter exists within the request
  */
 protected static Ds3Request updateDs3RequestParamTypes(final Ds3Request ds3Request) {
   return new Ds3Request(
       ds3Request.getName(),
       ds3Request.getHttpVerb(),
       ds3Request.getClassification(),
       ds3Request.getBucketRequirement(),
       ds3Request.getObjectRequirement(),
       ds3Request.getAction(),
       ds3Request.getResource(),
       ds3Request.getResourceType(),
       ds3Request.getOperation(),
       ds3Request.getIncludeInPath(),
       ds3Request.getDs3ResponseCodes(),
       updateDs3ParamListTypes(ds3Request.getOptionalQueryParams()),
       updateDs3ParamListTypes(ds3Request.getRequiredQueryParams()));
 }