Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 16 | package org.onosproject.rest; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 17 | |
| 18 | import java.io.ByteArrayInputStream; |
| 19 | import java.io.InputStream; |
| 20 | import java.util.Enumeration; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
| 27 | * Resource for serving semi-static resources. |
| 28 | */ |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 29 | public class AbstractInjectionResource extends AbstractWebResource { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 30 | |
| 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 Vachuska | d32bfdc | 2015-02-21 16:39:25 -0800 | [diff] [blame] | 38 | * @return index where the split should occur |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 39 | */ |
| 40 | protected int split(String string, int start, String stopPattern) { |
| 41 | int i = stopPattern != null ? string.indexOf(stopPattern, start) : string.length(); |
Thomas Vachuska | 9730ec9 | 2015-03-07 17:25:21 -0800 | [diff] [blame] | 42 | checkArgument(i >= 0, "Unable to locate pattern %s", stopPattern); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 43 | 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 Vachuska | d32bfdc | 2015-02-21 16:39:25 -0800 | [diff] [blame] | 52 | * @return input stream |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 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 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 64 | public StreamEnumeration(List<InputStream> streams) { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 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 | } |