|
| 1 | +/** |
| 2 | + * Copyright 2015 IBM Corp. All Rights Reserved. |
| 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 | + */ |
| 16 | + |
| 17 | +package com.ibm.watson.developer_cloud.alchemy.v1.util; |
| 18 | + |
| 19 | +import com.google.gson.JsonElement; |
| 20 | +import com.google.gson.JsonObject; |
| 21 | +import com.google.gson.JsonParseException; |
| 22 | +import com.google.gson.JsonParser; |
| 23 | + |
| 24 | +import java.io.InputStream; |
| 25 | +import java.io.InputStreamReader; |
| 26 | +import java.io.Reader; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.logging.Level; |
| 30 | +import java.util.logging.Logger; |
| 31 | + |
| 32 | +/** |
| 33 | + * The Class AlchemyEndPoints. |
| 34 | + * |
| 35 | + * @author Nizar Alseddeg (nmalsedd@us.ibm.com) |
| 36 | + */ |
| 37 | +public class AlchemyEndPoints { |
| 38 | + |
| 39 | + /** The Constant log. */ |
| 40 | + private static final Logger log = Logger.getLogger(AlchemyEndPoints.class.getName()); |
| 41 | + |
| 42 | + /** The file where alchemy endpoints are described. */ |
| 43 | + private static final String filePath = "/alchemy_endpoints.json"; |
| 44 | + |
| 45 | + |
| 46 | + /** The alchemy operations. */ |
| 47 | + private static Map<String, Map<String, String>> operations; |
| 48 | + |
| 49 | + static { |
| 50 | + loadEndPointsFromJsonFile(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * The AlchemyOperations. |
| 55 | + */ |
| 56 | + public enum AlchemyAPI { |
| 57 | + |
| 58 | + /** The entities. */ |
| 59 | + entities, |
| 60 | + |
| 61 | + /** The keywords. */ |
| 62 | + keywords, |
| 63 | + |
| 64 | + /** The concepts. */ |
| 65 | + concepts, |
| 66 | + |
| 67 | + /** The sentiment. */ |
| 68 | + sentiment, |
| 69 | + |
| 70 | + /** The sentiment_targeted. */ |
| 71 | + sentiment_targeted, |
| 72 | + |
| 73 | + /** The relations. */ |
| 74 | + relations, |
| 75 | + |
| 76 | + /** The language. */ |
| 77 | + language, |
| 78 | + |
| 79 | + /** The text. */ |
| 80 | + text, |
| 81 | + |
| 82 | + /** The text_raw. */ |
| 83 | + text_raw, |
| 84 | + |
| 85 | + /** The authors. */ |
| 86 | + authors, |
| 87 | + |
| 88 | + /** The feeds. */ |
| 89 | + feeds, |
| 90 | + |
| 91 | + /** The microformats. */ |
| 92 | + microformats, |
| 93 | + |
| 94 | + /** The title. */ |
| 95 | + title, |
| 96 | + |
| 97 | + /** The taxonomy. */ |
| 98 | + taxonomy, |
| 99 | + |
| 100 | + /** The combined. */ |
| 101 | + combined, |
| 102 | + |
| 103 | + /** The image_link. */ |
| 104 | + image_link, |
| 105 | + |
| 106 | + /** The image_keywords. */ |
| 107 | + image_keywords, |
| 108 | + |
| 109 | + /** The image_recognition. */ |
| 110 | + image_recognition |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Load the endpoints from json file. |
| 115 | + */ |
| 116 | + private static void loadEndPointsFromJsonFile() { |
| 117 | + log.log(Level.FINE, "Parsing End Points JSON file "); |
| 118 | + operations = new HashMap<String, Map<String, String>>(); |
| 119 | + JsonParser parser = new JsonParser(); |
| 120 | + try { |
| 121 | + Reader fileReader = null; |
| 122 | + |
| 123 | + InputStream is = AlchemyEndPoints.class.getResourceAsStream(filePath); |
| 124 | + if (null != is) { |
| 125 | + fileReader = new InputStreamReader(is); |
| 126 | + } |
| 127 | + Object obj = parser.parse(fileReader); |
| 128 | + JsonObject jsonObject = (JsonObject) obj; |
| 129 | + for (AlchemyAPI object : AlchemyAPI.values()) { |
| 130 | + if (jsonObject.get(object.name()) == null) |
| 131 | + continue;; |
| 132 | + JsonElement elt = jsonObject.get(object.name()).getAsJsonObject(); |
| 133 | + if (elt.isJsonObject()) { |
| 134 | + Map<String, String> records = new HashMap<String, String>(); |
| 135 | + for (Map.Entry<String, JsonElement> e : elt.getAsJsonObject().entrySet()) { |
| 136 | + records.put(e.getKey(), e.getValue().getAsString()); |
| 137 | + } |
| 138 | + operations.put(object.name(), records); |
| 139 | + } |
| 140 | + } |
| 141 | + } catch (JsonParseException e) { |
| 142 | + log.log(Level.SEVERE, "Could not parse json file: " + filePath, e); |
| 143 | + } catch (NullPointerException e){ |
| 144 | + log.log(Level.SEVERE, "Not able to locate the end points json file: " + filePath, e); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Gets the path based on the operation and input type. |
| 150 | + * |
| 151 | + * @param operation the operation |
| 152 | + * @param inputType the input type |
| 153 | + * @return the string that represent the path based on the operation |
| 154 | + * and input type |
| 155 | + */ |
| 156 | + public static String getPath(AlchemyAPI operation, String inputType) { |
| 157 | + if ((operations.get(operation.name()) != null) && operations.get(operation.name()).get(inputType) != null) |
| 158 | + return operations.get(operation.name()).get(inputType); |
| 159 | + else { |
| 160 | + String error = "Operation: "+ operation + ", inputType: "+inputType+" not found"; |
| 161 | + log.log(Level.SEVERE,error); |
| 162 | + throw new IllegalArgumentException(error); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments