1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.tika.metadata; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.net.URL; 22 import java.net.URLConnection; 23 24 /** 25 * Collection of static helper methods for handling metadata. 26 * 27 * @since Apache Tika 0.7 28 */ 29 public class MetadataHelper { 30 31 /** 32 * Private constructor to prevent instantiation. 33 */ 34 private MetadataHelper() { 35 } 36 37 /** 38 * Returns the content at the given URL, and sets any related 39 * metadata entries. 40 * 41 * @param url the URL of the resource to be read 42 * @param metadata where the resource metadata is stored 43 * @return resource content 44 * @throws IOException if the URL can not be accessed 45 */ 46 public static InputStream getInputStream(URL url, Metadata metadata) 47 throws IOException { 48 URLConnection connection = url.openConnection(); 49 50 String path = url.getPath(); 51 int slash = path.lastIndexOf('/'); 52 if (slash + 1 < path.length()) { // works even with -1! 53 metadata.set(Metadata.RESOURCE_NAME_KEY, path.substring(slash + 1)); 54 } 55 56 String type = connection.getContentType(); 57 if (type != null) { 58 metadata.set(Metadata.CONTENT_TYPE, type); 59 } 60 61 String encoding = connection.getContentEncoding(); 62 if (encoding != null) { 63 metadata.set(Metadata.CONTENT_TYPE, encoding); 64 } 65 66 int length = connection.getContentLength(); 67 if (length >= 0) { 68 metadata.set(Metadata.CONTENT_LENGTH, Integer.toString(length)); 69 } 70 71 return connection.getInputStream(); 72 } 73 74 }