<commit_before>/*
 * Copyright 2005-2007 WSO2, Inc. (http://wso2.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wso2.carbon.logging.view.ui;

import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.logging.view.stub.LogViewerLogViewerException;
import org.wso2.carbon.logging.view.stub.LogViewerStub;
import org.wso2.carbon.logging.view.stub.types.carbon.LogEvent;
import org.wso2.carbon.logging.view.stub.types.carbon.PaginatedLogEvent;
import org.wso2.carbon.logging.view.stub.types.carbon.PaginatedLogFileInfo;

import javax.activation.DataHandler;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.rmi.RemoteException;

public class LogViewerClient {
    private static final Log log = LogFactory.getLog(LogViewerClient.class);
    public LogViewerStub stub;

    public LogViewerClient(String cookie, String backendServerURL, ConfigurationContext configCtx)
            throws AxisFault {
        String serviceURL = backendServerURL + "LogViewer";
        stub = new LogViewerStub(configCtx, serviceURL);
        ServiceClient client = stub._getServiceClient();
        Options option = client.getOptions();
        option.setManageSession(true);
        option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, cookie);
//        option.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    }


    public void clearLogs() throws Exception {
        stub.clearLogs();
    }

    public void downloadArchivedLogFiles(String logFile, HttpServletResponse response, String tenantDomain, String serverKey)
            throws Exception {
        try {
        	logFile=logFile.replace(".gz", "");
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/txt");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + logFile.replaceAll("\\s", "_"));
            DataHandler data = stub.downloadArchivedLogFiles(logFile, tenantDomain, serverKey);
            InputStream fileToDownload = data.getInputStream();
            int c;
            while ((c = fileToDownload.read()) != -1) {
                outputStream.write(c);
            }
            outputStream.flush();
            outputStream.flush();
        } catch (Exception e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public int getLineNumbers(String logFile)
            throws Exception {
        try {
            return stub.getLineNumbers(logFile);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public PaginatedLogFileInfo getPaginatedLogFileInfo(int pageNumber, String tenantDomain,
                                                        String serviceName) throws Exception {
        try {
            return stub.getPaginatedLogFileInfo(pageNumber, tenantDomain, serviceName);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public LogEvent[] getLogs(String type, String keyword, String tenantDomain, String serverkey) throws Exception {
        if (type == null || type.equals("")) {
            type = "ALL";
        }
        try {
            return stub.getLogs(type, keyword, tenantDomain, serverkey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public LogEvent[] getApplicationLogs(String type, String keyword, String applicationName, String tenantDomain, String serverKey)
            throws Exception {
        if (type == null || type.equals("")) {
            type = "ALL";
        }
        if (applicationName == null || applicationName.equals("")) {
            applicationName = "FIRST";
        }
        try {
            return stub.getApplicationLogs(type, keyword, applicationName, tenantDomain, serverKey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public String[] getLogLinesFromFile(String logFile, int maxLogs, int start, int end) throws Exception {
        try {
            return stub.getLogLinesFromFile(logFile, maxLogs, start, end);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }


//	public boolean isManager() throws RemoteException {
//		try {
//			return stub.isManager();
//		} catch (RemoteException e) {
//			String msg = "Error occurred while getting logger data. Backend service may be unavailable";
//			log.error(msg, e);
//			throw e;
//		}
//	}

    public String[] getApplicationNames(String tenantDomain, String serverKey) throws LogViewerLogViewerException, RemoteException {
        try {
            return stub.getApplicationNames(tenantDomain, serverKey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public int getNoOfLogEvents() throws Exception {
        try {
            return 20;// stub.getNoOfLogEvents();
        } catch (Exception e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public String[] getServiceNames() throws RemoteException, LogViewerLogViewerException {
        try {
            return stub.getServiceNames();
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public boolean isFileAppenderConfiguredForST() throws RemoteException {
        try {
            return stub.isFileAppenderConfiguredForST();
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public PaginatedLogEvent getPaginatedLogEvents(int pageNumber, String type, String keyword, String tenantDomain, String serverKey)
            throws Exception {
        try {
            return stub.getPaginatedLogEvents(pageNumber, type, keyword, tenantDomain, serverKey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public PaginatedLogEvent getPaginatedApplicationLogEvents(int pageNumber, String type,
                                                              String keyword, String appName, String tenantDomain, String serverKey) throws Exception {
        try {
            return stub.getPaginatedApplicationLogEvents(pageNumber, type, keyword, appName, tenantDomain, serverKey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public PaginatedLogFileInfo getLocalLogFiles(int pageNo, String tenantDomain, String serverKey) throws Exception {

        try {
            return stub.getLocalLogFiles(pageNo, tenantDomain, serverKey);
        } catch (Exception e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }

    }

    public boolean isLogEventReciverConfigured() throws RemoteException {
        try {
            return stub.isLogEventReciverConfigured();
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be unavailable";
            log.error(msg, e);
            throw e;
        }
    }<commit_msg>Refactored LogViewerClient class.<commit_after>/*
 * Copyright 2005-2007 WSO2, Inc. (http://wso2.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wso2.carbon.logging.view.ui;

import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.logging.view.stub.LogViewerLogViewerException;
import org.wso2.carbon.logging.view.stub.LogViewerStub;
import org.wso2.carbon.logging.view.stub.types.carbon.LogEvent;
import org.wso2.carbon.logging.view.stub.types.carbon.PaginatedLogEvent;
import org.wso2.carbon.logging.view.stub.types.carbon.PaginatedLogFileInfo;

import javax.activation.DataHandler;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.rmi.RemoteException;

public class LogViewerClient {
    private static final Log log = LogFactory.getLog(LogViewerClient.class);
    public LogViewerStub stub;

    public LogViewerClient(String cookie, String backendServerURL, ConfigurationContext configCtx)
            throws AxisFault {
        String serviceURL = backendServerURL + "LogViewer";
        stub = new LogViewerStub(configCtx, serviceURL);
        ServiceClient client = stub._getServiceClient();
        Options option = client.getOptions();
        option.setManageSession(true);
        option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, cookie);
//        option.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    }


    public void clearLogs() throws Exception {
        stub.clearLogs();
    }

    public void downloadArchivedLogFiles(String logFile, HttpServletResponse response,
                                         String tenantDomain, String serverKey)
            throws Exception {
        try {
            logFile = logFile.replace(".gz", "");
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/txt");
            response.setHeader("Content-Disposition",
                               "attachment;filename=" + logFile.replaceAll("\\s", "_"));
            DataHandler data = stub.downloadArchivedLogFiles(logFile, tenantDomain, serverKey);
            InputStream fileToDownload = data.getInputStream();
            int c;
            while ((c = fileToDownload.read()) != -1) {
                outputStream.write(c);
            }
            outputStream.flush();
            outputStream.flush();
        } catch (Exception e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public int getLineNumbers(String logFile)
            throws Exception {
        try {
            return stub.getLineNumbers(logFile);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public PaginatedLogFileInfo getPaginatedLogFileInfo(int pageNumber, String tenantDomain,
                                                        String serviceName) throws Exception {
        try {
            return stub.getPaginatedLogFileInfo(pageNumber, tenantDomain, serviceName);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public LogEvent[] getLogs(String type, String keyword, String tenantDomain, String serverkey)
            throws Exception {
        if (type == null || type.equals("")) {
            type = "ALL";
        }
        try {
            return stub.getLogs(type, keyword, tenantDomain, serverkey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public LogEvent[] getApplicationLogs(String type, String keyword, String applicationName,
                                         String tenantDomain, String serverKey)
            throws RemoteException, LogViewerLogViewerException {
        if (type == null || type.equals("")) {
            type = "ALL";
        }
        if (applicationName == null || applicationName.equals("")) {
            applicationName = "FIRST";
        }
        try {
            return stub.getApplicationLogs(type, keyword, applicationName, tenantDomain, serverKey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        } catch (LogViewerLogViewerException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public String[] getLogLinesFromFile(String logFile, int maxLogs, int start, int end)
            throws RemoteException, LogViewerLogViewerException {
        try {
            return stub.getLogLinesFromFile(logFile, maxLogs, start, end);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        } catch (LogViewerLogViewerException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public String[] getApplicationNames(String tenantDomain, String serverKey)
            throws LogViewerLogViewerException, RemoteException {
        try {
            return stub.getApplicationNames(tenantDomain, serverKey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public int getNoOfLogEvents() throws Exception {
        try {
            return 20;// stub.getNoOfLogEvents();
        } catch (Exception e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public String[] getServiceNames() throws RemoteException, LogViewerLogViewerException {
        try {
            return stub.getServiceNames();
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public boolean isFileAppenderConfiguredForST() throws RemoteException {
        try {
            return stub.isFileAppenderConfiguredForST();
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public PaginatedLogEvent getPaginatedLogEvents(int pageNumber, String type, String keyword,
                                                   String tenantDomain, String serverKey)
            throws Exception {
        try {
            return stub.getPaginatedLogEvents(pageNumber, type, keyword, tenantDomain, serverKey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public PaginatedLogEvent getPaginatedApplicationLogEvents(int pageNumber, String type,
                                                              String keyword, String appName,
                                                              String tenantDomain, String serverKey)
            throws Exception {
        try {
            return stub.getPaginatedApplicationLogEvents(pageNumber, type, keyword, appName,
                                                         tenantDomain, serverKey);
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }

    public PaginatedLogFileInfo getLocalLogFiles(int pageNo, String tenantDomain, String serverKey)
            throws RemoteException, LogViewerLogViewerException {

        String msg = "Error occurred while getting logger data. Backend service may be unavailable";
        try {
            return stub.getLocalLogFiles(pageNo, tenantDomain, serverKey);
        } catch (RemoteException e) {
            log.error(msg, e);
            throw e;
        } catch (LogViewerLogViewerException e) {
            log.error(msg, e);
            throw e;
        }

    }

    public boolean isLogEventReciverConfigured() throws RemoteException {
        try {
            return stub.isLogEventReciverConfigured();
        } catch (RemoteException e) {
            String msg = "Error occurred while getting logger data. Backend service may be " +
                         "unavailable";
            log.error(msg, e);
            throw e;
        }
    }
{'fraction_non_alphanumeric': 0.0830188679245283, 'fraction_numerical': 0.0028176100628930817, 'mean_word_length': 2.1379854752131355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '686207', 'n_tokens_mistral': 4961, 'n_tokens_neox': 4870, 'n_words': 1457}