Search in sources :

Example 76 with EventMetadata

use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.

the class ProxyResponseWriter method transfer.

private void transfer(final HttpConduitWrapper http, final RemoteRepository repo, final String path, final boolean writeBody, final UserPass proxyUserPass) throws IOException, IndyWorkflowException {
    if (transferred) {
        return;
    }
    transferred = true;
    if (!http.isOpen()) {
        throw new IOException("Sink channel already closed (or null)!");
    }
    final EventMetadata eventMetadata = createEventMetadata(writeBody, proxyUserPass, path, repo);
    Transfer txfr = null;
    try {
        txfr = contentController.get(repo.getKey(), path, eventMetadata);
    } catch (final IndyWorkflowException e) {
        // block TransferException to allow handling below.
        if (!(e.getCause() instanceof TransferException)) {
            throw e;
        }
        logger.debug("Suppressed exception for further handling inside proxy logic:", e);
    }
    if (txfr != null && txfr.exists()) {
        http.writeExistingTransfer(txfr, writeBody, path, eventMetadata);
    } else {
        http.writeNotFoundTransfer(repo, path);
    }
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 77 with EventMetadata

use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.

the class ProxyResponseWriter method getRepository.

private RemoteRepository getRepository(final URL url) throws IndyDataException {
    int port = url.getPort();
    if (port < 1) {
        port = url.getDefaultPort();
    }
    String name = PROXY_REPO_PREFIX + url.getHost().replace('.', '-') + '_' + port;
    final String baseUrl = String.format("%s://%s:%s/", url.getProtocol(), url.getHost(), port);
    ArtifactStoreQuery<RemoteRepository> query = storeManager.query().packageType(GENERIC_PKG_KEY).storeType(RemoteRepository.class);
    RemoteRepository remote = query.getRemoteRepositoryByUrl(baseUrl);
    if (remote == null) {
        logger.debug("Looking for remote repo with name: {}", name);
        remote = query.getByName(name);
        // if repo with this name already exists, it has a different url, so we need to use a different name
        int i = 1;
        while (remote != null) {
            name = PROXY_REPO_PREFIX + url.getHost().replace('.', '-') + "_" + i++;
            logger.debug("Looking for remote repo with name: {}", name);
            remote = query.getByName(name);
        }
    }
    if (remote == null) {
        logger.debug("Creating remote repository for HTTProx request: {}", url);
        final UrlInfo info = new UrlInfo(url.toExternalForm());
        if (repoCreator == null) {
            throw new IndyDataException("No valid instance of ProxyRepositoryCreator. Cannot auto-create remote proxy to: '{}'", baseUrl);
        }
        final UserPass up = UserPass.parse(ApplicationHeader.authorization, httpRequest, url.getAuthority());
        remote = repoCreator.create(name, baseUrl, info, up, LoggerFactory.getLogger(repoCreator.getClass()));
        remote.setMetadata(ArtifactStore.METADATA_ORIGIN, ProxyAcceptHandler.HTTPROX_ORIGIN);
        final ChangeSummary changeSummary = new ChangeSummary(ChangeSummary.SYSTEM_USER, "Creating HTTProx proxy for: " + info.getUrl());
        storeManager.storeArtifactStore(remote, changeSummary, false, true, new EventMetadata());
    }
    return remote;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) UrlInfo(org.commonjava.indy.util.UrlInfo) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) UserPass(org.commonjava.indy.subsys.http.util.UserPass) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 78 with EventMetadata

use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.

the class ProxyResponseWriter method createEventMetadata.

private EventMetadata createEventMetadata(final boolean writeBody, final UserPass proxyUserPass, final String path, final RemoteRepository repo) throws IndyWorkflowException {
    final EventMetadata eventMetadata = new EventMetadata();
    if (writeBody) {
        TrackingKey tk = null;
        switch(config.getTrackingType()) {
            case ALWAYS:
                {
                    if (proxyUserPass == null) {
                        throw new IndyWorkflowException(ApplicationStatus.BAD_REQUEST.code(), "Tracking is always-on, but no username was provided! Cannot initialize tracking key.");
                    }
                    tk = new TrackingKey(proxyUserPass.getUser());
                    break;
                }
            case SUFFIX:
                {
                    if (proxyUserPass != null) {
                        final String user = proxyUserPass.getUser();
                        // TODO: Will this always be non-null here? Can we have an unsecured proxy?
                        if (user.endsWith(TRACKED_USER_SUFFIX) && user.length() > TRACKED_USER_SUFFIX.length()) {
                            tk = new TrackingKey(StringUtils.substring(user, 0, -TRACKED_USER_SUFFIX.length()));
                        }
                    }
                    break;
                }
            default:
                {
                }
        }
        if (tk != null) {
            logger.debug("TRACKING {} in {} (KEY: {})", path, repo, tk);
            eventMetadata.set(FoloConstants.TRACKING_KEY, tk);
            eventMetadata.set(FoloConstants.ACCESS_CHANNEL, AccessChannel.GENERIC_PROXY);
        } else {
            logger.debug("NOT TRACKING: {} in {}", path, repo);
        }
    } else {
        logger.debug("NOT TRACKING non-body request: {} in {}", path, repo);
    }
    eventMetadata.setPackageType(PKG_TYPE_GENERIC_HTTP);
    return eventMetadata;
}
Also used : IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) TrackingKey(org.commonjava.indy.folo.model.TrackingKey)

Example 79 with EventMetadata

use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.

the class MergedFileUploadListener method reMerge.

private void reMerge(final Group group, final String path) throws IOException {
    logger.debug("Updating merged metadata file: {} in group: {}", path, group.getKey());
    final Transfer[] toDelete = { fileManager.getStorageReference(group, path), fileManager.getStorageReference(group, path + GroupMergeHelper.MERGEINFO_SUFFIX), fileManager.getStorageReference(group, path + GroupMergeHelper.SHA_SUFFIX), fileManager.getStorageReference(group, path + GroupMergeHelper.MD5_SUFFIX) };
    for (final Transfer item : toDelete) {
        logger.debug("Attempting to delete: {}", item);
        if (item.exists()) {
            final boolean result = item.delete();
            logger.debug("Deleted: {} (success? {})", item, result);
            if (fileEvent != null) {
                logger.debug("Firing deletion event for: {}", item);
                fileEvent.fire(new FileDeletionEvent(item, new EventMetadata()));
            }
        }
    }
}
Also used : Transfer(org.commonjava.maven.galley.model.Transfer) FileDeletionEvent(org.commonjava.maven.galley.event.FileDeletionEvent) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 80 with EventMetadata

use of org.commonjava.maven.galley.event.EventMetadata in project indy by Commonjava.

the class AutoProxDataManagerDecorator method getHostedRepository.

private HostedRepository getHostedRepository(final StoreKey key, final StoreKey impliedBy) throws IndyDataException {
    HostedRepository repo = (HostedRepository) dataManager.getArtifactStore(key);
    if (!catalog.isEnabled()) {
        logger.debug("AutoProx decorator disabled; returning: {}", repo);
        return repo;
    }
    logger.debug("AutoProx decorator active");
    if (repo == null) {
        logger.info("AutoProx: creating repository for: {}", key);
        try {
            repo = catalog.createHostedRepository(key);
        } catch (final AutoProxRuleException e) {
            throw new IndyDataException("[AUTOPROX] Failed to create new hosted repository from factory matching: '%s'. Reason: %s", e, key, e.getMessage());
        }
        if (repo != null) {
            final ChangeSummary changeSummary = new ChangeSummary(ChangeSummary.SYSTEM_USER, "AUTOPROX: Creating hosted repository for: '" + key + "'");
            final EventMetadata eventMetadata = new EventMetadata().set(StoreDataManager.EVENT_ORIGIN, AutoProxConstants.STORE_ORIGIN).set(AutoProxConstants.ORIGINATING_STORE, impliedBy == null ? repo.getKey() : impliedBy);
            dataManager.storeArtifactStore(repo, changeSummary, false, true, eventMetadata);
        }
    }
    return repo;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Aggregations

EventMetadata (org.commonjava.maven.galley.event.EventMetadata)154 Transfer (org.commonjava.maven.galley.model.Transfer)65 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)49 Test (org.junit.Test)47 IndyDataException (org.commonjava.indy.data.IndyDataException)44 StoreKey (org.commonjava.indy.model.core.StoreKey)40 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)38 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)37 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)35 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)28 Group (org.commonjava.indy.model.core.Group)26 ApiOperation (io.swagger.annotations.ApiOperation)23 Path (javax.ws.rs.Path)23 Logger (org.slf4j.Logger)23 ApiResponses (io.swagger.annotations.ApiResponses)22 IOException (java.io.IOException)21 InputStream (java.io.InputStream)20 HostedRepository (org.commonjava.indy.model.core.HostedRepository)20 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)20 ArrayList (java.util.ArrayList)18