Commit ec184135 authored by Maxime Lefrançois's avatar Maxime Lefrançois
Browse files

Merge branch 'SalvaBranch2' into 'master'

Changed read of xml as text file and solved some issues with timeouts

See merge request saref/saref-pipeline!10
parents 589e589e 058b1163
Loading
Loading
Loading
Loading
+104 −50
Original line number Diff line number Diff line
@@ -26,14 +26,15 @@
package fr.emse.gitlab.saref.checkers;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import fr.emse.gitlab.saref.managers.ThemisManager;
import org.apache.jena.rdf.model.Model;
import org.semanticweb.owl.explanation.api.Explanation;
import org.semanticweb.owl.explanation.api.ExplanationGenerator;
@@ -48,13 +49,16 @@ import org.semanticweb.owlapi.profiles.violations.UseOfUnknownDatatype;

import fr.emse.gitlab.saref.SAREFPipelineException;
import fr.emse.gitlab.saref.managers.OntologyManager;
import fr.emse.gitlab.saref.managers.OopsManager;
import fr.emse.gitlab.saref.managers.RepositoryManager;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.semanticweb.owlapi.reasoner.TimeOutException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Checks TS 103 673 Clause 9.4.5: OWL Profile, Consistency, and Satisfiability
@@ -139,6 +143,7 @@ public class Clause_9_4_5_Checker extends AbstractClauseChecker {
				+ "<OutputFormat>RDF/XML</OutputFormat>" + "</OOPSRequest>";



		Object[] arrayData = new Object[0];

		try {
@@ -152,45 +157,94 @@ public class Clause_9_4_5_Checker extends AbstractClauseChecker {
			Request request = new Request.Builder().url("http://oops.linkeddata.es/rest").method("POST", body)
					.addHeader("Content-Type", "application/xml").build();

			Response response = httpClient.newCall(request).execute();
			if (response.code() != 200) {
				throw new SAREFPipelineException("response", "Unexpected response code " + response.code());
			}
			Response response;

			String result = response.body().string();
			try{
				response = httpClient.newCall(request).execute();
			}
			catch (SocketTimeoutException e){

			String[] results = result.split("\\n");
				httpClient = new OkHttpClient().newBuilder().readTimeout(10, TimeUnit.SECONDS).build();

			List<String> pureData = new ArrayList<String>();
			for (int i = 0; i < results.length; i++) {
				pureData.add(results[i]);
				try{
					response = httpClient.newCall(request).execute();
				}
				catch (SocketTimeoutException f){
					httpClient = new OkHttpClient().newBuilder().readTimeout(20, TimeUnit.SECONDS).build();
					response = httpClient.newCall(request).execute();
				}
			}

			arrayData = pureData.toArray(); // response into array

		} catch (IOException | SAREFPipelineException e) {
			logWarning(getMessage(MESSAGE.oopsError), e);
			if (response.code() != 200) {
				throw new SAREFPipelineException("response", "Unexpected response code " + response.code());
			}

		int count = OopsManager.counter(arrayData);
			String result = response.body().string();

		ArrayList<ArrayList<String>> result = OopsManager.getData(arrayData);
			Document doc = ThemisManager.convertStringToXMLDocument(result);

			NodeList nodeList = doc.getElementsByTagName("rdf:Description");

			ArrayList<String> oopsErrors = new ArrayList<String>();
			for (int temp = 0; temp < nodeList.getLength(); temp++){
				org.w3c.dom.Node node = nodeList.item(temp);
				if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
					Element element = (Element) node;
					if (((Element) node).getElementsByTagName("oops:hasCode").item(0) != null) {
						if (((Element) node).getElementsByTagName("oops:hasName").item(0) != null) {
							String code = element.getElementsByTagName("oops:hasCode").item(0).getTextContent();
							String name = element.getElementsByTagName("oops:hasName").item(0).getTextContent();
							String description = element.getElementsByTagName("oops:hasDescription").item(0).getTextContent();
							String affected = "";
							NodeList affectedElements = element.getElementsByTagName("oops:hasAffectedElement");
							if (affectedElements != null) {
								for (int k = 0; k < affectedElements.getLength(); k++) {
									if (k == 0) {
										affected += "\"" + affectedElements.item(k).getTextContent() + "\"";
									} else {
										affected += " ; \"" + affectedElements.item(k).getTextContent() + "\"";
									}
								}
							}

		int s = 0;

							String oopsError = String.format("Code: %s, Name: %s, Description: %s, Affected Elements: %s", code, name, description, affected);
							oopsErrors.add(oopsError);
						}
						else{
							String code = element.getElementsByTagName("oops:hasCode").item(0).getTextContent();
							String description = element.getElementsByTagName("oops:hasDescription").item(0).getTextContent();
							String affected = "";
							NodeList affectedElements = element.getElementsByTagName("oops:hasAffectedElement");
							if (affectedElements != null) {
								for (int k = 0; k < affectedElements.getLength(); k++) {
									if (k == 0) {
										affected += "\"" + affectedElements.item(k).getTextContent() + "\"";
									} else {
										affected += " ; \"" + affectedElements.item(k).getTextContent() + "\"";
									}
								}
							}

		ArrayList<String> results = new ArrayList<String>();

		while (s < count) {
			String data = result.get(0).get(s).concat("-") + result.get(1).get(s).concat(".") + result.get(2).get(s) + "Affected Elements: " + result.get(3).get(s);
			results.add(data);
			s++;
							String oopsError = String.format("Code: %s, Description: %s, Affected Elements: %s", code, description, affected);
							oopsErrors.add(oopsError);
						}
					}

		String printData = results.stream().map(e -> e.toString())
				}
			}
			if (!oopsErrors.isEmpty()) {
				String data = oopsErrors.stream().map(e -> e.toString())
						.collect(Collectors.joining("\n- ", "\n\n- ", "\n\n"));
		if (count != 0) {
			logWarning(getMessage(MESSAGE.oops, printData));
				logWarning(getMessage(MESSAGE.oops, data));

			}


		} catch (IOException | SAREFPipelineException e) {
			logWarning(getMessage(MESSAGE.oopsError), e);
		}
	}

+0 −103
Original line number Diff line number Diff line
/*
 * Copyright 2020 ETSI
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package fr.emse.gitlab.saref.managers;

import java.util.ArrayList;

public class OopsManager {

	public static ArrayList<ArrayList<String>> getData(Object[] data) {
		ArrayList<String> getID = new ArrayList<String>();
		ArrayList<String> getName = new ArrayList<String>();
		ArrayList<String> getInfo = new ArrayList<String>();
		ArrayList<String> affectedElements = new ArrayList<String>();

		ArrayList<ArrayList<String>> elements = new ArrayList<ArrayList<String>>();
		elements.add(getID);
		elements.add(getName);
		elements.add(getInfo);
		elements.add(affectedElements);

		String affected = "";

		for (int i = 0; i < data.length; i++) {
			String data2 = data[i].toString().replaceAll("  ", "");


			if (data2.startsWith("<oops:hasCode")) {
				String[] data3 = data2.split("(<|>)");
				getID.add(data3[2]);
				elements.set(0, getID);
			} else if (data2.startsWith("<oops:hasName")) {
				String[] data3 = data2.split("(<|>)");
				getName.add(data3[2]);
				elements.set(1, getName);
			} else if (data2.startsWith("<oops:hasDescription")) {
				String[] data3 = data2.split("(<|>)");
				getInfo.add(data3[2]);
				elements.set(2, getInfo);
			}
			if (i>0 && i < data.length-1) {
				String prove = data[i - 1].toString().replaceAll("  ", "");
				String prove2 = data[i + 1].toString().replaceAll("  ", "");

				if (data2.startsWith("<oops:hasAffectedElement")) {
					if (!prove.startsWith("<oops:hasAffectedElement")) {
						String[] data3 = data2.split("(<|>)");
						affected += data3[2];
					}
					else if (!prove2.startsWith("<oops:hasAffectedElement")){
						String[] data3 = data2.split("(<|>)");
						affected += ", " + data3[2];
						affectedElements.add(affected);
						elements.set(3, affectedElements);
						affected = "";
					}
					else{
						String[] data3 = data2.split("(<|>)");
						affected += ", " + data3[2];
					}
				}

			}

		}

		return elements;
	}

	public static int counter(Object[] data) {
		int counter = 0;
		for (int i = 0; i < data.length; i++) {
			String data2 = data[i].toString().replaceAll("  ", "");
			if (data2.startsWith("<oops:hasCode")) {
				counter++;
			}
		}
		return counter;
	}

}
+20 −20

File changed.

Contains only whitespace changes.