blob: 2ed4ecd9b82c7fdfbea651a6b70d330139280f6e [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
40 */
41 protected int split(String string, int start, String stopPattern) {
42 int i = stopPattern != null ? string.indexOf(stopPattern, start) : string.length();
43 checkArgument(i > 0, "Unable to locate stop pattern %s", stopPattern);
44 return i + (stopPattern != null ? stopPattern.length() : 0);
45 }
46
47 /**
48 * Produces an input stream from the bytes of the specified sub-string.
49 *
50 * @param string source string
51 * @param start index where to start stream
52 * @param end index where to end stream
53 */
54 protected InputStream stream(String string, int start, int end) {
55 return new ByteArrayInputStream(string.substring(start, end).getBytes());
56 }
57
58 /**
59 * Auxiliary enumeration to sequence input streams.
60 */
61 protected class StreamEnumeration implements Enumeration<InputStream> {
62 private final Iterator<InputStream> iterator;
63
64 StreamEnumeration(List<InputStream> streams) {
65 this.iterator = streams.iterator();
66 }
67
68 @Override
69 public boolean hasMoreElements() {
70 return iterator.hasNext();
71 }
72
73 @Override
74 public InputStream nextElement() {
75 return iterator.next();
76 }
77 }
78}