Search in sources :

Example 1 with IFileRangeSpecification

use of org.eclipse.ecf.filetransfer.IFileRangeSpecification in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method setRequestHeaderValues.

protected void setRequestHeaderValues() throws InvalidFileRangeSpecificationException {
    final IFileRangeSpecification rangeSpec = getFileRangeSpecification();
    if (rangeSpec != null) {
        final long startPosition = rangeSpec.getStartPosition();
        final long endPosition = rangeSpec.getEndPosition();
        if (startPosition < 0)
            throw new InvalidFileRangeSpecificationException(Messages.HttpClientRetrieveFileTransfer_RESUME_START_POSITION_LESS_THAN_ZERO, rangeSpec);
        if (endPosition != -1L && endPosition <= startPosition)
            throw new InvalidFileRangeSpecificationException(Messages.HttpClientRetrieveFileTransfer_RESUME_ERROR_END_POSITION_LESS_THAN_START, rangeSpec);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        String rangeHeader = "bytes=" + startPosition + "-" + ((endPosition == -1L) ? "" : ("" + endPosition));
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "retrieve range header=" + rangeHeader);
        setRangeHeader(rangeHeader);
    }
    // $NON-NLS-1$
    int maxAge = Integer.getInteger("org.eclipse.ecf.http.cache.max-age", 0);
    // fix the fix for bug 249990 with bug 410813
    if (maxAge == 0) {
        // $NON-NLS-1$//$NON-NLS-2$
        getMethod.addHeader("Cache-Control", "max-age=0");
    } else if (maxAge > 0) {
        // $NON-NLS-1$//$NON-NLS-2$
        getMethod.addHeader("Cache-Control", "max-age=" + maxAge);
    }
    setRequestHeaderValuesFromOptions();
}
Also used : IFileRangeSpecification(org.eclipse.ecf.filetransfer.IFileRangeSpecification) InvalidFileRangeSpecificationException(org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException)

Example 2 with IFileRangeSpecification

use of org.eclipse.ecf.filetransfer.IFileRangeSpecification in project ecf by eclipse.

the class UrlConnectionRetrieveFileTransfer method setRequestHeaderValues.

protected void setRequestHeaderValues() throws InvalidFileRangeSpecificationException {
    final IFileRangeSpecification rangeSpec = getFileRangeSpecification();
    if (rangeSpec != null && isHTTP()) {
        final long startPosition = rangeSpec.getStartPosition();
        final long endPosition = rangeSpec.getEndPosition();
        if (startPosition < 0)
            throw new InvalidFileRangeSpecificationException(Messages.UrlConnectionRetrieveFileTransfer_RESUME_START_POSITION_LESS_THAN_ZERO, rangeSpec);
        if (endPosition != -1L && endPosition <= startPosition)
            throw new InvalidFileRangeSpecificationException(Messages.UrlConnectionRetrieveFileTransfer_RESUME_ERROR_END_POSITION_LESS_THAN_START, rangeSpec);
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        setRangeHeader("bytes=" + startPosition + "-" + ((endPosition == -1L) ? "" : ("" + endPosition)));
    }
    // Add http 1.1 'Connection: close' header in order to potentially avoid
    // server issue described here
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=234916#c13
    // See bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247197
    // also see http 1.1 rfc section 14-10 in
    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    // $NON-NLS-1$ //$NON-NLS-2$
    urlConnection.setRequestProperty("Connection", "close");
    // $NON-NLS-1$
    int maxAge = Integer.getInteger("org.eclipse.ecf.http.cache.max-age", 0).intValue();
    // fix the fix for bug 249990 with bug 410813
    if (maxAge == 0) {
        // $NON-NLS-1$//$NON-NLS-2$
        urlConnection.setRequestProperty("Cache-Control", "max-age=0");
    } else if (maxAge > 0) {
        // $NON-NLS-1$//$NON-NLS-2$
        urlConnection.setRequestProperty("Cache-Control", "max-age=" + maxAge);
    }
    setRequestHeaderValuesFromOptions();
}
Also used : IFileRangeSpecification(org.eclipse.ecf.filetransfer.IFileRangeSpecification) InvalidFileRangeSpecificationException(org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException)

Example 3 with IFileRangeSpecification

use of org.eclipse.ecf.filetransfer.IFileRangeSpecification in project ecf by eclipse.

the class URLPartialRetrieveTest method testReceiveHttp.

protected void testReceiveHttp(final long start, final long end, String url) throws Exception {
    assertNotNull(transferInstance);
    final IFileTransferListener listener = new IFileTransferListener() {

        public void handleTransferEvent(IFileTransferEvent event) {
            if (event instanceof IIncomingFileTransferReceiveResumedEvent) {
                try {
                    final IIncomingFileTransferReceiveResumedEvent rse = (IIncomingFileTransferReceiveResumedEvent) event;
                    session = rse.receive(outs);
                } catch (final Exception e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveStartEvent) {
                final IIncomingFileTransferReceiveStartEvent rse = (IIncomingFileTransferReceiveStartEvent) event;
                try {
                    outs = new FileOutputStream(FILENAME);
                    session = rse.receive(outs);
                    pausable = (IFileTransferPausable) session.getAdapter(IFileTransferPausable.class);
                    if (pausable == null)
                        fail("pausable is null");
                } catch (final IOException e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveDataEvent) {
                System.out.println("data=" + event);
            } else if (event instanceof IIncomingFileTransferReceivePausedEvent) {
                System.out.println("paused=" + event);
            } else if (event instanceof IIncomingFileTransferReceiveDoneEvent) {
                closeOutputStream();
                System.out.println("done=" + event);
                synchronized (notify) {
                    isDone = true;
                    notify.notify();
                }
                session = ((IIncomingFileTransferReceiveDoneEvent) event).getSource();
            }
        }
    };
    final IFileID fileID = FileIDFactory.getDefault().createFileID(transferInstance.getRetrieveNamespace(), url);
    IFileRangeSpecification rangeSpecification = null;
    if (start != -1) {
        rangeSpecification = new IFileRangeSpecification() {

            public long getEndPosition() {
                return end;
            }

            public long getStartPosition() {
                return start;
            }
        };
    }
    transferInstance.sendRetrieveRequest(fileID, rangeSpecification, listener, null);
    if (!isDone) {
        synchronized (notify) {
            notify.wait();
        }
    }
    final Exception e = session.getException();
    if (e != null)
        throw e;
    incomingFile = new File(FILENAME);
    final long fileLength = incomingFile.length();
    final long bytesReceived = session.getBytesReceived();
    System.out.println("start=" + start);
    System.out.println("end=" + end);
    System.out.println("bytes received=" + bytesReceived);
    System.out.println("fileLength=" + fileLength);
    if (start != -1) {
        assertTrue(fileLength == bytesReceived);
        if (end != -1) {
            assertTrue(fileLength == (end - start + 1));
        }
    }
}
Also used : IIncomingFileTransferReceiveResumedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent) IFileRangeSpecification(org.eclipse.ecf.filetransfer.IFileRangeSpecification) IFileTransferPausable(org.eclipse.ecf.filetransfer.IFileTransferPausable) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IOException(java.io.IOException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) IFileID(org.eclipse.ecf.filetransfer.identity.IFileID) FileOutputStream(java.io.FileOutputStream) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IIncomingFileTransferReceivePausedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent) IIncomingFileTransferReceiveDataEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent) File(java.io.File)

Aggregations

IFileRangeSpecification (org.eclipse.ecf.filetransfer.IFileRangeSpecification)3 InvalidFileRangeSpecificationException (org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)1 IFileTransferPausable (org.eclipse.ecf.filetransfer.IFileTransferPausable)1 IncomingFileTransferException (org.eclipse.ecf.filetransfer.IncomingFileTransferException)1 IFileTransferEvent (org.eclipse.ecf.filetransfer.events.IFileTransferEvent)1 IIncomingFileTransferReceiveDataEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent)1 IIncomingFileTransferReceiveDoneEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)1 IIncomingFileTransferReceivePausedEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent)1 IIncomingFileTransferReceiveResumedEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent)1 IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)1 IFileID (org.eclipse.ecf.filetransfer.identity.IFileID)1