| margaretha | ac92c82 | 2023-05-15 11:43:22 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.core.service; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.InputStreamReader; |
| 5 | import java.io.OutputStream; |
| 6 | import java.net.HttpURLConnection; |
| 7 | import java.net.URL; |
| 8 | |
| 9 | import org.apache.http.HttpStatus; |
| 10 | import org.apache.logging.log4j.LogManager; |
| 11 | import org.apache.logging.log4j.Logger; |
| 12 | import org.springframework.beans.factory.annotation.Autowired; |
| 13 | import org.springframework.stereotype.Service; |
| 14 | |
| 15 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 16 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 17 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 18 | |
| 19 | @Service |
| 20 | public class SearchNetworkEndpoint { |
| 21 | |
| 22 | private final static Logger jlog = LogManager |
| 23 | .getLogger(SearchNetworkEndpoint.class); |
| 24 | |
| 25 | @Autowired |
| 26 | private KustvaktConfiguration config; |
| 27 | |
| 28 | public String search (String query) throws KustvaktException { |
| 29 | String networkEndpointURL = config.getNetworkEndpointURL(); |
| 30 | if (networkEndpointURL == null || networkEndpointURL.isEmpty()) { |
| 31 | throw new KustvaktException( |
| 32 | StatusCodes.NETWORK_ENDPOINT_NOT_AVAILABLE, |
| 33 | "Network endpoint is not available"); |
| 34 | } |
| 35 | else { |
| 36 | try { |
| 37 | URL url = new URL(networkEndpointURL); |
| 38 | HttpURLConnection connection = (HttpURLConnection) url |
| 39 | .openConnection(); |
| 40 | connection.setRequestMethod("POST"); |
| 41 | connection.setRequestProperty("Content-Type", |
| 42 | "application/json; charset=UTF-8"); |
| 43 | connection.setRequestProperty("Accept", "application/json"); |
| 44 | connection.setDoOutput(true); |
| 45 | OutputStream os = connection.getOutputStream(); |
| 46 | byte[] input = query.getBytes("utf-8"); |
| 47 | os.write(input, 0, input.length); |
| 48 | |
| 49 | String entity = null; |
| 50 | if (connection.getResponseCode() == HttpStatus.SC_OK) { |
| 51 | BufferedReader br = new BufferedReader( |
| 52 | new InputStreamReader(connection.getInputStream(), |
| 53 | "utf-8")); |
| 54 | StringBuilder response = new StringBuilder(); |
| 55 | String responseLine = null; |
| 56 | while ((responseLine = br.readLine()) != null) { |
| 57 | response.append(responseLine.trim()); |
| 58 | } |
| 59 | entity = response.toString(); |
| 60 | } |
| 61 | |
| 62 | if (entity != null && !entity.isEmpty()) { |
| 63 | return entity; |
| 64 | } |
| 65 | else { |
| 66 | String message = connection.getResponseCode() + " " |
| 67 | + connection.getResponseMessage(); |
| 68 | jlog.warn("Search on network endpoint failed " |
| 69 | + networkEndpointURL + ". Message: " + message); |
| 70 | |
| 71 | throw new KustvaktException( |
| 72 | StatusCodes.SEARCH_NETWORK_ENDPOINT_FAILED, |
| 73 | "Failed searching at network endpoint: " |
| 74 | + networkEndpointURL, |
| 75 | message); |
| 76 | } |
| 77 | } |
| 78 | catch (Exception e) { |
| 79 | throw new KustvaktException( |
| 80 | StatusCodes.SEARCH_NETWORK_ENDPOINT_FAILED, |
| 81 | "Failed searching at network endpoint: " |
| 82 | + networkEndpointURL, |
| 83 | e.getCause()); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |