Example usage for java.util NoSuchElementException NoSuchElementException

List of usage examples for java.util NoSuchElementException NoSuchElementException

Introduction

In this page you can find the example usage for java.util NoSuchElementException NoSuchElementException.

Prototype

public NoSuchElementException() 

Source Link

Document

Constructs a NoSuchElementException with null as its error message string.

Usage

From source file:com.opengamma.util.timeseries.fast.integer.object.FastArrayIntObjectTimeSeries.java

@Override
public T getValueFast(final int time) {
    final int binarySearch = Arrays.binarySearch(_times, time);
    if (binarySearch >= 0 && _times[binarySearch] == time) {
        return _values[binarySearch];
    } else {//from   w  w  w . j  a v a  2s .  co m
        throw new NoSuchElementException();
    }
}

From source file:net.sf.nmedit.jpatch.impl.PBasicConnectionManager.java

public Iterator<PConnection> iterator() {
    return new Iterator<PConnection>() {
        Iterator<Node> iter = nodemap.values().iterator();
        Node next;/*from w w  w.j a va2 s  .c  om*/
        int expectedModCount = modCount;

        void align() {
            if (next == null) {
                Node n;
                while (iter.hasNext()) {
                    n = iter.next();
                    if (n.p != null) {
                        next = n;
                        break;
                    }
                }
            }
        }

        public boolean hasNext() {
            align();
            return next != null;
        }

        public PConnection next() {
            if (expectedModCount != modCount)
                throw new ConcurrentModificationException();

            if (!hasNext())
                throw new NoSuchElementException();
            PConnection c = new PConnection(next.c, next.parent());
            next = null;
            return c;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:edu.cornell.med.icb.goby.alignments.AlignmentReaderImpl.java

/**
 * Returns the next alignment entry from the input stream.
 *
 * @return the alignment read entry from the input stream.
 *//*from w  w  w .  ja  va 2 s .c o  m*/
public Alignments.AlignmentEntry next() {
    // System.out.println("next");
    if (!hasNext()) {
        throw new NoSuchElementException();
    }
    try {

        if (!nextEntry.hasMultiplicity()) {
            // set the default multiplicity when the field was not defined.
            Alignments.AlignmentEntry.Builder builder = Alignments.AlignmentEntry.newBuilder(nextEntry);
            builder.setMultiplicity(1);
            nextEntry = builder.build();
        }
        return nextEntry;

    } finally {

        nextEntry = null;
    }

}

From source file:com.opengamma.util.timeseries.fast.longint.object.FastArrayLongObjectTimeSeries.java

@Override
public T getValueFast(final long time) {
    final int binarySearch = Arrays.binarySearch(_times, time);
    if (binarySearch >= 0 && _times[binarySearch] == time) {
        return _values[binarySearch];
    } else {//from  w  ww  . ja v a  2  s . com
        throw new NoSuchElementException();
    }
}

From source file:com.opengamma.util.timeseries.fast.longint.FastArrayLongDoubleTimeSeries.java

@Override
public double getValueFast(final long time) {
    final int binarySearch = Arrays.binarySearch(_times, time);
    if (binarySearch >= 0 && _times[binarySearch] == time) {
        return _values[binarySearch];
    } else {//from   www . jav  a2s .  c  om
        throw new NoSuchElementException();
    }
}

From source file:StringTokenizer.java

/**
 * Returns the next token from this string tokenizer.
 * <p>//from  w ww. ja va  2  s.  c  o  m
 * The current position is set after the token returned.
 *
 * @return the next token from this string tokenizer.
 * @throws NoSuchElementException if there are no more tokens in this tokenizer's string.
 *
 * @since ostermillerutils 1.00.00
 */
public String nextToken() {
    int workingPosition = position;
    boolean workingEmptyReturned = emptyReturned;
    boolean onToken = advancePosition();
    while (position != workingPosition || emptyReturned != workingEmptyReturned) {
        if (onToken) {
            // returning a token decreases the token count
            tokenCount--;
            return (emptyReturned ? ""
                    : text.substring(workingPosition, (position != -1) ? position : strLength));
        }
        workingPosition = position;
        workingEmptyReturned = emptyReturned;
        onToken = advancePosition();
    }
    throw new NoSuchElementException();
}

From source file:org.fusesource.camel.component.sap.SapRfcTestSupport.java

public void doPreSetup() throws Exception {
    super.doPreSetup();

    /* Create mocks for repository and function template */
    mockDestination = mock(JCoDestination.class);
    mockServer = mock(JCoServer.class);
    mockServerFactory = mock(JCoServerFactory.class);
    mockServerContext = mock(JCoServerContext.class);
    mockServerTIDHandler = mock(JCoServerTIDHandler.class);
    mockRepository = mock(JCoRepository.class);
    mockFunction = mock(JCoFunction.class);
    mockFunctionTemplate = mock(JCoFunctionTemplate.class);
    mockRequest = mock(JCoRequest.class);
    mockResponse = mock(JCoResponse.class);

    /* Create mocks for parameter list */
    mockImportParameterList = mock(JCoParameterList.class, "ImportParameterList");
    mockChangingParameterList = mock(JCoParameterList.class, "ChangingParameterList");
    mockExportParameterList = mock(JCoParameterList.class, "ExportParameterList");
    mockTableParameterList = mock(JCoParameterList.class, "TableParameterList");

    /* Create mock field iterator for parameter list  */
    mockEmptyParameterListFieldIterator = mock(JCoFieldIterator.class, "EmptyParameterListFieldIterator");
    mockParameterListFieldIterator = mock(JCoFieldIterator.class, "ParameterListFieldIterator");
    mockTableParameterListFieldIterator = mock(JCoFieldIterator.class, "TableParameterListFieldIterator");

    /* Create field mocks for field iterator */
    mockParameterListCharField = mock(JCoField.class, "ParameterListCharField");
    mockParameterListNumField = mock(JCoField.class, "ParameterListNumField");
    mockParameterListIntField = mock(JCoField.class, "ParameterListIntField");
    mockParameterListFloatField = mock(JCoField.class, "ParameterListFloatField");
    mockParameterListBCDField = mock(JCoField.class, "ParameterListBCDField");
    mockParameterListBinaryField = mock(JCoField.class, "ParameterListBinaryField");
    mockParameterListBinaryArrayField = mock(JCoField.class, "ParameterListBinaryArrayField");
    mockParameterListDateField = mock(JCoField.class, "ParameterListDateField");
    mockParameterListTimeField = mock(JCoField.class, "ParameterListTimeField");
    mockParameterListStringField = mock(JCoField.class, "ParameterListStringField");
    mockParameterListStructureField = mock(JCoField.class, "ParameterListStructureField");
    mockParameterListTableField = mock(JCoField.class, "ParameterListTableField");

    /* Create mock for structure */
    mockStructure = mock(JCoStructure.class, "Structure");

    /* Create mock field iterators for structure and table */
    mockStructureFieldIterator = mock(JCoFieldIterator.class, "StructureFieldIterator");
    mockTableFieldIterator = mock(JCoFieldIterator.class, "TableFieldIterator");

    /* Create field mocks for field iterator */
    mockCharField = mock(JCoField.class, "CharField");
    mockNumField = mock(JCoField.class, "NumField");
    mockIntField = mock(JCoField.class, "IntField");
    mockFloatField = mock(JCoField.class, "FloatField");
    mockBCDField = mock(JCoField.class, "BCDField");
    mockBinaryField = mock(JCoField.class, "BinaryField");
    mockBinaryArrayField = mock(JCoField.class, "BinaryArrayField");
    mockDateField = mock(JCoField.class, "DateField");
    mockTimeField = mock(JCoField.class, "TimeField");
    mockStringField = mock(JCoField.class, "StringField");

    mockTable = mock(JCoTable.class, "Table");

    /* Create mocks for parameter list meta data */
    mockImportParameterListMetaData = mock(JCoListMetaData.class, "ImportParameterListMetaData");
    mockChangingParameterListMetaData = mock(JCoListMetaData.class, "ChangingParameterListMetaData");
    mockTableParameterListMetaData = mock(JCoListMetaData.class, "TableParameterListMetaData");
    mockExportParameterListMetaData = mock(JCoListMetaData.class, "ExportParameterListMetaData");

    /* Create mocks for structure meta data */
    mockStructureMetaData = mock(JCoRecordMetaData.class, "StructureMetaData");

    /* Enhance destination mock */
    when(mockDestination.getRepository()).thenReturn(mockRepository);
    when(mockDestination.createTID()).thenReturn(TEST_TID);

    /* Enhance server mock */
    when(mockServer.getRepositoryDestination()).thenReturn(DESTINATION_NAME);
    when(mockServer.getProgramID()).thenReturn(SERVER_PROGRAM_ID);
    when(mockServer.getCallHandlerFactory()).thenReturn(new FunctionHandlerFactory());
    when(mockServer.getTIDHandler()).thenReturn(mockServerTIDHandler);

    /* Enhance server context mock */
    when(mockServerContext.getRepository()).thenReturn(mockRepository);
    when(mockServerContext.getServer()).thenReturn(mockServer);

    /* Enhance repository mock */
    when(mockRepository.getFunction(FUNCTION_MODULE_NAME)).thenReturn(mockFunction);
    when(mockRepository.getFunctionTemplate(FUNCTION_MODULE_NAME)).thenReturn(mockFunctionTemplate);
    when(mockRepository.getName()).thenReturn(REPOSITORY_NAME);

    /* Enhance function mock */
    when(mockFunction.getName()).thenReturn(FUNCTION_MODULE_NAME);
    when(mockFunction.getImportParameterList()).thenReturn(mockImportParameterList);
    when(mockFunction.getChangingParameterList()).thenReturn(mockChangingParameterList);
    when(mockFunction.getExportParameterList()).thenReturn(mockExportParameterList);
    when(mockFunction.getTableParameterList()).thenReturn(mockTableParameterList);

    /* Enhance function template mock */
    when(mockFunctionTemplate.getImportParameterList()).thenReturn(mockImportParameterListMetaData);
    when(mockFunctionTemplate.getChangingParameterList()).thenReturn(mockChangingParameterListMetaData);
    when(mockFunctionTemplate.getExportParameterList()).thenReturn(mockExportParameterListMetaData);
    when(mockFunctionTemplate.getTableParameterList()).thenReturn(mockTableParameterListMetaData);

    /* Enhance request/response mock */
    when(mockRequest.getFieldIterator()).thenReturn(mockParameterListFieldIterator);
    when(mockResponse.getFieldIterator()).thenReturn(mockParameterListFieldIterator);

    /* Enhance parameter list mock */
    when(mockImportParameterList.getFieldIterator()).thenReturn(mockEmptyParameterListFieldIterator);
    when(mockChangingParameterList.getFieldIterator()).thenReturn(mockParameterListFieldIterator);
    when(mockExportParameterList.getFieldIterator()).thenReturn(mockEmptyParameterListFieldIterator);
    when(mockTableParameterList.getFieldIterator()).thenReturn(mockTableParameterListFieldIterator);

    /* Enhance field iterator mocks  */
    when(mockEmptyParameterListFieldIterator.hasNextField()).thenReturn(false);
    when(mockParameterListFieldIterator.hasNextField()).thenReturn(true, true, true, true, true, true, true,
            true, true, true, true, false, true, true, true, true, true, true, true, true, true, true, true,
            false);// w  ww  . j a v  a  2s.co m
    when(mockParameterListFieldIterator.nextField()).thenReturn(mockParameterListCharField,
            mockParameterListNumField, mockParameterListIntField, mockParameterListFloatField,
            mockParameterListBCDField, mockParameterListBinaryField, mockParameterListBinaryArrayField,
            mockParameterListDateField, mockParameterListTimeField, mockParameterListStringField,
            mockParameterListStructureField, mockParameterListCharField, mockParameterListNumField,
            mockParameterListIntField, mockParameterListFloatField, mockParameterListBCDField,
            mockParameterListBinaryField, mockParameterListBinaryArrayField, mockParameterListDateField,
            mockParameterListTimeField, mockParameterListStringField, mockParameterListStructureField)
            .thenThrow(new NoSuchElementException());

    when(mockTableParameterListFieldIterator.hasNextField()).thenReturn(true, false, true, false);
    when(mockTableParameterListFieldIterator.nextField())
            .thenReturn(mockParameterListTableField, mockParameterListTableField)
            .thenThrow(new NoSuchElementException());

    when(mockStructureFieldIterator.hasNextField()).thenReturn(true, true, true, true, true, true, true, true,
            true, true, false, true, true, true, true, true, true, true, true, true, true, false);
    when(mockStructureFieldIterator.nextField())
            .thenReturn(mockCharField, mockNumField, mockIntField, mockFloatField, mockBCDField,
                    mockBinaryField, mockBinaryArrayField, mockDateField, mockTimeField, mockStringField,
                    mockCharField, mockNumField, mockIntField, mockFloatField, mockBCDField, mockBinaryField,
                    mockBinaryArrayField, mockDateField, mockTimeField, mockStringField)
            .thenThrow(new NoSuchElementException());

    when(mockTableFieldIterator.hasNextField()).thenReturn(true, true, true, true, true, true, true, true, true,
            true, false, true, true, true, true, true, true, true, true, true, true, false);
    when(mockTableFieldIterator.nextField())
            .thenReturn(mockCharField, mockNumField, mockIntField, mockFloatField, mockBCDField,
                    mockBinaryField, mockBinaryArrayField, mockDateField, mockTimeField, mockStringField,
                    mockCharField, mockNumField, mockIntField, mockFloatField, mockBCDField, mockBinaryField,
                    mockBinaryArrayField, mockDateField, mockTimeField, mockStringField)
            .thenThrow(new NoSuchElementException());

    /* Enhance parameter list field mocks */
    when(mockParameterListCharField.getName()).thenReturn(PARAM_LIST_CHAR_PARAM);
    when(mockParameterListCharField.getValue()).thenReturn(CHAR_PARAM_OUT_VAL);

    when(mockParameterListNumField.getName()).thenReturn(PARAM_LIST_NUM_PARAM);
    when(mockParameterListNumField.getValue()).thenReturn(NUM_PARAM_OUT_VAL);

    when(mockParameterListIntField.getName()).thenReturn(PARAM_LIST_INT_PARAM);
    when(mockParameterListIntField.getValue()).thenReturn(INT_PARAM_OUT_VAL);

    when(mockParameterListFloatField.getName()).thenReturn(PARAM_LIST_FLOAT_PARAM);
    when(mockParameterListFloatField.getValue()).thenReturn(FLOAT_PARAM_OUT_VAL);

    when(mockParameterListBCDField.getName()).thenReturn(PARAM_LIST_BCD_PARAM);
    when(mockParameterListBCDField.getValue()).thenReturn(BCD_PARAM_OUT_VAL);

    when(mockParameterListBinaryField.getName()).thenReturn(PARAM_LIST_BINARY_PARAM);
    when(mockParameterListBinaryField.getValue()).thenReturn(BINARY_PARAM_OUT_VAL);

    when(mockParameterListBinaryArrayField.getName()).thenReturn(PARAM_LIST_BINARY_ARRAY_PARAM);
    when(mockParameterListBinaryArrayField.getValue()).thenReturn(BINARY_ARRAY_PARAM_OUT_VAL);

    when(mockParameterListDateField.getName()).thenReturn(PARAM_LIST_DATE_PARAM);
    when(mockParameterListDateField.getValue()).thenReturn(DATE_PARAM_OUT_VAL);

    when(mockParameterListTimeField.getName()).thenReturn(PARAM_LIST_TIME_PARAM);
    when(mockParameterListTimeField.getValue()).thenReturn(TIME_PARAM_OUT_VAL);

    when(mockParameterListStringField.getName()).thenReturn(PARAM_LIST_STRING_PARAM);
    when(mockParameterListStringField.getValue()).thenReturn(STRING_PARAM_OUT_VAL);

    when(mockParameterListStructureField.getName()).thenReturn(PARAM_LIST_STRUCTURE_PARAM);
    when(mockParameterListStructureField.isStructure()).thenReturn(true);
    when(mockParameterListStructureField.getStructure()).thenReturn(mockStructure);

    when(mockParameterListTableField.getName()).thenReturn(PARAM_LIST_TABLE_PARAM);
    when(mockParameterListTableField.isTable()).thenReturn(true);
    when(mockParameterListTableField.getTable()).thenReturn(mockTable);

    /* Enhance structure field mocks */
    when(mockCharField.getName()).thenReturn(CHAR_PARAM);
    when(mockCharField.getValue()).thenReturn(CHAR_PARAM_OUT_VAL);

    when(mockNumField.getName()).thenReturn(NUM_PARAM);
    when(mockNumField.getValue()).thenReturn(NUM_PARAM_OUT_VAL);

    when(mockIntField.getName()).thenReturn(INT_PARAM);
    when(mockIntField.getValue()).thenReturn(INT_PARAM_OUT_VAL);

    when(mockFloatField.getName()).thenReturn(FLOAT_PARAM);
    when(mockFloatField.getValue()).thenReturn(FLOAT_PARAM_OUT_VAL);

    when(mockBCDField.getName()).thenReturn(BCD_PARAM);
    when(mockBCDField.getValue()).thenReturn(BCD_PARAM_OUT_VAL);

    when(mockBinaryField.getName()).thenReturn(BINARY_PARAM);
    when(mockBinaryField.getValue()).thenReturn(BINARY_PARAM_OUT_VAL);

    when(mockBinaryArrayField.getName()).thenReturn(BINARY_ARRAY_PARAM);
    when(mockBinaryArrayField.getValue()).thenReturn(BINARY_ARRAY_PARAM_OUT_VAL);

    when(mockDateField.getName()).thenReturn(DATE_PARAM);
    when(mockDateField.getValue()).thenReturn(DATE_PARAM_OUT_VAL);

    when(mockTimeField.getName()).thenReturn(TIME_PARAM);
    when(mockTimeField.getValue()).thenReturn(TIME_PARAM_OUT_VAL);

    when(mockStringField.getName()).thenReturn(STRING_PARAM);
    when(mockStringField.getValue()).thenReturn(STRING_PARAM_OUT_VAL);

    /* Enhance structure mock */
    when(mockStructure.getFieldIterator()).thenReturn(mockStructureFieldIterator);

    /* Enhance table mock */
    when(mockTable.getNumRows()).thenReturn(1);
    when(mockTable.getFieldIterator()).thenReturn(mockTableFieldIterator);

    /* Enhance structure meta data mock */
    when(mockStructureMetaData.getName()).thenReturn(STRUCTURE_TYPE_NAME);

}

From source file:com.octo.captcha.engine.bufferedengine.buffer.DiskCaptchaBuffer.java

/**
 * @see com.octo.captcha.engine.bufferedengine.buffer.CaptchaBuffer#removeCaptcha(java.util.Locale)
 *//*w  ww  . j a  v  a  2s .  c om*/
public Captcha removeCaptcha(Locale locale) throws NoSuchElementException {
    log.debug("entering removeCaptcha(Locale locale)");

    Collection captchas = null;
    try {
        captchas = remove(1, locale);
    } catch (IOException e) {
        throw new CaptchaException(e);
    }
    if (captchas.size() == 0) {
        throw new NoSuchElementException();
    }
    return (Captcha) captchas.toArray()[0];
}

From source file:chat.viska.commons.pipelines.Pipeline.java

@SchedulerSupport(SchedulerSupport.IO)
public Maybe<Pipe> replace(final Pipe oldPipe, final Pipe newPipe) {
    return Maybe.fromCallable(() -> {
        pipeLock.writeLock().lockInterruptibly();
        try {/*from   w  ww. java  2 s . c om*/
            final ListIterator<Map.Entry<String, Pipe>> iterator = getIteratorOf(oldPipe);
            if (iterator == null) {
                throw new NoSuchElementException();
            }
            Map.Entry<String, Pipe> oldEntry = iterator.next();
            iterator.set(new AbstractMap.SimpleImmutableEntry<>(oldEntry.getKey(), newPipe));
            oldPipe.onRemovedFromPipeline(this);
            newPipe.onAddedToPipeline(this);
        } finally {
            pipeLock.writeLock().unlock();
        }
        return oldPipe;
    }).subscribeOn(Schedulers.io());
}

From source file:io.warp10.continuum.egress.EgressFetchHandler.java

@Override
public void handle(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
    boolean fromArchive = false;
    boolean splitFetch = false;
    boolean writeTimestamp = false;

    if (Constants.API_ENDPOINT_FETCH.equals(target)) {
        baseRequest.setHandled(true);//  w w  w  .j a v a  2 s .  c  o m
        fromArchive = false;
    } else if (Constants.API_ENDPOINT_AFETCH.equals(target)) {
        baseRequest.setHandled(true);
        fromArchive = true;
    } else if (Constants.API_ENDPOINT_SFETCH.equals(target)) {
        baseRequest.setHandled(true);
        splitFetch = true;
    } else if (Constants.API_ENDPOINT_CHECK.equals(target)) {
        baseRequest.setHandled(true);
        resp.setStatus(HttpServletResponse.SC_OK);
        return;
    } else {
        return;
    }

    try {
        // Labels for Sensision
        Map<String, String> labels = new HashMap<String, String>();

        labels.put(SensisionConstants.SENSISION_LABEL_TYPE, target);

        //
        // Add CORS header
        //

        resp.setHeader("Access-Control-Allow-Origin", "*");

        String start = null;
        String stop = null;

        long now = Long.MIN_VALUE;
        long timespan = 0L;

        String nowParam = null;
        String timespanParam = null;
        String dedupParam = null;
        String showErrorsParam = null;

        if (splitFetch) {
            nowParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_NOW_HEADERX));
            timespanParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_TIMESPAN_HEADERX));
            showErrorsParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_SHOW_ERRORS_HEADERX));
        } else {
            start = req.getParameter(Constants.HTTP_PARAM_START);
            stop = req.getParameter(Constants.HTTP_PARAM_STOP);

            nowParam = req.getParameter(Constants.HTTP_PARAM_NOW);
            timespanParam = req.getParameter(Constants.HTTP_PARAM_TIMESPAN);
            dedupParam = req.getParameter(Constants.HTTP_PARAM_DEDUP);
            showErrorsParam = req.getParameter(Constants.HTTP_PARAM_SHOW_ERRORS);
        }

        String maxDecoderLenParam = req.getParameter(Constants.HTTP_PARAM_MAXSIZE);
        int maxDecoderLen = null != maxDecoderLenParam ? Integer.parseInt(maxDecoderLenParam)
                : Constants.DEFAULT_PACKED_MAXSIZE;

        String suffix = req.getParameter(Constants.HTTP_PARAM_SUFFIX);
        if (null == suffix) {
            suffix = Constants.DEFAULT_PACKED_CLASS_SUFFIX;
        }

        boolean unpack = null != req.getParameter(Constants.HTTP_PARAM_UNPACK);

        long chunksize = Long.MAX_VALUE;

        if (null != req.getParameter(Constants.HTTP_PARAM_CHUNKSIZE)) {
            chunksize = Long.parseLong(req.getParameter(Constants.HTTP_PARAM_CHUNKSIZE));
        }

        if (chunksize <= 0) {
            throw new IOException("Invalid chunksize.");
        }

        boolean showErrors = null != showErrorsParam;
        boolean dedup = null != dedupParam && "true".equals(dedupParam);

        if (null != start && null != stop) {
            long tsstart = fmt.parseDateTime(start).getMillis() * Constants.TIME_UNITS_PER_MS;
            long tsstop = fmt.parseDateTime(stop).getMillis() * Constants.TIME_UNITS_PER_MS;

            if (tsstart < tsstop) {
                now = tsstop;
                timespan = tsstop - tsstart;
            } else {
                now = tsstart;
                timespan = tsstart - tsstop;
            }
        } else if (null != nowParam && null != timespanParam) {
            if ("now".equals(nowParam)) {
                now = TimeSource.getTime();
            } else {
                try {
                    now = Long.parseLong(nowParam);
                } catch (Exception e) {
                    now = fmt.parseDateTime(nowParam).getMillis() * Constants.TIME_UNITS_PER_MS;
                }
            }

            timespan = Long.parseLong(timespanParam);
        }

        if (Long.MIN_VALUE == now) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Missing now/timespan or start/stop parameters.");
            return;
        }

        String selector = splitFetch ? null : req.getParameter(Constants.HTTP_PARAM_SELECTOR);

        //
        // Extract token from header
        //

        String token = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_TOKENX));

        // If token was not found in header, extract it from the 'token' parameter
        if (null == token && !splitFetch) {
            token = req.getParameter(Constants.HTTP_PARAM_TOKEN);
        }

        String fetchSig = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_FETCH_SIGNATURE));

        //
        // Check token signature if it was provided
        //

        boolean signed = false;

        if (splitFetch) {
            // Force showErrors
            showErrors = true;
            signed = true;
        }

        if (null != fetchSig) {
            if (null != fetchPSK) {
                String[] subelts = fetchSig.split(":");
                if (2 != subelts.length) {
                    throw new IOException("Invalid fetch signature.");
                }
                long nowts = System.currentTimeMillis();
                long sigts = new BigInteger(subelts[0], 16).longValue();
                long sighash = new BigInteger(subelts[1], 16).longValue();

                if (nowts - sigts > 10000L) {
                    throw new IOException("Fetch signature has expired.");
                }

                // Recompute hash of ts:token

                String tstoken = Long.toString(sigts) + ":" + token;

                long checkedhash = SipHashInline.hash24(fetchPSK, tstoken.getBytes(Charsets.ISO_8859_1));

                if (checkedhash != sighash) {
                    throw new IOException("Corrupted fetch signature");
                }

                signed = true;
            } else {
                throw new IOException("Fetch PreSharedKey is not set.");
            }
        }

        ReadToken rtoken = null;

        String format = splitFetch ? "wrapper" : req.getParameter(Constants.HTTP_PARAM_FORMAT);

        if (!splitFetch) {
            try {
                rtoken = Tokens.extractReadToken(token);

                if (rtoken.getHooksSize() > 0) {
                    throw new IOException("Tokens with hooks cannot be used for fetching data.");
                }
            } catch (WarpScriptException ee) {
                throw new IOException(ee);
            }

            if (null == rtoken) {
                resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Missing token.");
                return;
            }
        }

        boolean showAttr = "true".equals(req.getParameter(Constants.HTTP_PARAM_SHOWATTR));

        boolean sortMeta = "true".equals(req.getParameter(Constants.HTTP_PARAM_SORTMETA));

        //
        // Extract the class and labels selectors
        // The class selector and label selectors are supposed to have
        // values which use percent encoding, i.e. explicit percent encoding which
        // might have been re-encoded using percent encoding when passed as parameter
        //
        //

        Set<Metadata> metadatas = new HashSet<Metadata>();
        List<Iterator<Metadata>> iterators = new ArrayList<Iterator<Metadata>>();

        if (!splitFetch) {

            if (null == selector) {
                throw new IOException("Missing '" + Constants.HTTP_PARAM_SELECTOR + "' parameter.");
            }

            String[] selectors = selector.split("\\s+");

            for (String sel : selectors) {
                Matcher m = SELECTOR_RE.matcher(sel);

                if (!m.matches()) {
                    resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
                    return;
                }

                String classSelector = URLDecoder.decode(m.group(1), "UTF-8");
                String labelsSelection = m.group(2);

                Map<String, String> labelsSelectors;

                try {
                    labelsSelectors = GTSHelper.parseLabelsSelectors(labelsSelection);
                } catch (ParseException pe) {
                    throw new IOException(pe);
                }

                //
                // Force 'producer'/'owner'/'app' from token
                //

                labelsSelectors.remove(Constants.PRODUCER_LABEL);
                labelsSelectors.remove(Constants.OWNER_LABEL);
                labelsSelectors.remove(Constants.APPLICATION_LABEL);

                labelsSelectors.putAll(Tokens.labelSelectorsFromReadToken(rtoken));

                List<Metadata> metas = null;

                List<String> clsSels = new ArrayList<String>();
                List<Map<String, String>> lblsSels = new ArrayList<Map<String, String>>();

                clsSels.add(classSelector);
                lblsSels.add(labelsSelectors);

                try {
                    metas = directoryClient.find(clsSels, lblsSels);
                    metadatas.addAll(metas);
                } catch (Exception e) {
                    //
                    // If metadatas is not empty, create an iterator for it, then clear it
                    //
                    if (!metadatas.isEmpty()) {
                        iterators.add(metadatas.iterator());
                        metadatas.clear();
                    }
                    iterators.add(directoryClient.iterator(clsSels, lblsSels));
                }
            }
        } else {
            //
            // Add an iterator which reads splits from the request body
            //

            boolean gzipped = false;

            if (null != req.getHeader("Content-Type")
                    && "application/gzip".equals(req.getHeader("Content-Type"))) {
                gzipped = true;
            }

            BufferedReader br = null;

            if (gzipped) {
                GZIPInputStream is = new GZIPInputStream(req.getInputStream());
                br = new BufferedReader(new InputStreamReader(is));
            } else {
                br = req.getReader();
            }

            final BufferedReader fbr = br;

            MetadataIterator iterator = new MetadataIterator() {

                private List<Metadata> metadatas = new ArrayList<Metadata>();

                private boolean done = false;

                private String lasttoken = "";

                @Override
                public void close() throws Exception {
                    fbr.close();
                }

                @Override
                public Metadata next() {
                    if (!metadatas.isEmpty()) {
                        Metadata meta = metadatas.get(metadatas.size() - 1);
                        metadatas.remove(metadatas.size() - 1);
                        return meta;
                    } else {
                        if (hasNext()) {
                            return next();
                        } else {
                            throw new NoSuchElementException();
                        }
                    }
                }

                @Override
                public boolean hasNext() {
                    if (!metadatas.isEmpty()) {
                        return true;
                    }

                    if (done) {
                        return false;
                    }

                    String line = null;

                    try {
                        line = fbr.readLine();
                    } catch (IOException ioe) {
                        throw new RuntimeException(ioe);
                    }

                    if (null == line) {
                        done = true;
                        return false;
                    }

                    //
                    // Decode/Unwrap/Deserialize the split
                    //

                    byte[] data = OrderPreservingBase64.decode(line.getBytes(Charsets.US_ASCII));
                    if (null != fetchAES) {
                        data = CryptoUtils.unwrap(fetchAES, data);
                    }

                    if (null == data) {
                        throw new RuntimeException("Invalid wrapped content.");
                    }

                    TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());

                    GTSSplit split = new GTSSplit();

                    try {
                        deserializer.deserialize(split, data);
                    } catch (TException te) {
                        throw new RuntimeException(te);
                    }

                    //
                    // Check the expiry
                    //

                    long instant = System.currentTimeMillis();

                    if (instant - split.getTimestamp() > maxSplitAge || instant > split.getExpiry()) {
                        throw new RuntimeException("Split has expired.");
                    }

                    this.metadatas.addAll(split.getMetadatas());

                    // We assume there was at least one metadata instance in the split!!!
                    return true;
                }
            };

            iterators.add(iterator);
        }

        List<Metadata> metas = new ArrayList<Metadata>();
        metas.addAll(metadatas);

        if (!metas.isEmpty()) {
            iterators.add(metas.iterator());
        }

        //
        // Loop over the iterators, storing the read metadata to a temporary file encrypted on disk
        // Data is encrypted using a onetime pad
        //

        final byte[] onetimepad = new byte[(int) Math.min(65537, System.currentTimeMillis() % 100000)];
        new Random().nextBytes(onetimepad);

        final File cache = File.createTempFile(
                Long.toHexString(System.currentTimeMillis()) + "-" + Long.toHexString(System.nanoTime()),
                ".dircache");
        cache.deleteOnExit();

        FileWriter writer = new FileWriter(cache);

        TSerializer serializer = new TSerializer(new TCompactProtocol.Factory());

        int padidx = 0;

        for (Iterator<Metadata> itermeta : iterators) {
            try {
                while (itermeta.hasNext()) {
                    Metadata metadata = itermeta.next();

                    try {
                        byte[] bytes = serializer.serialize(metadata);
                        // Apply onetimepad
                        for (int i = 0; i < bytes.length; i++) {
                            bytes[i] = (byte) (bytes[i] ^ onetimepad[padidx++]);
                            if (padidx >= onetimepad.length) {
                                padidx = 0;
                            }
                        }
                        OrderPreservingBase64.encodeToWriter(bytes, writer);
                        writer.write('\n');
                    } catch (TException te) {
                    }
                }

                if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) {
                    try {
                        ((MetadataIterator) itermeta).close();
                    } catch (Exception e) {
                    }
                }
            } catch (Throwable t) {
                throw t;
            } finally {
                if (itermeta instanceof MetadataIterator) {
                    try {
                        ((MetadataIterator) itermeta).close();
                    } catch (Exception e) {
                    }
                }
            }
        }

        writer.close();

        //
        // Create an iterator based on the cache
        //

        MetadataIterator cacheiterator = new MetadataIterator() {

            BufferedReader reader = new BufferedReader(new FileReader(cache));

            private Metadata current = null;
            private boolean done = false;

            private TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());

            int padidx = 0;

            @Override
            public boolean hasNext() {
                if (done) {
                    return false;
                }

                if (null != current) {
                    return true;
                }

                try {
                    String line = reader.readLine();
                    if (null == line) {
                        done = true;
                        return false;
                    }
                    byte[] raw = OrderPreservingBase64.decode(line.getBytes(Charsets.US_ASCII));
                    // Apply one time pad
                    for (int i = 0; i < raw.length; i++) {
                        raw[i] = (byte) (raw[i] ^ onetimepad[padidx++]);
                        if (padidx >= onetimepad.length) {
                            padidx = 0;
                        }
                    }
                    Metadata metadata = new Metadata();
                    try {
                        deserializer.deserialize(metadata, raw);
                        this.current = metadata;
                        return true;
                    } catch (TException te) {
                        LOG.error("", te);
                    }
                } catch (IOException ioe) {
                    LOG.error("", ioe);
                }

                return false;
            }

            @Override
            public Metadata next() {
                if (null != this.current) {
                    Metadata metadata = this.current;
                    this.current = null;
                    return metadata;
                } else {
                    throw new NoSuchElementException();
                }
            }

            @Override
            public void close() throws Exception {
                this.reader.close();
                cache.delete();
            }
        };

        iterators.clear();
        iterators.add(cacheiterator);

        metas = new ArrayList<Metadata>();

        PrintWriter pw = resp.getWriter();

        AtomicReference<Metadata> lastMeta = new AtomicReference<Metadata>(null);
        AtomicLong lastCount = new AtomicLong(0L);

        long fetchtimespan = timespan;

        for (Iterator<Metadata> itermeta : iterators) {
            while (itermeta.hasNext()) {
                metas.add(itermeta.next());

                //
                // Access the data store every 'FETCH_BATCHSIZE' GTS or at the end of each iterator
                //

                if (metas.size() > FETCH_BATCHSIZE || !itermeta.hasNext()) {
                    try (GTSDecoderIterator iterrsc = storeClient.fetch(rtoken, metas, now, fetchtimespan,
                            fromArchive, writeTimestamp)) {
                        GTSDecoderIterator iter = iterrsc;

                        if (unpack) {
                            iter = new UnpackingGTSDecoderIterator(iter, suffix);
                            timespan = Long.MIN_VALUE + 1;
                        }

                        if ("text".equals(format)) {
                            textDump(pw, iter, now, timespan, false, dedup, signed, showAttr, lastMeta,
                                    lastCount, sortMeta);
                        } else if ("fulltext".equals(format)) {
                            textDump(pw, iter, now, timespan, true, dedup, signed, showAttr, lastMeta,
                                    lastCount, sortMeta);
                        } else if ("raw".equals(format)) {
                            rawDump(pw, iter, dedup, signed, timespan, lastMeta, lastCount, sortMeta);
                        } else if ("wrapper".equals(format)) {
                            wrapperDump(pw, iter, dedup, signed, fetchPSK, timespan, lastMeta, lastCount);
                        } else if ("json".equals(format)) {
                            jsonDump(pw, iter, now, timespan, dedup, signed, lastMeta, lastCount);
                        } else if ("tsv".equals(format)) {
                            tsvDump(pw, iter, now, timespan, false, dedup, signed, lastMeta, lastCount,
                                    sortMeta);
                        } else if ("fulltsv".equals(format)) {
                            tsvDump(pw, iter, now, timespan, true, dedup, signed, lastMeta, lastCount,
                                    sortMeta);
                        } else if ("pack".equals(format)) {
                            packedDump(pw, iter, now, timespan, dedup, signed, lastMeta, lastCount,
                                    maxDecoderLen, suffix, chunksize, sortMeta);
                        } else if ("null".equals(format)) {
                            nullDump(iter);
                        } else {
                            textDump(pw, iter, now, timespan, false, dedup, signed, showAttr, lastMeta,
                                    lastCount, sortMeta);
                        }
                    } catch (Throwable t) {
                        LOG.error("", t);
                        Sensision.update(SensisionConstants.CLASS_WARP_FETCH_ERRORS, Sensision.EMPTY_LABELS, 1);
                        if (showErrors) {
                            pw.println();
                            StringWriter sw = new StringWriter();
                            PrintWriter pw2 = new PrintWriter(sw);
                            t.printStackTrace(pw2);
                            pw2.close();
                            sw.flush();
                            String error = URLEncoder.encode(sw.toString(), "UTF-8");
                            pw.println(Constants.EGRESS_FETCH_ERROR_PREFIX + error);
                        }
                        throw new IOException(t);
                    } finally {
                        if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) {
                            try {
                                ((MetadataIterator) itermeta).close();
                            } catch (Exception e) {
                            }
                        }
                    }

                    //
                    // Reset 'metas'
                    //

                    metas.clear();
                }
            }

            if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) {
                try {
                    ((MetadataIterator) itermeta).close();
                } catch (Exception e) {
                }
            }
        }

        Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_FETCH_REQUESTS, labels, 1);
    } catch (Exception e) {
        if (!resp.isCommitted()) {
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
            return;
        }
    }
}