Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2014 Red Hat, Inc.. All rights reserved. This program and the accompanying materials are made available under the terms of the GNU Public License v3.0 which accompanies this distribution, and is available at http://www.gnu.org/licenses/gpl.html Contributors: Red Hat, Inc. - initial API and implementation /
 
 package org.commonjava.aprox.core.bind.vertx;
 
 import static org.apache.commons.codec.digest.DigestUtils.shaHex;
 import static org.apache.commons.io.IOUtils.closeQuietly;
 import static org.commonjava.aprox.bind.vertx.util.ResponseUtils.formatCreatedResponse;
 import static org.commonjava.aprox.bind.vertx.util.ResponseUtils.formatOkResponseWithEntity;
 import static org.commonjava.aprox.bind.vertx.util.ResponseUtils.formatResponse;
 import static org.commonjava.aprox.core.ctl.ContentController.LISTING_HTML_FILE;
 import static org.commonjava.vertx.vabr.types.BuiltInParam._routeBase;
 
 import java.io.File;
 import java.util.Date;
 
 
 
 @Handles( key = "content" )
 public class ContentAccessHandler
     implements RequestHandler
 {
 
     private final Logger logger = LoggerFactory.getLoggergetClass() );
 
     @Inject
 
     @Inject
     private DataFileConfiguration config;
 
     @Inject
     private Vertx vertx;
 
     @Inject
     private UriFormatter uriFormatter;
 
     protected ContentAccessHandler()
     {
     }
 
     public ContentAccessHandlerfinal ContentController controllerfinal UriFormatter uriFormatter )
     {
         this. = controller;
         this. = uriFormatter;
     }
 
     @Routes( { @Route( path = "/:type=(hosted|group)/:name/:path=(.+)", method = ., binding = ., fork = false ) } )
     public void doCreatefinal HttpServerRequest request )
     {
         request.pause();
 
         final String name = request.params()
                                    .get..key() );
         final String path = request.params()
                                    .get..key() );
 
         final String type = request.params()
                                   .get..key() );
        final StoreType st = StoreType.gettype );
        final File dir = new File.getWorkBasedir(), "pending-uploads" );
        dir.getAbsoluteFile()
           .mkdirs();
        final String fname = shaHextype + ":" + name + "/" + path + "." + System.currentTimeMillis() );
        final File workFile = new Filedirfname );
        // FIXME: What a horrible mess!
        .fileSystem()
             .openworkFile.getAbsolutePath(), new AsyncResultHandler<AsyncFile>()
             {
                 @Override
                 public void handlefinal AsyncResult<AsyncFilear )
                 {
                     if ( ar.failed() )
                     {
                         final Throwable error = ar.cause();
                         .error( String.format"Failed to upload: %s to: %s. Reason: %s"pathname,
                                                      error.getMessage() ), error );
                         formatResponseerrorrequest );
                         return;
                     }
                     final AsyncFile file = ar.result();
                     final Pump pump = Pump.createPumprequestfile );
                     request.endHandlernew Handler<Void>()
                     {
                         @Override
                         public void handlefinal Void event )
                         {
                             file.closenew AsyncResultHandler<Void>()
                             {
                                 @Override
                                 public void handlefinal AsyncResult<Voidar )
                                 {
                                     if ( ar.failed() )
                                     {
                                         final Throwable error = ar.cause();
                                         .error( String.format"Failed to upload: %s to: %s. Reason: %s"path,
                                                                      nameerror.getMessage() ), error );
                                         formatResponseerrorrequest );
                                         return;
                                     }
                                     .info"Creating Path: {}\nin: {}:{}"pathstname );
                                     InputStream stream = null;
                                     try
                                     {
                                         stream = new FileInputStreamworkFile );
                                         final Transfer stored = .storestnamepathstream );
                                         .info"Stored: {}"stored );
                                         final StoreKey storageKey = LocationUtils.getKeystored );
                                         .info"Key for storage location: {}"storageKey );
                                         formatCreatedResponserequestst.singularEndpointName(),
                                                                storageKey.getName(), stored.getPath() ).end();
                                     }
                                     catch ( final AproxWorkflowException | IOException e )
                                     {
                                         .error( String.format"Failed to upload: %s to: %s. Reason: %s"path,
                                                                      namee.getMessage() ), e );
                                         formatResponseerequest );
                                     }
                                     finally
                                     {
                                         closeQuietlystream );
                                         workFile.delete();
                                     }
                                 }
                             } );
                         }
                     } );
                     pump.start();
                     request.resume();
                 }
             } );
    }
    @Routes( { @Route( path = "/:type=(hosted|remote|group)/:name:?path=(/.+)", method = ., fork = false ) } )
    public void doDeletefinal HttpServerRequest request )
    {
        //        request.pause();
        //        request.bodyHandler( new Handler<Buffer>()
        //        {
        //            @Override
        //            public void handle( final Buffer event )
        //            {
        final String name = request.params()
                                   .get..key() );
        final String path = request.params()
                                   .get..key() );
        final String type = request.params()
                                   .get..key() );
        final StoreType st = StoreType.gettype );
        try
        {
            final ApplicationStatus result = .deletestnamepath );
            request.response()
                   .setStatusCoderesult.code() )
                   .setStatusMessageresult.message() );
        }
        catch ( final AproxWorkflowException e )
        {
            .error( String.format"Failed to delete artifact: %s from: %s. Reason: %s"pathname,
                                         e.getMessage() ), e );
            formatResponseerequest );
        }
        //            }
        //        } );
        //        request.resume();
    }
    @Routes( { @Route( path = "/:type=(hosted|remote|group)/:name:path=(/.*)", method = ., fork = false ) } )
    public void doHeadfinal HttpServerRequest request )
    {
        //        request.pause();
        //        request.bodyHandler( new Handler<Buffer>()
        //        {
        //            @Override
        //            public void handle( final Buffer event )
        //            {
        final String name = request.params()
                                   .get..key() );
        final String path = request.params()
                                   .get..key() );
        final String type = request.params()
                                   .get..key() );
        final StoreType st = StoreType.gettype );
        final String standardAccept = VertxRequestUtils.getStandardAcceptrequest. );
        String givenAccept = request.headers()
                                    .get..header() );
        if ( givenAccept == null )
        {
            givenAccept = standardAccept;
        }
        try
        {
            final String baseUri = request.params()
                                          .get.key() );
            if ( path.equals"" ) || path.endsWith"/" ) || path.endsWith ) )
            {
                .info"Getting listing at: {}"path );
                final String content =
                    .renderListingstandardAcceptstnamepathbaseUri );
                request.response()
                       .putHeader..key(), givenAccept )
                       .putHeader..key(), Long.toStringcontent.length() ) )
                       .putHeader..key(), RequestUtils.formatDateHeadernew Date() ) )
                       .end();
                request.response()
                       .close();
            }
            else
            {
                final Transfer item = .getstnamepath );
                final String contentType = .getContentTypepath );
                request.response()
                       .putHeader..key(), contentType )
                       .putHeader..key(), Long.toStringitem.getDetachedFile()
                                                                                              .length() ) )
                       .putHeader..key(),
                                   RequestUtils.formatDateHeaderitem.getDetachedFile()
                                                                      .lastModified() ) )
                       .end();
                request.response()
                       .close();
            }
        }
        catch ( final AproxWorkflowException e )
        {
            .error( String.format"Failed to download artifact: %s from: %s. Reason: %s"pathname,
                                         e.getMessage() ), e );
            formatResponseerequest );
        }
        //            }
        //        } );
        //
        //        request.resume();
    }
    @Routes( { @Route( path = "/:type=(hosted|remote|group)/:name:path=(/.*)", method = . ) } )
    public void doGetfinal HttpServerRequest request )
    {
        //        request.resume();
        //        request.bodyHandler( new Handler<Buffer>()
        //        {
        //            @Override
        //            public void handle( final Buffer event )
        //            {
        final String name = request.params()
                                   .get..key() );
        final String path = request.params()
                                   .get..key() );
        final String type = request.params()
                                   .get..key() );
        final StoreType st = StoreType.gettype );
        final String standardAccept = VertxRequestUtils.getStandardAcceptrequest. );
        String givenAccept = request.headers()
                                    .get..header() );
        if ( givenAccept == null )
        {
            givenAccept = standardAccept;
        }
        .info"User asked for: {}\nStandard accept header for that is: {}"givenAcceptstandardAccept );
        try
        {
            final String baseUri = request.params()
                                          .get.key() );
            if ( path.equals"" ) || path.endsWith"/" ) || path.endsWith ) )
            {
                //                logger.info( "Redirecting to index.html under: {}", path );
                //                formatRedirect( request, uriFormatter.formatAbsolutePathTo( baseUri, getStoreType().singularEndpointName(), name, path, LISTING_FILE ) );
                //            }
                //            else if ( path.endsWith( LISTING_FILE ) )
                //            {
                .info"Getting listing at: {}"path );
                final String content =
                    .renderListingstandardAcceptstnamepathbaseUri );
                formatOkResponseWithEntityrequestcontentgivenAccept );
            }
            else
            {
                final Transfer item = .getstnamepath );
                if ( item.isDirectory()
                    || ( path.lastIndexOf'.' ) < path.lastIndexOf'/' ) && .isHtmlContentitem ) ) )
                {
                    item.deletefalse );
                    .info"Getting listing at: {}"path + "/" );
                    final String content =
                        .renderListingstandardAcceptstnamepath + "/"baseUri );
                    formatOkResponseWithEntityrequestcontentgivenAccept );
                }
                else
                {
                    final String contentType = .getContentTypepath );
                    item.touch();
                    request.resume()
                           .response()
                           .putHeader..key(), contentType )
                           .sendFileitem.getDetachedFile()
                                          .getCanonicalPath() )
                           .close();
                }
            }
        }
        catch ( final AproxWorkflowException e )
        {
            .error( String.format"Failed to download artifact: %s from: %s. Reason: %s"pathname,
                                         e.getMessage() ), e );
            formatResponseerequest );
        }
        catch ( final IOException e )
        {
            .error( String.format"Failed to download artifact: %s from: %s. Reason: %s"pathname,
                                         e.getMessage() ), e );
            formatResponseerequest );
        }
        //            }
        //        } );
        //
        //        request.resume();
    }
New to GrepCode? Check out our FAQ X