Search in sources :

Example 1 with HttpException

use of dmg.util.HttpException in project dcache by dCache.

the class HttpPoolRequestHandler method doOnPut.

@Override
protected ChannelFuture doOnPut(ChannelHandlerContext context, HttpRequest request) {
    NettyTransferService<HttpProtocolInfo>.NettyMoverChannel file = null;
    Exception exception = null;
    if (isBadRequest(request)) {
        return context.newSucceededFuture();
    }
    try {
        checkContentHeader(request.headers().names(), SUPPORTED_CONTENT_HEADERS);
        file = open(request, true);
        if (!file.getIoMode().contains(StandardOpenOption.WRITE)) {
            throw new HttpException(METHOD_NOT_ALLOWED.code(), "Resource is not open for writing");
        }
        contentMd5Checksum(request).ifPresent(file::addChecksum);
        OptionalLong contentLength = contentLength(request);
        if (contentLength.isPresent()) {
            file.truncate(contentLength.getAsLong());
        } else if (file.getFileAttributes().isDefined(FileAttribute.SIZE)) {
            file.truncate(file.getFileAttributes().getSize());
        }
        file.getProtocolInfo().getWantedChecksum().ifPresent(file::addChecksumType);
        _wantedDigest = wantDigest(request).flatMap(Checksums::parseWantDigest);
        _wantedDigest.ifPresent(file::addChecksumType);
        if (is100ContinueExpected(request)) {
            context.writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        }
        _writeChannel = file;
        file = null;
        return null;
    } catch (Redirect e) {
        exception = e;
        return context.writeAndFlush(e.createResponse());
    } catch (HttpException e) {
        exception = e;
        return context.writeAndFlush(createErrorResponse(HttpResponseStatus.valueOf(e.getErrorCode()), e.getMessage()));
    } catch (URISyntaxException e) {
        exception = e;
        return context.writeAndFlush(createErrorResponse(BAD_REQUEST, "URI is not valid: " + e.getMessage()));
    } catch (IllegalArgumentException e) {
        exception = e;
        return context.writeAndFlush(createErrorResponse(BAD_REQUEST, e.getMessage()));
    } catch (IOException | RuntimeException e) {
        exception = e;
        return context.writeAndFlush(createErrorResponse(INTERNAL_SERVER_ERROR, e.getMessage()));
    } finally {
        if (file != null) {
            file.release(exception);
            _files.remove(file);
        }
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NettyTransferService(org.dcache.pool.movers.NettyTransferService) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) OutOfDiskException(org.dcache.pool.repository.OutOfDiskException) HttpException(dmg.util.HttpException) CacheException(diskCacheV111.util.CacheException) FileCorruptedCacheException(diskCacheV111.util.FileCorruptedCacheException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) OptionalLong(java.util.OptionalLong) HttpException(dmg.util.HttpException)

Example 2 with HttpException

use of dmg.util.HttpException in project dcache by dCache.

the class HttpPoolRequestHandler method doOnGet.

/**
 * Single GET operation.
 * <p>
 * Finds the correct mover channel using the UUID in the GET. Range queries are supported. The
 * file will be sent to the remote peer in chunks to avoid server side memory issues.
 */
@Override
protected ChannelFuture doOnGet(ChannelHandlerContext context, HttpRequest request) {
    NettyTransferService<HttpProtocolInfo>.NettyMoverChannel file;
    List<HttpByteRange> ranges;
    long fileSize;
    if (isBadRequest(request)) {
        return context.newSucceededFuture();
    }
    try {
        file = open(request, false);
        if (file.getIoMode().contains(StandardOpenOption.WRITE)) {
            throw new HttpException(METHOD_NOT_ALLOWED.code(), "Resource is not open for reading");
        }
        fileSize = file.size();
        ranges = parseHttpRange(request, 0, fileSize - 1);
    } catch (Redirect e) {
        return context.writeAndFlush(e.createResponse());
    } catch (HttpException e) {
        return context.writeAndFlush(createErrorResponse(e.getErrorCode(), e.getMessage()));
    } catch (URISyntaxException e) {
        return context.writeAndFlush(createErrorResponse(BAD_REQUEST, "URI not valid: " + e.getMessage()));
    } catch (IllegalArgumentException e) {
        return context.writeAndFlush(createErrorResponse(BAD_REQUEST, e.getMessage()));
    } catch (IOException e) {
        return context.writeAndFlush(createErrorResponse(INTERNAL_SERVER_ERROR, e.getMessage()));
    }
    Optional<String> digest = wantDigest(request).flatMap(h -> Checksums.digestHeader(h, file.getFileAttributes()));
    if (ranges == null || ranges.isEmpty()) {
        /*
             * GET for a whole file
             */
        context.write(new HttpGetResponse(fileSize, file, digest)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        context.write(read(file, 0, fileSize - 1)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        ChannelFuture writeAndFlush = context.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        // Release the file immediately after supplying all of the file's content.  We're
        // assuming that the client will not make further requests against this URL.  This is
        // done to send the DoorTransferFinishedMessage in a timely fashion.
        writeAndFlush.addListener(f -> file.release());
        return writeAndFlush;
    } else if (ranges.size() == 1) {
        /* RFC 2616: 14.16. A response to a request for a single range
             * MUST NOT be sent using the multipart/byteranges media type.
             */
        HttpByteRange range = ranges.get(0);
        context.write(new HttpPartialContentResponse(range.getLower(), range.getUpper(), fileSize, digest)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        context.write(read(file, range.getLower(), range.getUpper())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        // client will not make further requests against this URL.
        return context.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    } else {
        /*
             * GET for multiple ranges
             */
        long totalLen = 0;
        ByteBuf[] fragmentMarkers = new ByteBuf[ranges.size()];
        for (int i = 0; i < ranges.size(); i++) {
            HttpByteRange range = ranges.get(i);
            long upper = range.getUpper();
            long lower = range.getLower();
            totalLen += upper - lower + 1;
            ByteBuf buffer = fragmentMarkers[i] = createMultipartFragmentMarker(lower, upper, fileSize);
            totalLen += buffer.readableBytes();
        }
        ByteBuf endMarker = createMultipartEnd();
        totalLen += endMarker.readableBytes();
        context.write(new HttpMultipartResponse(digest, totalLen)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        for (int i = 0; i < ranges.size(); i++) {
            HttpByteRange range = ranges.get(i);
            context.write(fragmentMarkers[i]).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
            context.write(read(file, range.getLower(), range.getUpper())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        }
        // client will not make further requests against this URL.
        return context.writeAndFlush(new DefaultLastHttpContent(endMarker));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NettyTransferService(org.dcache.pool.movers.NettyTransferService) HttpByteRange(diskCacheV111.util.HttpByteRange) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StringMarkup.quotedString(org.dcache.util.StringMarkup.quotedString) ByteBuf(io.netty.buffer.ByteBuf) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HttpException(dmg.util.HttpException)

Example 3 with HttpException

use of dmg.util.HttpException in project dcache by dCache.

the class HttpBillingEngine method printMainStatisticsPage.

private void printMainStatisticsPage(OutputStream out) throws HttpException {
    HTMLWriter html = new HTMLWriter(out, _context);
    try {
        Object[][] x = _billing.sendAndWait("get billing info", Object[][].class);
        html.addHeader("/styles/billing.css", "dCache Billing");
        printTotalStatistics(html, x);
        try {
            Map<String, long[]> map = _billing.sendAndWait("get pool statistics", Map.class);
            printPoolStatistics(html, map, null);
        } catch (InterruptedException | TimeoutCacheException e) {
            throw e;
        } catch (CacheException e) {
            html.print("<p class=\"error\">This 'billingCell' doesn't support: 'get pool statistics':");
            html.print("<blockquote><pre>" + e + "</pre></blockquote>");
        }
    } catch (NoRouteToCellException e) {
        throw new HttpException(500, "No connection to billing");
    } catch (TimeoutCacheException e) {
        throw new HttpException(500, "Request Timed Out");
    } catch (InterruptedException | CacheException e) {
        throw new HttpException(500, "Problem : " + e.getMessage());
    } finally {
        html.addFooter(getClass().getName());
    }
}
Also used : HTMLWriter(diskCacheV111.util.HTMLWriter) TimeoutCacheException(diskCacheV111.util.TimeoutCacheException) CacheException(diskCacheV111.util.CacheException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) HttpException(dmg.util.HttpException) TimeoutCacheException(diskCacheV111.util.TimeoutCacheException)

Example 4 with HttpException

use of dmg.util.HttpException in project dcache by dCache.

the class HttpPoolMgrEngineV3 method printConfigurationPages.

private void printConfigurationPages(PrintWriter pw, String[] urlItems, HttpRequest request) throws HttpException, NoRouteToCellException, InterruptedException {
    printConfigurationHeader(pw);
    printPoolManagerHeader(pw, null);
    if (urlItems.length < 2) {
        showDirectory(pw);
    } else if (urlItems[1].equals("pools")) {
        showDirectory(pw, 1);
        queryPool(pw, urlItems[2]);
    } else if (urlItems[1].equals("units")) {
        showDirectory(pw, 3);
        StringBuilder sb = new StringBuilder();
        int i;
        for (i = 2; i < (urlItems.length - 1); i++) {
            sb.append(urlItems[i]).append("/");
        }
        sb.append(urlItems[i]);
        queryUnit(pw, sb.toString());
    } else if (urlItems[1].equals("ugroups")) {
        showDirectory(pw, 4);
        queryUnitGroup(pw, urlItems[2]);
    } else if (urlItems[1].equals("pgroups")) {
        showDirectory(pw, 2);
        queryPoolGroup(pw, urlItems[2]);
    } else if (urlItems[1].equals("links")) {
        showDirectory(pw, 5);
        queryLink(pw, urlItems[2]);
    } else if (urlItems[1].equals("linklist")) {
        showDirectory(pw, 7);
        queryLinkList(pw);
    } else if (urlItems[1].equals("match")) {
        showDirectory(pw, 6);
        showMatch(pw, request);
    } else {
        throw new HttpException(404, "Unknown key : " + urlItems[1]);
    }
}
Also used : HttpException(dmg.util.HttpException) CellEndpoint(dmg.cells.nucleus.CellEndpoint)

Example 5 with HttpException

use of dmg.util.HttpException in project dcache by dCache.

the class HandlerDelegator method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
    String uri = null;
    String alias;
    AliasEntry entry = null;
    try {
        uri = request.getRequestURI();
        alias = extractAlias(uri);
        LOGGER.debug("handle {}, {}", uri, alias);
        entry = aliases.get(alias);
        if (entry == null) {
            entry = aliases.get("<default>");
        }
        if (entry == null) {
            throw new HttpException(HttpServletResponse.SC_NOT_FOUND, "Alias not found : " + alias);
        }
        LOGGER.debug("alias: {}, entry {}", alias, entry);
        /*
             * Exclusion of POST is absolute.
             */
        if (!request.getMethod().equals("GET")) {
            throw new HttpException(HttpServletResponse.SC_NOT_IMPLEMENTED, "Method not implemented: " + request.getMethod());
        }
        /*
             * check for overwritten alias
             */
        final String alternate = entry.getOverwrite();
        if (alternate != null) {
            LOGGER.debug("handle, overwritten alias: {}", alternate);
            final AliasEntry overwrittenEntry = aliases.get(alternate);
            if (overwrittenEntry != null) {
                entry = overwrittenEntry;
            }
            LOGGER.debug("handle, alias {}, entry {}", alternate, entry);
        }
        final Handler handler = entry.getHandler();
        LOGGER.debug("got handler: {}", handler);
        if (handler != null) {
            handler.handle(target, baseRequest, request, response);
        }
    } catch (final Exception e) {
        if (entry != null && e.getCause() instanceof OnErrorException) {
            final String alternate = entry.getOnError();
            if (alternate != null) {
                LOGGER.debug("handle, onError alias: {}", alternate);
                final AliasEntry overwrittenEntry = aliases.get(alternate);
                if (overwrittenEntry != null) {
                    entry = overwrittenEntry;
                    LOGGER.debug("handle, alias {}, entry {}", alternate, entry);
                    final Handler handler = entry.getHandler();
                    if (handler != null) {
                        try {
                            handler.handle(target, baseRequest, request, response);
                        } catch (final ServletException t) {
                            handleException(t, uri, response);
                        }
                    }
                } else {
                    handleException(new HttpException(HttpServletResponse.SC_NOT_FOUND, "Not found : " + entry.getSpecificString()), uri, response);
                }
            }
        } else {
            handleException(e, uri, response);
        }
    }
    LOGGER.info("Finished");
}
Also used : OnErrorException(org.dcache.services.httpd.exceptions.OnErrorException) ServletException(javax.servlet.ServletException) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpException(dmg.util.HttpException) AliasEntry(org.dcache.services.httpd.util.AliasEntry) HttpBasicAuthenticationException(dmg.util.HttpBasicAuthenticationException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpException(dmg.util.HttpException) OnErrorException(org.dcache.services.httpd.exceptions.OnErrorException)

Aggregations

HttpException (dmg.util.HttpException)16 IOException (java.io.IOException)7 CacheException (diskCacheV111.util.CacheException)5 URISyntaxException (java.net.URISyntaxException)5 TimeoutCacheException (diskCacheV111.util.TimeoutCacheException)3 NoRouteToCellException (dmg.cells.nucleus.NoRouteToCellException)3 OutputStream (java.io.OutputStream)3 FileCorruptedCacheException (diskCacheV111.util.FileCorruptedCacheException)2 HTMLWriter (diskCacheV111.util.HTMLWriter)2 HttpByteRange (diskCacheV111.util.HttpByteRange)2 HttpRequest (dmg.util.HttpRequest)2 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelFuture (io.netty.channel.ChannelFuture)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)2 ServletException (javax.servlet.ServletException)2 NettyTransferService (org.dcache.pool.movers.NettyTransferService)2 CharMatcher (com.google.common.base.CharMatcher)1 HashMultiset (com.google.common.collect.HashMultiset)1 ImmutableList (com.google.common.collect.ImmutableList)1