blob: 4ee6cf93a800a9b2b34dde275daa753cb17f722c [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ui.impl;
17
18import org.onlab.rest.BaseResource;
19
20import java.io.ByteArrayInputStream;
21import java.io.InputStream;
22import java.util.Enumeration;
23import java.util.Iterator;
24import java.util.List;
25
26import static com.google.common.base.Preconditions.checkArgument;
27
28/**
29 * Resource for serving semi-static resources.
30 */
31public class AbstractInjectionResource extends BaseResource {
32
33 /**
34 * Returns the index into the supplied string where the end of the
35 * specified pattern is located.
36 *
37 * @param string string to split
38 * @param start index where to start looking for pattern
39 * @param stopPattern optional pattern where to stop
Thomas Vachuskad32bfdc2015-02-21 16:39:25 -080040 * @return index where the split should occur
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080041 */
42 protected int split(String string, int start, String stopPattern) {
43 int i = stopPattern != null ? string.indexOf(stopPattern, start) : string.length();
Thomas Vachuska9730ec92015-03-07 17:25:21 -080044 checkArgument(i >= 0, "Unable to locate pattern %s", stopPattern);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080045 return i + (stopPattern != null ? stopPattern.length() : 0);
46 }
47
48 /**
49 * Produces an input stream from the bytes of the specified sub-string.
50 *
51 * @param string source string
52 * @param start index where to start stream
53 * @param end index where to end stream
Thomas Vachuskad32bfdc2015-02-21 16:39:25 -080054 * @return input stream
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080055 */
56 protected InputStream stream(String string, int start, int end) {
57 return new ByteArrayInputStream(string.substring(start, end).getBytes());
58 }
59
60 /**
61 * Auxiliary enumeration to sequence input streams.
62 */
63 protected class StreamEnumeration implements Enumeration<InputStream> {
64 private final Iterator<InputStream> iterator;
65
66 StreamEnumeration(List<InputStream> streams) {
67 this.iterator = streams.iterator();
68 }
69
70 @Override
71 public boolean hasMoreElements() {
72 return iterator.hasNext();
73 }
74
75 @Override
76 public InputStream nextElement() {
77 return iterator.next();
78 }
79 }
80}