|
| 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.pipes.core.serialization; |
| 18 | + |
| 19 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.EMIT_KEY; |
| 20 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.EMITTER; |
| 21 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.FETCHER; |
| 22 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.FETCH_KEY; |
| 23 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.FETCH_RANGE_END; |
| 24 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.FETCH_RANGE_START; |
| 25 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.ID; |
| 26 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.METADATA_KEY; |
| 27 | +import static org.apache.tika.pipes.core.serialization.FetchEmitTupleSerializer.ON_PARSE_EXCEPTION; |
| 28 | +import static org.apache.tika.serialization.ParseContextSerializer.PARSE_CONTEXT; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import com.fasterxml.jackson.core.JacksonException; |
| 34 | +import com.fasterxml.jackson.core.JsonParser; |
| 35 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 36 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 37 | +import com.fasterxml.jackson.databind.JsonNode; |
| 38 | + |
| 39 | +import org.apache.tika.metadata.Metadata; |
| 40 | +import org.apache.tika.parser.ParseContext; |
| 41 | +import org.apache.tika.pipes.core.FetchEmitTuple; |
| 42 | +import org.apache.tika.pipes.core.emitter.EmitKey; |
| 43 | +import org.apache.tika.pipes.core.fetcher.FetchKey; |
| 44 | +import org.apache.tika.serialization.ParseContextDeserializer; |
| 45 | + |
| 46 | +public class FetchEmitTupleDeserializer extends JsonDeserializer<FetchEmitTuple> { |
| 47 | + |
| 48 | + @Override |
| 49 | + public FetchEmitTuple deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException { |
| 50 | + JsonNode root = jsonParser.readValueAsTree(); |
| 51 | + |
| 52 | + String id = readVal(ID, root, null, true); |
| 53 | + String fetcherName = readVal(FETCHER, root, null, true); |
| 54 | + String fetchKey = readVal(FETCH_KEY, root, null, true); |
| 55 | + String emitterName = readVal(EMITTER, root, "", false); |
| 56 | + String emitKey = readVal(EMIT_KEY, root, "", false); |
| 57 | + long fetchRangeStart = readLong(FETCH_RANGE_START, root, -1l, false); |
| 58 | + long fetchRangeEnd = readLong(FETCH_RANGE_END, root, -1l, false); |
| 59 | + Metadata metadata = readMetadata(root); |
| 60 | + JsonNode parseContextNode = root.get(PARSE_CONTEXT); |
| 61 | + ParseContext parseContext = parseContextNode == null ? new ParseContext() : ParseContextDeserializer.readParseContext(parseContextNode); |
| 62 | + FetchEmitTuple.ON_PARSE_EXCEPTION onParseException = readOnParseException(root); |
| 63 | + |
| 64 | + return new FetchEmitTuple(id, new FetchKey(fetcherName, fetchKey, fetchRangeStart, fetchRangeEnd), |
| 65 | + new EmitKey(emitterName, emitKey), metadata, parseContext, |
| 66 | + onParseException); |
| 67 | + } |
| 68 | + |
| 69 | + private static FetchEmitTuple.ON_PARSE_EXCEPTION readOnParseException(JsonNode root) throws IOException { |
| 70 | + JsonNode onParseExNode = root.get(ON_PARSE_EXCEPTION); |
| 71 | + if (onParseExNode == null) { |
| 72 | + return FetchEmitTuple.ON_PARSE_EXCEPTION.EMIT; |
| 73 | + } |
| 74 | + String txt = onParseExNode.asText(); |
| 75 | + if ("skip".equalsIgnoreCase(txt)) { |
| 76 | + return FetchEmitTuple.ON_PARSE_EXCEPTION.SKIP; |
| 77 | + } else if ("emit".equalsIgnoreCase(txt)) { |
| 78 | + return FetchEmitTuple.ON_PARSE_EXCEPTION.EMIT; |
| 79 | + } else { |
| 80 | + throw new IOException(ON_PARSE_EXCEPTION + " must be either 'skip' or 'emit'"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static Metadata readMetadata(JsonNode root) { |
| 85 | + JsonNode metadataNode = root.get(METADATA_KEY); |
| 86 | + if (metadataNode == null) { |
| 87 | + return new Metadata(); |
| 88 | + } |
| 89 | + Metadata metadata = new Metadata(); |
| 90 | + for (Map.Entry<String, JsonNode> e : metadataNode.properties()) { |
| 91 | + JsonNode vals = e.getValue(); |
| 92 | + String k = e.getKey(); |
| 93 | + if (vals.isArray()) { |
| 94 | + for (JsonNode arrVal : vals) { |
| 95 | + metadata.add(k, arrVal.textValue()); |
| 96 | + } |
| 97 | + } else { |
| 98 | + metadata.set(k, vals.asText()); |
| 99 | + } |
| 100 | + } |
| 101 | + return metadata; |
| 102 | + } |
| 103 | + |
| 104 | + private static String readVal(String key, JsonNode jsonObj, String defaultRet, boolean isRequired) throws IOException { |
| 105 | + JsonNode valNode = jsonObj.get(key); |
| 106 | + if (valNode == null) { |
| 107 | + if (isRequired) { |
| 108 | + throw new IOException("required value string, but see: " + key); |
| 109 | + } |
| 110 | + return defaultRet; |
| 111 | + } |
| 112 | + return valNode.asText(); |
| 113 | + } |
| 114 | + |
| 115 | + private static long readLong(String key, JsonNode jsonObj, long defaultVal, boolean isRequired) throws IOException { |
| 116 | + JsonNode val = jsonObj.get(key); |
| 117 | + if (val == null) { |
| 118 | + if (isRequired) { |
| 119 | + throw new IOException("required value long, but see: " + key); |
| 120 | + } |
| 121 | + return defaultVal; |
| 122 | + } |
| 123 | + return val.longValue(); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments