blob: de182f03e392b1b09ebe4e7e6ca50594497efb26 [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 */
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070016package org.onosproject.rest;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080017
18import java.io.ByteArrayInputStream;
19import java.io.InputStream;
20import java.util.Enumeration;
21import java.util.Iterator;
22import java.util.List;
23
24import static com.google.common.base.Preconditions.checkArgument;
25
26/**
27 * Resource for serving semi-static resources.
28 */
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070029public class AbstractInjectionResource extends AbstractWebResource {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080030
31 /**
32 * Returns the index into the supplied string where the end of the
33 * specified pattern is located.
34 *
35 * @param string string to split
36 * @param start index where to start looking for pattern
37 * @param stopPattern optional pattern where to stop
Thomas Vachuskad32bfdc2015-02-21 16:39:25 -080038 * @return index where the split should occur
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080039 */
40 protected int split(String string, int start, String stopPattern) {
41 int i = stopPattern != null ? string.indexOf(stopPattern, start) : string.length();
Thomas Vachuska9730ec92015-03-07 17:25:21 -080042 checkArgument(i >= 0, "Unable to locate pattern %s", stopPattern);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080043 return i + (stopPattern != null ? stopPattern.length() : 0);
44 }
45
46 /**
47 * Produces an input stream from the bytes of the specified sub-string.
48 *
49 * @param string source string
50 * @param start index where to start stream
51 * @param end index where to end stream
Thomas Vachuskad32bfdc2015-02-21 16:39:25 -080052 * @return input stream
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080053 */
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
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 public StreamEnumeration(List<InputStream> streams) {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080065 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}