1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.Enumeration;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipFile;
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33 import org.json.JSONTokener;
35 public class HtDatabaseUpdateFile extends ZipFile {
37 public static final String FILENAME_DEFAULT = "elasticsearch_update.zip";
39 public class EsUpdateObject {
40 public final String Uri;
41 public final String Method;
42 public final JSONObject Body;
44 public EsUpdateObject(JSONObject o) {
45 this.Uri = o.getString("uri");
46 this.Method = o.getString("method");
47 this.Body = o.getJSONObject("body");
51 public interface FileReadCallback {
52 void read(EsUpdateObject obj,String filename);
54 void onerror(String filename,IOException e);
57 private static final Comparator<ZipEntry> byfilenameComparator = new Comparator<ZipEntry>() {
60 public int compare(ZipEntry o1, ZipEntry o2) {
61 return o1.getName().compareTo(o2.getName());
65 public HtDatabaseUpdateFile(String filename) throws IOException {
69 private static String readFile(final InputStream s) throws IOException {
71 BufferedReader in = new BufferedReader(new InputStreamReader(s));
72 StringBuilder sb = new StringBuilder();
74 while ((inputLine = in.readLine()) != null) {
82 public boolean readFiles(FileReadCallback cb) {
84 Enumeration<? extends ZipEntry> entries = this.entries();
85 ArrayList<? extends ZipEntry> list = Collections.list(entries);
86 Collections.sort(list, byfilenameComparator);
87 for (ZipEntry entry : list) {
88 if (entry.isDirectory())
91 InputStream stream = this.getInputStream(entry);
92 Object data = new JSONTokener(readFile(stream)).nextValue();
94 if(data instanceof JSONArray)
96 JSONArray a=(JSONArray)data;
97 for(int i=0;i<a.length();i++)
99 cb.read(new EsUpdateObject(a.getJSONObject(i)),entry.getName());
102 else if(data instanceof JSONObject)
104 cb.read(new EsUpdateObject( (JSONObject)data),entry.getName());
106 } catch (IOException e) {
108 cb.onerror(entry.getName(),e);