@Override
  public Object upload(Context context, Request request, Response response, List<FileItem> files)
      throws ResourceException {
    // we have "nibbles": (params,fileA,[fileB])+
    // the second file is optional
    // if two files are present, one of them should be POM
    String repositoryId = null;

    boolean hasPom = false;

    boolean isPom = false;

    InputStream is = null;

    String groupId = null;

    String artifactId = null;

    String version = null;

    String classifier = null;

    String packaging = null;

    String extension = null;

    ArtifactCoordinate coords = null;

    PomArtifactManager pomManager =
        new PomArtifactManager(getNexus().getNexusConfiguration().getTemporaryDirectory());

    try {
      for (FileItem fi : files) {
        if (fi.isFormField()) {
          // a parameter
          if ("r".equals(fi.getFieldName())) {
            repositoryId = fi.getString();
          } else if ("g".equals(fi.getFieldName())) {
            groupId = fi.getString();
          } else if ("a".equals(fi.getFieldName())) {
            artifactId = fi.getString();
          } else if ("v".equals(fi.getFieldName())) {
            version = fi.getString();
          } else if ("p".equals(fi.getFieldName())) {
            packaging = fi.getString();
          } else if ("c".equals(fi.getFieldName())) {
            classifier = fi.getString();
          } else if ("e".equals(fi.getFieldName())) {
            extension = fi.getString();
          } else if ("hasPom".equals(fi.getFieldName())) {
            hasPom = Boolean.parseBoolean(fi.getString());
          }

          coords = new ArtifactCoordinate();
          coords.setGroupId(groupId);
          coords.setArtifactId(artifactId);
          coords.setVersion(version);
          coords.setPackaging(packaging);
        } else {
          // a file
          isPom = fi.getName().endsWith(".pom") || fi.getName().endsWith("pom.xml");

          ArtifactStoreRequest gavRequest = null;

          if (hasPom) {
            if (isPom) {
              // let it "thru" the pomManager to be able to get GAV from it on later pass
              pomManager.storeTempPomFile(fi.getInputStream());

              is = pomManager.getTempPomFileInputStream();
            } else {
              is = fi.getInputStream();
            }

            try {
              coords = pomManager.getArtifactCoordinateFromTempPomFile();
            } catch (IOException e) {
              getLogger().info(e.getMessage());

              throw new ResourceException(
                  Status.CLIENT_ERROR_BAD_REQUEST,
                  "Error occurred while reading the POM file. Malformed POM?");
            }

            if (isPom) {
              gavRequest =
                  getResourceStoreRequest(
                      request,
                      true,
                      repositoryId,
                      coords.getGroupId(),
                      coords.getArtifactId(),
                      coords.getVersion(),
                      coords.getPackaging(),
                      null,
                      null);
            } else {
              gavRequest =
                  getResourceStoreRequest(
                      request,
                      true,
                      repositoryId,
                      coords.getGroupId(),
                      coords.getArtifactId(),
                      coords.getVersion(),
                      coords.getPackaging(),
                      classifier,
                      extension);
            }
          } else {
            is = fi.getInputStream();

            gavRequest =
                getResourceStoreRequest(
                    request,
                    true,
                    repositoryId,
                    groupId,
                    artifactId,
                    version,
                    packaging,
                    classifier,
                    extension);
          }

          MavenRepository mr = gavRequest.getMavenRepository();

          ArtifactStoreHelper helper = mr.getArtifactStoreHelper();

          // temporarily we disable SNAPSHOT upload
          // check is it a Snapshot repo
          if (RepositoryPolicy.SNAPSHOT.equals(mr.getRepositoryPolicy())) {
            getLogger()
                .info("Upload to SNAPSHOT maven repository attempted, returning Bad Request.");

            throw new ResourceException(
                Status.CLIENT_ERROR_BAD_REQUEST,
                "This is a Maven SNAPSHOT repository, and manual upload against it is forbidden!");
          }

          if (!versionMatchesPolicy(gavRequest.getVersion(), mr.getRepositoryPolicy())) {
            getLogger()
                .warn("Version (" + gavRequest.getVersion() + ") and Repository Policy mismatch");
            throw new ResourceException(
                Status.CLIENT_ERROR_BAD_REQUEST,
                "The version "
                    + gavRequest.getVersion()
                    + " does not match the repository policy!");
          }

          if (isPom) {
            helper.storeArtifactPom(gavRequest, is, null);

            isPom = false;
          } else {
            if (hasPom) {
              helper.storeArtifact(gavRequest, is, null);
            } else {
              helper.storeArtifactWithGeneratedPom(gavRequest, packaging, is, null);
            }
          }
        }
      }
    } catch (Throwable t) {
      return buildUploadFailedHtmlResponse(t, request, response);
    } finally {
      if (hasPom) {
        pomManager.removeTempPomFile();
      }
    }

    return coords;
  }