http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrAuditLogData.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrAuditLogData.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrAuditLogData.java new file mode 100644 index 0000000..9de05db --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrAuditLogData.java @@ -0,0 +1,341 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.solr.model; + +import org.apache.ambari.logsearch.model.response.AuditLogData; +import org.apache.solr.client.solrj.beans.Field; + +import java.util.Date; +import java.util.List; + +import static org.apache.ambari.logsearch.solr.SolrConstants.AuditLogConstants.*; + +public class SolrAuditLogData extends SolrCommonLogData implements AuditLogData { + + @Field(AUDIT_LOG_TYPE) + private String logType; + + @Field(AUDIT_POLICY) + private String policy; + + @Field(AUDIT_ACCESS) + private String access; + + @Field(AUDIT_ACTION) + private String action; + + @Field(AUDIT_AGENT) + private String agent; + + @Field(AUDIT_AGENT_HOST) + private String agentHost; + + @Field(AUDIT_CLIENT_IP) + private String clientIp; + + @Field(AUDIT_CLIENT_TYPE) + private String clientType; + + @Field(AUDIT_REQEST_CONTEXT) + private String requestContext; + + @Field(AUDIT_ENFORCER) + private String enforcer; + + @Field(AUDIT_EVTTIME) + private Date eventTime; + + @Field(AUDIT_REASON) + private String reason; + + @Field(AUDIT_PROXY_USERS) + private List<String> proxyUsers; + + @Field(AUDIT_COMPONENT) + private String repo; + + @Field(AUDIT_REPO_TYPE) + private Integer repoType; + + @Field(AUDIT_REQEST_DATA) + private String requestData; + + @Field(AUDIT_REQUEST_USER) + private String requestUser; + + @Field(AUDIT_RESPONSE_TYPE) + private String responseType; + + @Field(AUDIT_RESOURCE) + private String resource; + + @Field(AUDIT_RESULT) + private Integer result; + + @Field(AUDIT_SESSION) + private String session; + + @Field(AUDIT_TAGS) + private List<String> tags; + + @Field(AUDIT_TAGS_STR) + private String tagsStr; + + @Field(AUDIT_TEXT) + private String text; + + @Override + public String getText() { + return text; + } + + @Override + public void setText(String text) { + this.text = text; + } + + @Override + public String getTagsStr() { + return tagsStr; + } + + @Override + public void setTagsStr(String tagsStr) { + this.tagsStr = tagsStr; + } + + @Override + public List<String> getTags() { + return tags; + } + + @Override + public void setTags(List<String> tags) { + this.tags = tags; + } + + @Override + public String getSession() { + return session; + } + + @Override + public void setSession(String session) { + this.session = session; + } + + @Override + public Integer getResult() { + return result; + } + + @Override + public void setResult(Integer result) { + this.result = result; + } + + @Override + public String getResource() { + return resource; + } + + @Override + public void setResource(String resource) { + this.resource = resource; + } + + @Override + public String getResponseType() { + return responseType; + } + + public void setResponseType(String responseType) { + this.responseType = responseType; + } + + @Override + public String getRequestUser() { + return requestUser; + } + + @Override + public void setRequestUser(String requestUser) { + this.requestUser = requestUser; + } + + @Override + public String getRequestData() { + return requestData; + } + + @Override + public void setRequestData(String requestData) { + this.requestData = requestData; + } + + @Override + public Integer getRepoType() { + return repoType; + } + + @Override + public void setRepoType(Integer repoType) { + this.repoType = repoType; + } + + @Override + public String getRepo() { + return repo; + } + + @Override + public void setRepo(String repo) { + this.repo = repo; + } + + @Override + public List<String> getProxyUsers() { + return proxyUsers; + } + + @Override + public void setProxyUsers(List<String> proxyUsers) { + this.proxyUsers = proxyUsers; + } + + @Override + public String getReason() { + return reason; + } + + @Override + public void setReason(String reason) { + this.reason = reason; + } + + @Override + public Date getEventTime() { + return eventTime; + } + + @Override + public void setEventTime(Date eventTime) { + this.eventTime = eventTime; + } + + @Override + public String getEnforcer() { + return enforcer; + } + + @Override + public void setEnforcer(String enforcer) { + this.enforcer = enforcer; + } + + @Override + public String getRequestContext() { + return requestContext; + } + + @Override + public void setRequestContext(String requestContext) { + this.requestContext = requestContext; + } + + @Override + public String getClientType() { + return clientType; + } + + @Override + public void setClientType(String clientType) { + this.clientType = clientType; + } + + @Override + public String getClientIp() { + return clientIp; + } + + @Override + public void setClientIp(String clientIp) { + this.clientIp = clientIp; + } + + @Override + public String getAgent() { + return agent; + } + + @Override + public void setAgent(String agent) { + this.agent = agent; + } + + @Override + public String getAgentHost() { + return agentHost; + } + + @Override + public void setAgentHost(String agentHost) { + this.agentHost = agentHost; + } + + @Override + public String getAction() { + return action; + } + + @Override + public void setAction(String action) { + this.action = action; + } + + @Override + public String getAccess() { + return access; + } + + @Override + public void setAccess(String access) { + this.access = access; + } + + @Override + public String getPolicy() { + return policy; + } + + @Override + public void setPolicy(String policy) { + this.policy = policy; + } + + @Override + public String getLogType() { + return logType; + } + + @Override + public void setLogType(String logType) { + this.logType = logType; + } +}
http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrCommonLogData.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrCommonLogData.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrCommonLogData.java new file mode 100644 index 0000000..b5ce7ad --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrCommonLogData.java @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.solr.model; + +import org.apache.ambari.logsearch.model.response.CommonLogData; +import org.apache.solr.client.solrj.beans.Field; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import static org.apache.ambari.logsearch.solr.SolrConstants.CommonLogConstants.*; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.KEY_DYNAMIC_FIELDS; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.STORED_TOKEN_DYNAMIC_FIELDS; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.WS_DYNAMIC_FIELDS; + +public class SolrCommonLogData implements CommonLogData { + + @Field(ID) + private String id; + + @Field(BUNDLE_ID) + private String bundleId; + + @Field(CASE_ID) + private String caseId; + + @Field(CLUSTER) + private String cluster; + + @Field(SEQUENCE_ID) + private Long seqNum; + + @Field(LOG_MESSAGE) + private String logMessage; + + @Field(LOGFILE_LINE_NUMBER) + private Integer logFileLineNumber; + + @Field(EVENT_DURATION_MD5) + private Long eventDurationMs; + + @Field(FILE) + private String file; + + @Field(TYPE) + private String type; + + @Field(EVENT_COUNT) + private Long eventCount; + + @Field(EVENT_MD5) + private String eventMd5; + + @Field(MESSAGE_MD5) + private String messageMd5; + + @Field(TTL) + private String ttl; + + @Field(EXPIRE_AT) + private Date expire; + + @Field(VERSION) + private Long version; + + @Field(ROUTER_FIELD) + private Integer routerField; + + @Field(STORED_TOKEN_DYNAMIC_FIELDS) + private Map<String, Object> stdDynamicFields; + + @Field(KEY_DYNAMIC_FIELDS) + private Map<String, Object> keyDynamicFields; + + @Field(WS_DYNAMIC_FIELDS) + private Map<String, Object> wsDynamicFields; + + @Override + public String getId() { + return this.id; + } + + @Override + public void setId(String id) { + this.id = id; + } + + @Override + public String getCaseId() { + return this.caseId; + } + + @Override + public void setCaseId(String caseId) { + this.caseId = caseId; + } + + @Override + public String getLogMessage() { + return this.logMessage; + } + + @Override + public String getBundleId() { + return bundleId; + } + + @Override + public void setBundleId(String bundleId) { + this.bundleId = bundleId; + } + + @Override + public Integer getLogFileLineNumber() { + return logFileLineNumber; + } + + @Override + public void setLogFileLineNumber(Integer logFileLineNumber) { + this.logFileLineNumber = logFileLineNumber; + } + + @Override + public void setLogMessage(String logMessage) { + this.logMessage = logMessage; + } + + @Override + public Long getEventDurationMs() { + return eventDurationMs; + } + + @Override + public void setEventDurationMs(Long eventDurationMs) { + this.eventDurationMs = eventDurationMs; + } + + @Override + public String getFile() { + return file; + } + + @Override + public void setFile(String file) { + this.file = file; + } + + @Override + public Long getSeqNum() { + return seqNum; + } + + @Override + public void setSeqNum(Long seqNum) { + this.seqNum = seqNum; + } + + @Override + public String getMessageMd5() { + return messageMd5; + } + + @Override + public void setMessageMd5(String messageMd5) { + this.messageMd5 = messageMd5; + } + + @Override + public String getEventMd5() { + return eventMd5; + } + + @Override + public void setEventMd5(String eventMd5) { + this.eventMd5 = eventMd5; + } + + @Override + public String getCluster() { + return cluster; + } + + @Override + public void setCluster(String cluster) { + this.cluster = cluster; + } + + @Override + public Long getEventCount() { + return eventCount; + } + + @Override + public void setEventCount(Long eventCount) { + this.eventCount = eventCount; + } + + @Override + public String getTtl() { + return this.ttl; + } + + @Override + public void setTtl(String ttl) { + this.ttl = ttl; + } + + @Override + public Date getExpire() { + return expire; + } + + @Override + public void setExpire(Date expire) { + this.expire = expire; + } + + @Override + public Long getVersion() { + return version; + } + + @Override + public void setVersion(Long version) { + this.version = version; + } + + @Override + public Integer getRouterField() { + return this.routerField; + } + + @Override + public void setRouterField(Integer routerField) { + this.routerField = routerField; + } + + @Override + public String getType() { + return type; + } + + @Override + public void setType(String type) { + this.type = type; + } + + @Override + public Map<String, Object> getAllDynamicFields() { + Map<String, Object> allDynamicFields = new HashMap<>(); + if (stdDynamicFields != null) { + allDynamicFields.putAll(stdDynamicFields); + } + if (keyDynamicFields != null) { + allDynamicFields.putAll(keyDynamicFields); + } + if (wsDynamicFields != null) { + allDynamicFields.putAll(wsDynamicFields); + } + + return allDynamicFields; + } + + public void setStdDynamicFields(Map<String, Object> stdDynamicFields) { + this.stdDynamicFields = stdDynamicFields; + } + + public void setKeyDynamicFields(Map<String, Object> keyDynamicFields) { + this.keyDynamicFields = keyDynamicFields; + } + + public void setWsDynamicFields(Map<String, Object> wsDynamicFields) { + this.wsDynamicFields = wsDynamicFields; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrComponentTypeLogData.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrComponentTypeLogData.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrComponentTypeLogData.java new file mode 100644 index 0000000..1a63700 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrComponentTypeLogData.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.solr.model; + +import org.apache.ambari.logsearch.model.response.ComponentTypeLogData; +import org.apache.solr.client.solrj.beans.Field; + +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.COMPONENT; + +public class SolrComponentTypeLogData implements ComponentTypeLogData { + + @Field(COMPONENT) + private String type; + + @Override + public String getType() { + return this.type; + } + + @Override + public void setType(String type) { + this.type = type; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrHostLogData.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrHostLogData.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrHostLogData.java new file mode 100644 index 0000000..8391815 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrHostLogData.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.solr.model; + +import org.apache.ambari.logsearch.model.response.HostLogData; +import org.apache.solr.client.solrj.beans.Field; + +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.HOST; + +public class SolrHostLogData implements HostLogData { + + @Field(HOST) + private String host; + + @Override + public String getHost() { + return host; + } + + @Override + public void setHost(String host) { + this.host = host; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrServiceLogData.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrServiceLogData.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrServiceLogData.java new file mode 100644 index 0000000..c6fdba3 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/solr/model/SolrServiceLogData.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.solr.model; + +import org.apache.ambari.logsearch.model.response.ServiceLogData; +import org.apache.solr.client.solrj.beans.Field; + +import java.util.Date; + +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.*; + +public class SolrServiceLogData extends SolrCommonLogData implements ServiceLogData { + + @Field(LEVEL) + private String level; + + @Field(LINE_NUMBER) + private Integer lineNumber; + + @Field(LOGTIME) + private Date logTime; + + @Field(COMPONENT) + private String type; + + @Field(IP) + private String ip; + + @Field(PATH) + private String path; + + @Field(HOST) + private String host; + + @Override + public String getPath() { + return path; + } + + @Override + public void setPath(String path) { + this.path = path; + } + + @Override + public String getIp() { + return ip; + } + + @Override + public void setIp(String ip) { + this.ip = ip; + } + + @Override + public String getType() { + return type; + } + + @Override + public void setType(String type) { + this.type = type; + } + + @Override + public String getHost() { + return host; + } + + @Override + public void setHost(String host) { + this.host = host; + } + + @Override + public Date getLogTime() { + return logTime; + } + + @Override + public void setLogTime(Date logTime) { + this.logTime = logTime; + } + + @Override + public Integer getLineNumber() { + return lineNumber; + } + + @Override + public void setLineNumber(Integer lineNumber) { + this.lineNumber = lineNumber; + } + + @Override + public String getLevel() { + return level; + } + + @Override + public void setLevel(String level) { + this.level = level; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/CommonUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/CommonUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/CommonUtil.java new file mode 100644 index 0000000..1cfe469 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/CommonUtil.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import java.security.SecureRandom; + +import org.springframework.security.authentication.encoding.Md5PasswordEncoder; + +public class CommonUtil { + private CommonUtil() { + throw new UnsupportedOperationException(); + } + + private static SecureRandom secureRandom = new SecureRandom(); + private static int counter = 0; + + public static String genGUI() { + return System.currentTimeMillis() + "_" + secureRandom.nextInt(1000) + "_" + counter++; + } + + private static final Md5PasswordEncoder md5Encoder = new Md5PasswordEncoder(); + public static String encryptPassword(String username, String password) { + return md5Encoder.encodePassword(password, username); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/DateUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/DateUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/DateUtil.java new file mode 100644 index 0000000..9b4f553 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/DateUtil.java @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; + +import org.apache.ambari.logsearch.common.LogSearchConstants; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.time.DateUtils; +import org.apache.log4j.Logger; + +public class DateUtil { + + private static final Logger logger = Logger.getLogger(DateUtil.class); + + private DateUtil() { + throw new UnsupportedOperationException(); + } + + public static String addOffsetToDate(String date, Long utcOffset, String dateFormat) { + if (StringUtils.isBlank(date)) { + logger.debug("input date is empty or null."); + return null; + } + if (utcOffset == null) { + logger.debug("Utc offset is null, Return input date without adding offset."); + return date; + } + if (StringUtils.isBlank(dateFormat)) { + logger.debug("dateFormat is null or empty, Return input date without adding offset."); + return date; + } + String retDate = ""; + try { + String modifiedDate = date; + if (date.contains(".")) { + modifiedDate = date.replace(".", ","); + } + SimpleDateFormat formatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH); + Date startDate = formatter.parse(modifiedDate); + long toWithOffset = startDate.getTime() + TimeUnit.MINUTES.toMillis(utcOffset); + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(toWithOffset); + retDate = formatter.format(calendar.getTime()); + } catch (Exception e) { + logger.error(e); + } + return retDate; + } + + public static String getCurrentDateInString() { + DateFormat df = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss", Locale.ENGLISH); + Date today = Calendar.getInstance().getTime(); + return df.format(today); + } + + public static Date getTodayFromDate() { + return DateUtils.truncate(new Date(), Calendar.DATE); + } + + public static String convertGivenDateFormatToSolrDateFormat(Date date) throws ParseException { + String time = date.toString(); + SimpleDateFormat input = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); + SimpleDateFormat output = new SimpleDateFormat(LogSearchConstants.SOLR_DATE_FORMAT_PREFIX_Z, Locale.ENGLISH); + Date d = input.parse(time); + TimeZone timeZone = TimeZone.getTimeZone("UTC"); + output.setTimeZone(timeZone); + + return output.format(d); + } + + public static String convertDateWithMillisecondsToSolrDate(Date date) { + if (date == null) { + return ""; + } + SimpleDateFormat formatter = new SimpleDateFormat(LogSearchConstants.SOLR_DATE_FORMAT_PREFIX_Z, Locale.ENGLISH); + TimeZone timeZone = TimeZone.getTimeZone("GMT"); + formatter.setTimeZone(timeZone); + + return formatter.format(date); + } + + public static String convertSolrDateToNormalDateFormat(long d, long utcOffset) throws ParseException { + Date date = new Date(d); + SimpleDateFormat formatter = new SimpleDateFormat(LogSearchConstants.SOLR_DATE_FORMAT, Locale.ENGLISH); + TimeZone timeZone = TimeZone.getTimeZone("GMT"); + formatter.setTimeZone(timeZone); + String stringDate = formatter.format(date); + return addOffsetToDate(stringDate, Long.parseLong("" + utcOffset), LogSearchConstants.SOLR_DATE_FORMAT); + + } + + public static Date convertStringToSolrDate(String dateStr) { + try { + SimpleDateFormat formatter = new SimpleDateFormat(LogSearchConstants.SOLR_DATE_FORMAT_PREFIX_Z); + return formatter.parse(dateStr); + } catch (Exception e){ + throw new RuntimeException("Cannot parse date from request", e.getCause()); + } + } + + public static boolean isDateValid(String value) { + if (StringUtils.isBlank(value)) { + return false; + } + Date date = null; + try { + SimpleDateFormat sdf = new SimpleDateFormat(LogSearchConstants.SOLR_DATE_FORMAT_PREFIX_Z); + date = sdf.parse(value); + if (!value.equals(sdf.format(date))) { + date = null; + } + } catch (Exception ex) { + // do nothing + } + return date != null; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/DownloadUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/DownloadUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/DownloadUtil.java new file mode 100644 index 0000000..debe131 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/DownloadUtil.java @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import com.google.common.base.Splitter; +import org.apache.ambari.logsearch.common.LogSearchConstants; +import org.apache.ambari.logsearch.model.request.impl.ServiceLogExportRequest; +import org.apache.ambari.logsearch.model.response.BarGraphData; +import org.apache.ambari.logsearch.model.response.BarGraphDataListResponse; +import org.apache.ambari.logsearch.model.response.NameValueData; +import org.apache.ambari.logsearch.model.response.TemplateData; +import org.apache.commons.lang.StringUtils; +import org.apache.solr.common.SolrDocument; +import org.apache.solr.common.SolrDocumentList; + +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.LINE_NUMBER; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.LOGTIME; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.LOG_MESSAGE; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.LEVEL; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.HOST; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.COMPONENT; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.LOGGER_NAME; +import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.THREAD_NAME; +import static org.apache.ambari.logsearch.solr.SolrConstants.CommonLogConstants.FILE; + + +public class DownloadUtil { + + private DownloadUtil() { + throw new UnsupportedOperationException(); + } + + public static void fillModelsForLogFile(SolrDocumentList docList, Map<String, Object> models, ServiceLogExportRequest request, + String format, String from, String to) { + long numLogs = docList.getNumFound(); + List<String> hosts = new ArrayList<>(); + List<String> components = new ArrayList<>(); + List<String> levels = new ArrayList<>(); + List<TemplateData> logData = new ArrayList<>(); + for (SolrDocument doc : docList) { + if (doc != null) { + String hostname = (String) doc.getFieldValue(HOST); + String comp = (String) doc.getFieldValue(COMPONENT); + String level = (String) doc.getFieldValue(LEVEL); + + if (!hosts.contains(hostname)) { + hosts.add(hostname); + } + + if (!components.contains(comp)) { + components.add(comp); + } + + if (!levels.contains(level)) { + levels.add(level); + } + + StringBuffer textToWrite = new StringBuffer(); + + if (doc.getFieldValue(LOGTIME) != null) { + textToWrite.append(doc.getFieldValue(LOGTIME).toString() + " "); + } + if (doc.getFieldValue(LEVEL) != null) { + textToWrite.append(doc.getFieldValue(LEVEL).toString()).append(" "); + } + if (doc.getFieldValue(THREAD_NAME) != null) { + textToWrite.append(doc.getFieldValue(THREAD_NAME).toString().trim()).append(" "); + } + if (doc.getFieldValue(LOGGER_NAME) != null) { + textToWrite.append(doc.getFieldValue(LOGGER_NAME).toString().trim()).append(" "); + } + if (doc.getFieldValue(FILE) != null && doc.getFieldValue(LINE_NUMBER) != null) { + textToWrite + .append(doc.getFieldValue(FILE).toString()) + .append(":") + .append(doc.getFieldValue(LINE_NUMBER).toString()) + .append(" "); + } + if (doc.getFieldValue(LOG_MESSAGE) != null) { + textToWrite.append("- ") + .append(doc.getFieldValue(LOG_MESSAGE).toString()); + } + logData.add(new TemplateData((textToWrite.toString()))); + } + } + models.put("numberOfLogs", numLogs); + models.put("logs", logData); + models.put("hosts", "[ " + StringUtils.join(hosts, " ; ") + " ]"); + models.put("components", "[ " + StringUtils.join(components, " ; ") + " ]"); + models.put("format", format); + models.put("from", from); + models.put("levels", StringUtils.join(levels, ", ")); + models.put("to", to); + String includeString = request.getiMessage(); + if (StringUtils.isBlank(includeString)) { + includeString = "\"\""; + } else { + List<String> include = Splitter.on(request.getiMessage()).splitToList(LogSearchConstants.I_E_SEPRATOR); + includeString = "\"" + StringUtils.join(include, "\", \"") + "\""; + } + models.put("iString", includeString); + + String excludeString = request.geteMessage(); + if (StringUtils.isBlank(excludeString)) { + excludeString = "\"\""; + } else { + List<String> exclude = Splitter.on(request.getiMessage()).splitToList(LogSearchConstants.I_E_SEPRATOR); + excludeString = "\"" + StringUtils.join(exclude, "\", \"") + "\""; + } + models.put("eString", excludeString); + } + + public static void fillUserResourcesModel(Map<String, Object> models, BarGraphDataListResponse vBarUserDataList, BarGraphDataListResponse vBarResourceDataList) { + List<TemplateData> usersDataList = new ArrayList<>(); + List<TemplateData> resourceDataList = new ArrayList<>(); + Collection<BarGraphData> tableUserData = vBarUserDataList.getGraphData(); + for (BarGraphData graphData : tableUserData) { + String userName = graphData.getName().length() > 45 ? graphData.getName().substring(0, 45) : graphData.getName(); + Collection<NameValueData> vnameValueList = graphData.getDataCount(); + usersDataList.add(new TemplateData(appendNameValueData(addBlank(userName), vnameValueList))); + } + Collection<BarGraphData> tableResourceData = vBarResourceDataList.getGraphData(); + for (BarGraphData graphData : tableResourceData) { + String resourceName = graphData.getName().length() > 45 ? graphData.getName().substring(0, 45) : graphData.getName(); + Collection<NameValueData> vnameValueList = graphData.getDataCount(); + resourceDataList.add(new TemplateData(appendNameValueData(addBlank(resourceName), vnameValueList))); + } + models.put("users", usersDataList); + models.put("resources", resourceDataList); + models.put("usersSummary", vBarUserDataList.getGraphData().size()); + models.put("resourcesSummary", vBarResourceDataList.getGraphData().size()); + } + + private static String appendNameValueData(String data, Collection<NameValueData> vnameValueList) { + int count = 0; + String blank = ""; + for (NameValueData vNameValue : vnameValueList) { + data += blank + vNameValue.getName() + " " + vNameValue.getValue(); + if (count == 0) + blank = addBlank(blank); + count++; + } + return data; + } + + private static String addBlank(String field) { + int blanks = 50; + int strSize = field.length(); + String fieldWithBlank = field; + for (int i = 0; i < blanks - strSize; i++) { + fieldWithBlank += " "; + } + return fieldWithBlank; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java new file mode 100644 index 0000000..5d4efbc --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import java.io.File; +import java.net.URL; + +import org.apache.log4j.Logger; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.Chmod; +import org.apache.tools.ant.types.FileSet; + +public class FileUtil { + private static final Logger logger = Logger.getLogger(FileUtil.class); + + private FileUtil() { + throw new UnsupportedOperationException(); + } + + public static File getFileFromClasspath(String filename) { + URL fileCompleteUrl = Thread.currentThread().getContextClassLoader().getResource(filename); + logger.debug("File Complete URI :" + fileCompleteUrl); + File file = null; + try { + file = new File(fileCompleteUrl.toURI()); + } catch (Exception exception) { + logger.debug(exception.getMessage(), exception.getCause()); + } + return file; + } + + public static void createDirectory(String dirPath) { + File dir = new File(dirPath); + if (!dir.exists()) { + logger.info("Directory " + dirPath + " does not exist. Creating ..."); + boolean mkDirSuccess = dir.mkdirs(); + if (!mkDirSuccess) { + String errorMessage = String.format("Could not create directory %s", dirPath); + logger.error(errorMessage); + throw new RuntimeException(errorMessage); + } + } + } + + public static void setPermissionOnDirectory(String dirPath, String permission) { + Chmod chmod = new Chmod(); + chmod.setProject(new Project()); + FileSet fileSet = new FileSet(); + fileSet.setDir(new File(dirPath)); + fileSet.setIncludes("**"); + chmod.addFileset(fileSet); + chmod.setPerm(permission); + chmod.execute(); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/JSONUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/JSONUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/JSONUtil.java new file mode 100644 index 0000000..d08c0f4 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/JSONUtil.java @@ -0,0 +1,245 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.ambari.logsearch.common.MessageEnums; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.codehaus.jackson.JsonParseException; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; +import org.codehaus.jettison.json.JSONArray; +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class JSONUtil { + private static final Logger logger = Logger.getLogger(JSONUtil.class); + + private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; + private static final Gson gson = new GsonBuilder().setDateFormat(DATE_FORMAT).create(); + + private JSONUtil() { + throw new UnsupportedOperationException(); + } + + @SuppressWarnings("unchecked") + public static HashMap<String, Object> jsonToMapObject(String jsonStr) { + if (StringUtils.isBlank(jsonStr)) { + logger.info("jsonString is empty, cannot conver to map"); + return null; + } + ObjectMapper mapper = new ObjectMapper(); + try { + Object tempObject = mapper.readValue(jsonStr, new TypeReference<HashMap<String, Object>>() {}); + return (HashMap<String, Object>) tempObject; + + } catch (JsonParseException e) { + throw RESTErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA); + } catch (JsonMappingException e) { + throw RESTErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA); + } catch (IOException e) { + throw RESTErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA); + } + + } + + @SuppressWarnings("unchecked") + public static List<HashMap<String, Object>> jsonToMapObjectList(String jsonStr) { + if (StringUtils.isBlank(jsonStr)) { + return null; + } + ObjectMapper mapper = new ObjectMapper(); + try { + Object tempObject = mapper.readValue(jsonStr, new TypeReference<List<HashMap<String, Object>>>() {}); + return (List<HashMap<String, Object>>) tempObject; + + } catch (JsonParseException e) { + throw RESTErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA); + } catch (JsonMappingException e) { + throw RESTErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA); + } catch (IOException e) { + throw RESTErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA); + } + + } + + public static boolean isJSONValid(String jsonString) { + try { + new JSONObject(jsonString); + } catch (JSONException ex) { + try { + new JSONArray(jsonString); + } catch (JSONException ex1) { + return false; + } + } + return true; + } + + public static HashMap<String, Object> readJsonFromFile(File jsonFile) { + ObjectMapper mapper = new ObjectMapper(); + try { + HashMap<String, Object> jsonmap = mapper.readValue(jsonFile, new TypeReference<HashMap<String, Object>>() {}); + return jsonmap; + } catch (IOException e) { + logger.error(e, e.getCause()); + } + return new HashMap<String, Object>(); + } + + public static String mapToJSON(Map<String, Object> map) { + ObjectMapper om = new ObjectMapper(); + try { + String json = om.writeValueAsString(map); + + return json; + } catch (IOException e) { + logger.error(e, e.getCause()); + } + return ""; + } + + /** + * WRITE JOSN IN FILE ( Delete existing file and create new file) + */ + public static synchronized void writeJSONInFile(String jsonStr, File outputFile, boolean beautify) { + FileWriter fileWriter = null; + if (outputFile == null) { + logger.error("user_pass json file can't be null."); + return; + } + try { + boolean writePermission = false; + if (outputFile.exists() && outputFile.canWrite()) { + writePermission = true; + } + if (writePermission) { + fileWriter = new FileWriter(outputFile); + if (beautify) { + ObjectMapper mapper = new ObjectMapper(); + Object json = mapper.readValue(jsonStr, Object.class); + jsonStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json); + } + fileWriter.write(jsonStr); + } else { + logger.error("Applcation does not have permission to update file to write enc_password. file="+ outputFile.getAbsolutePath()); + } + } catch (IOException e) { + logger.error("Error writing to password file.", e.getCause()); + } finally { + if (fileWriter != null) { + try { + fileWriter.flush(); + fileWriter.close(); + } catch (Exception exception) { + logger.error(exception); + } + } + } + } + + public static String objToJson(Object obj) { + return gson.toJson(obj); + } + + public static Object jsonToObj(String json, Class<?> klass) { + return gson.fromJson(json, klass); + } + + /** + * GET VALUES FROM JSON BY GIVING KEY RECURSIVELY + */ + @SuppressWarnings("rawtypes") + public static String getValuesOfKey(String jsonStr, String keyName, List<String> values) { + if (values == null) { + return null; + } + Object jsonObj = null; + try { + jsonObj = new JSONObject(jsonStr); + } catch (Exception e) { + // ignore + } + if (jsonObj == null) { + try { + JSONArray jsonArray = new JSONArray(jsonStr); + String str = null; + for (int i = 0; i < jsonArray.length(); i++) { + + str = getValuesOfKey(jsonArray.getString(i), keyName, values); + if (str != null) { + return str; + } + } + + } catch (Exception e) { + // ignore + } + } + if (jsonObj == null) { + return null; + } + + Iterator iterator = ((JSONObject) jsonObj).keys(); + if (iterator == null) { + return null; + } + while (iterator.hasNext()) { + String key = (String) iterator.next(); + + if (key != null && key.equals(keyName)) { + + try { + String val = ((JSONObject) jsonObj).getString(key); + values.add(val); + } catch (Exception e) { + // ignore + } + + } else if ((((JSONObject) jsonObj).optJSONArray(key) != null) || (((JSONObject) jsonObj).optJSONObject(key) != null)) { + + String str = null; + try { + str = getValuesOfKey("" + ((JSONObject) jsonObj).getString(key), keyName, values); + } catch (Exception e) { + // ignore + } + if (str != null) { + return str; + } + + } + + } + return null; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/RESTErrorUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/RESTErrorUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/RESTErrorUtil.java new file mode 100644 index 0000000..b1a53f2 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/RESTErrorUtil.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +import org.apache.ambari.logsearch.common.MessageEnums; +import org.apache.ambari.logsearch.common.MessageData; +import org.apache.ambari.logsearch.common.VResponse; +import org.apache.log4j.Logger; + +public class RESTErrorUtil { + private static final Logger logger = Logger.getLogger(RESTErrorUtil.class); + + private RESTErrorUtil() { + throw new UnsupportedOperationException(); + } + + public static WebApplicationException createRESTException(VResponse response) { + return createRESTException(response, HttpServletResponse.SC_BAD_REQUEST); + } + + public static VResponse createMessageResponse(String errorMessage, MessageEnums messageEnum) { + List<MessageData> messageList = new ArrayList<>(); + messageList.add(messageEnum.getMessage()); + VResponse response = new VResponse(); + response.setStatusCode(VResponse.STATUS_ERROR); + response.setMsgDesc(errorMessage); + response.setMessageList(messageList); + return response; + } + + public static WebApplicationException createRESTException(String errorMessage, MessageEnums messageEnum) { + VResponse response = createMessageResponse(errorMessage, messageEnum); + WebApplicationException webAppEx = createRESTException(response); + logger.error("Operation error. response=" + response, webAppEx); + return webAppEx; + } + + private static WebApplicationException createRESTException(VResponse response, int sc) { + Response errorResponse = Response.status(sc).entity(response).build(); + WebApplicationException restException = new WebApplicationException(errorResponse); + restException.fillInStackTrace(); + return restException; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/SSLUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/SSLUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/SSLUtil.java new file mode 100644 index 0000000..d4b6544 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/SSLUtil.java @@ -0,0 +1,363 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import javax.net.ssl.SSLContext; + +import org.apache.ambari.logsearch.common.PropertiesHelper; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.hadoop.conf.Configuration; +import org.bouncycastle.asn1.ASN1InputStream; +import org.bouncycastle.asn1.x500.X500Name; +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.operator.ContentSigner; +import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder; +import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder; +import org.bouncycastle.operator.OperatorCreationException; +import org.bouncycastle.operator.bc.BcContentSignerBuilder; +import org.bouncycastle.operator.bc.BcRSAContentSignerBuilder; +import org.bouncycastle.cert.X509CertificateHolder; +import org.bouncycastle.cert.X509v3CertificateBuilder; +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; +import org.bouncycastle.crypto.params.RSAKeyParameters; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.net.InetAddress; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.KeyStore; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.security.cert.Certificate; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.util.Date; + +public class SSLUtil { + private static final Logger LOG = LoggerFactory.getLogger(SSLUtil.class); + + private static final String KEYSTORE_LOCATION_ARG = "javax.net.ssl.keyStore"; + private static final String KEYSTORE_PASSWORD_ARG = "javax.net.ssl.keyStorePassword"; + private static final String KEYSTORE_TYPE_ARG = "javax.net.ssl.keyStoreType"; + private static final String DEFAULT_KEYSTORE_TYPE = "JKS"; + private static final String TRUSTSTORE_LOCATION_ARG = "javax.net.ssl.trustStore"; + private static final String TRUSTSTORE_PASSWORD_ARG = "javax.net.ssl.trustStorePassword"; + private static final String TRUSTSTORE_TYPE_ARG = "javax.net.ssl.trustStoreType"; + private static final String DEFAULT_TRUSTSTORE_TYPE = "JKS"; + private static final String KEYSTORE_PASSWORD_PROPERTY_NAME = "logsearch_keystore_password"; + private static final String TRUSTSTORE_PASSWORD_PROPERTY_NAME = "logsearch_truststore_password"; + private static final String KEYSTORE_PASSWORD_FILE = "ks_pass.txt"; + private static final String TRUSTSTORE_PASSWORD_FILE = "ts_pass.txt"; + private static final String CREDENTIAL_STORE_PROVIDER_PATH = "hadoop.security.credential.provider.path"; + + private static final String LOGSEARCH_CERT_FOLDER_LOCATION = "logsearch.cert.folder.location"; + private static final String LOGSEARCH_CERT_ALGORITHM = "logsearch.cert.algorithm"; + + private static final String LOGSEARCH_CERT_FILENAME = "logsearch.crt"; + private static final String LOGSEARCH_KEYSTORE_FILENAME = "logsearch.jks"; + private static final String LOGSEARCH_KEYSTORE_PRIVATE_KEY = "logsearch.private.key"; + private static final String LOGSEARCH_KEYSTORE_PUBLIC_KEY = "logsearch.public.key"; + private static final String LOGSEARCH_CERT_DEFAULT_ALGORITHM = "sha256WithRSA"; + + private static final String LOGSEARCH_CERT_DEFAULT_FOLDER = "/etc/ambari-logsearch-portal/conf/keys"; + private static final String LOGSEARCH_KEYSTORE_DEFAULT_PASSWORD = "bigdata"; + + private SSLUtil() { + throw new UnsupportedOperationException(); + } + + public static String getKeyStoreLocation() { + return System.getProperty(KEYSTORE_LOCATION_ARG); + } + + public static String getKeyStorePassword() { + return System.getProperty(KEYSTORE_PASSWORD_ARG); + } + + public static String getKeyStoreType() { + return System.getProperty(KEYSTORE_TYPE_ARG, DEFAULT_KEYSTORE_TYPE); + } + + public static String getTrustStoreLocation() { + return System.getProperty(TRUSTSTORE_LOCATION_ARG); + } + + public static String getTrustStorePassword() { + return System.getProperty(TRUSTSTORE_PASSWORD_ARG); + } + + public static String getTrustStoreType() { + return System.getProperty(TRUSTSTORE_TYPE_ARG, DEFAULT_TRUSTSTORE_TYPE); + } + + public static boolean isKeyStoreSpecified() { + return StringUtils.isNotEmpty(getKeyStoreLocation()); + } + + private static boolean isTrustStoreSpecified() { + return StringUtils.isNotEmpty(getTrustStoreLocation()); + } + + public static SslContextFactory getSslContextFactory() { + SslContextFactory sslContextFactory = new SslContextFactory(); + sslContextFactory.setKeyStorePath(getKeyStoreLocation()); + sslContextFactory.setKeyStorePassword(getKeyStorePassword()); + sslContextFactory.setKeyStoreType(getKeyStoreType()); + if (isTrustStoreSpecified()) { + sslContextFactory.setTrustStorePath(getTrustStoreLocation()); + sslContextFactory.setTrustStorePassword(getTrustStorePassword()); + sslContextFactory.setTrustStoreType(getTrustStoreType()); + } + + return sslContextFactory; + } + + public static SSLContext getSSLContext() { + SslContextFactory sslContextFactory = getSslContextFactory(); + + try { + sslContextFactory.start(); + return sslContextFactory.getSslContext(); + } catch (Exception e) { + LOG.error("Could not create SSL Context", e); + return null; + } finally { + try { + sslContextFactory.stop(); + } catch (Exception e) { + LOG.error("Could not stop sslContextFactory", e); + } + } + } + + private static String getPasswordFromFile(String fileName) { + try { + File pwdFile = new File(LOGSEARCH_CERT_DEFAULT_FOLDER, fileName); + if (!pwdFile.exists()) { + FileUtils.writeStringToFile(pwdFile, LOGSEARCH_KEYSTORE_DEFAULT_PASSWORD); + return LOGSEARCH_KEYSTORE_DEFAULT_PASSWORD; + } else { + return FileUtils.readFileToString(pwdFile); + } + } catch (Exception e) { + LOG.warn("Exception occurred during read/write password file for keystore/truststore.", e); + return null; + } + } + + private static String getPasswordFromCredentialStore(String propertyName) { + try { + String providerPath = PropertiesHelper.getProperty(CREDENTIAL_STORE_PROVIDER_PATH); + if (providerPath == null) { + return null; + } + + Configuration config = new Configuration(); + config.set(CREDENTIAL_STORE_PROVIDER_PATH, providerPath); + char[] passwordChars = config.getPassword(propertyName); + return (ArrayUtils.isNotEmpty(passwordChars)) ? new String(passwordChars) : null; + } catch (Exception e) { + LOG.warn(String.format("Could not load password %s from credential store, using default password", propertyName), e); + return null; + } + } + + private static String getPassword(String propertyName, String fileName) { + String credentialStorePassword = getPasswordFromCredentialStore(propertyName); + if (credentialStorePassword != null) { + return credentialStorePassword; + } + + String filePassword = getPasswordFromFile(fileName); + if (filePassword != null) { + return filePassword; + } + + return LOGSEARCH_KEYSTORE_DEFAULT_PASSWORD; + } + + /** + * Put private key into in-memory keystore and write it to a file (JKS file) + */ + private static void setKeyAndCertInKeystore(X509Certificate cert, KeyPair keyPair, KeyStore keyStore, String keyStoreLocation, char[] password) + throws Exception { + Certificate[] certChain = new Certificate[1]; + certChain[0] = cert; + try (FileOutputStream fos = new FileOutputStream(keyStoreLocation)) { + keyStore.setKeyEntry("logsearch.alias", keyPair.getPrivate(), password, certChain); + keyStore.store(fos, password); + } catch (Exception e) { + LOG.error("Could not write certificate to Keystore", e); + throw e; + } + } + + /** + * Create in-memory keypair with bouncy castle + */ + private static KeyPair createKeyPair(String encryptionType, int byteCount) + throws NoSuchProviderException, NoSuchAlgorithmException { + Security.addProvider(new BouncyCastleProvider()); + KeyPairGenerator keyPairGenerator = createKeyPairGenerator(encryptionType, byteCount); + return keyPairGenerator.genKeyPair(); + } + + /** + * Generate X509 certificate if it does not exist + */ + private static X509Certificate generateCertificate(String certificateLocation, KeyPair keyPair, String algorithm) throws Exception { + try { + File certFile = new File(certificateLocation); + if (certFile.exists()) { + LOG.info("Certificate file exists ({}), skip the generation.", certificateLocation); + return getCertFile(certificateLocation); + } else { + Security.addProvider(new BouncyCastleProvider()); + X509Certificate cert = createCert(keyPair, algorithm, InetAddress.getLocalHost().getCanonicalHostName()); + FileUtils.writeByteArrayToFile(certFile, cert.getEncoded()); + return cert; + } + } catch (Exception e) { + LOG.error("Could not create certificate.", e); + throw e; + } + } + + private static void ensureStorePassword(String locationArg, String pwdArg, String propertyName, String fileName) { + if (StringUtils.isNotEmpty(System.getProperty(locationArg)) && StringUtils.isEmpty(System.getProperty(pwdArg))) { + String password = getPassword(propertyName, fileName); + System.setProperty(pwdArg, password); + } + } + + public static void ensureStorePasswords() { + ensureStorePassword(KEYSTORE_LOCATION_ARG, KEYSTORE_PASSWORD_ARG, KEYSTORE_PASSWORD_PROPERTY_NAME, KEYSTORE_PASSWORD_FILE); + ensureStorePassword(TRUSTSTORE_LOCATION_ARG, TRUSTSTORE_PASSWORD_ARG, TRUSTSTORE_PASSWORD_PROPERTY_NAME, TRUSTSTORE_PASSWORD_FILE); + } + + private static X509Certificate getCertFile(String location) throws Exception { + try (FileInputStream fos = new FileInputStream(location)) { + CertificateFactory factory = CertificateFactory.getInstance("X.509"); + return (X509Certificate) factory.generateCertificate(fos); + } catch (Exception e) { + LOG.error("Cannot read cert file. ('" + location + "')", e); + throw e; + } + } + + private static X509Certificate createCert(KeyPair keyPair, String signatureAlgoritm, String domainName) + throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, OperatorCreationException, CertificateException, IOException { + + RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); + RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); + + AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(signatureAlgoritm); + AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId); + BcContentSignerBuilder sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId); + + ASN1InputStream publicKeyStream = new ASN1InputStream(rsaPublicKey.getEncoded()); + SubjectPublicKeyInfo pubKey = SubjectPublicKeyInfo.getInstance(publicKeyStream.readObject()); + publicKeyStream.close(); + + X509v3CertificateBuilder v3CertBuilder = new X509v3CertificateBuilder( + new X500Name("CN=" + domainName + ", OU=None, O=None L=None, C=None"), + BigInteger.valueOf(Math.abs(new SecureRandom().nextInt())), + new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30), + new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365*10)), + new X500Name("CN=" + domainName + ", OU=None, O=None L=None, C=None"), + pubKey); + + RSAKeyParameters keyParams = new RSAKeyParameters(true, rsaPrivateKey.getPrivateExponent(), rsaPrivateKey.getModulus()); + ContentSigner contentSigner = sigGen.build(keyParams); + + X509CertificateHolder certificateHolder = v3CertBuilder.build(contentSigner); + + JcaX509CertificateConverter certConverter = new JcaX509CertificateConverter().setProvider("BC"); + return certConverter.getCertificate(certificateHolder); + } + + private static KeyPairGenerator createKeyPairGenerator(String algorithmIdentifier, int bitCount) + throws NoSuchProviderException, NoSuchAlgorithmException { + KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithmIdentifier, BouncyCastleProvider.PROVIDER_NAME); + kpg.initialize(bitCount); + return kpg; + } + + /** + * Create keystore with keys and certificate (only if the keystore does not exist or if you have no permissions on the keystore file) + */ + public static void loadKeystore() { + try { + String certFolder = PropertiesHelper.getProperty(LOGSEARCH_CERT_FOLDER_LOCATION, LOGSEARCH_CERT_DEFAULT_FOLDER); + String certAlgorithm = PropertiesHelper.getProperty(LOGSEARCH_CERT_ALGORITHM, LOGSEARCH_CERT_DEFAULT_ALGORITHM); + String certLocation = String.format("%s/%s", LOGSEARCH_CERT_DEFAULT_FOLDER, LOGSEARCH_CERT_FILENAME); + String keyStoreLocation = StringUtils.isNotEmpty(getKeyStoreLocation()) ? getKeyStoreLocation() + : String.format("%s/%s", LOGSEARCH_CERT_DEFAULT_FOLDER, LOGSEARCH_KEYSTORE_FILENAME); + char[] password = StringUtils.isNotEmpty(getKeyStorePassword()) ? + getKeyStorePassword().toCharArray() : LOGSEARCH_KEYSTORE_DEFAULT_PASSWORD.toCharArray(); + boolean keyStoreFileExists = new File(keyStoreLocation).exists(); + if (!keyStoreFileExists) { + FileUtil.createDirectory(certFolder); + LOG.warn("Keystore file ('{}') does not exist, creating new one. " + + "If the file exists, make sure you have proper permissions on that.", keyStoreLocation); + if (isKeyStoreSpecified() && !"JKS".equalsIgnoreCase(getKeyStoreType())) { + throw new RuntimeException(String.format("Keystore does not exist. Only JKS keystore can be auto generated. (%s)", keyStoreLocation)); + } + LOG.info("SSL keystore is not specified. Generating it with certificate ... (using default format: JKS)"); + Security.addProvider(new BouncyCastleProvider()); + KeyPair keyPair = createKeyPair("RSA", 2048); + File privateKeyFile = new File(String.format("%s/%s", certFolder, LOGSEARCH_KEYSTORE_PRIVATE_KEY)); + if (!privateKeyFile.exists()) { + FileUtils.writeByteArrayToFile(privateKeyFile, keyPair.getPrivate().getEncoded()); + } + File file = new File(String.format("%s/%s", certFolder, LOGSEARCH_KEYSTORE_PUBLIC_KEY)); + if (!file.exists()) { + FileUtils.writeByteArrayToFile(file, keyPair.getPublic().getEncoded()); + } + X509Certificate cert = generateCertificate(certLocation, keyPair, certAlgorithm); + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + keyStore.load(null, password); + setKeyAndCertInKeystore(cert, keyPair, keyStore, keyStoreLocation, password); + FileUtil.setPermissionOnDirectory(certFolder, "600"); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/SolrUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/SolrUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/SolrUtil.java new file mode 100644 index 0000000..973e304 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/SolrUtil.java @@ -0,0 +1,258 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.google.common.base.Splitter; +import org.apache.ambari.logsearch.common.LogSearchConstants; +import org.apache.lucene.analysis.core.KeywordTokenizerFactory; +import org.apache.lucene.analysis.path.PathHierarchyTokenizerFactory; +import org.apache.lucene.analysis.standard.StandardTokenizerFactory; +import org.apache.lucene.analysis.util.TokenizerFactory; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.schema.TrieDoubleField; +import org.apache.solr.schema.TrieFloatField; +import org.apache.solr.schema.TrieIntField; +import org.apache.solr.schema.TrieLongField; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; + +public class SolrUtil { + private SolrUtil() { + throw new UnsupportedOperationException(); + } + + /** + * Copied from Solr ClientUtils.escapeQueryChars and removed escaping * + */ + public static String escapeQueryChars(String s) { + StringBuilder sb = new StringBuilder(); + if (s != null) { + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + int ic = (int) c; + if (ic == 10) { + sb.append('\\'); + sb.append((char) 13); + } + // Note: Remove || c == '*' + // These characters are part of the query syntax and must be escaped + if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' + || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' + || c == '\"' || c == '{' || c == '}' || c == '~' || c == '?' + || c == '|' || c == '&' || c == ';' || c == '/' + || Character.isWhitespace(c)) { + sb.append('\\'); + } + sb.append(c); + } + } + return sb.toString(); + } + + public static String escapeForStandardTokenizer(String search) { + if (search == null) { + return null; + } + String newSearch = escapeQueryChars(search.trim()); + if (StringUtils.containsWhitespace(newSearch)) { + newSearch = "\"" + newSearch + "\""; + } + + return newSearch; + } + + private static String makeSolrSearchStringWithoutAsterisk(String search) { + String newString = search.trim(); + String newSearch = newString.replaceAll("(?=[]\\[+&|!(){}^\"~=/$@%?:.\\\\])", "\\\\"); + newSearch = newSearch.replace("\n", "*"); + newSearch = newSearch.replace("\t", "*"); + newSearch = newSearch.replace("\r", "*"); + newSearch = newSearch.replace(" ", "\\ "); + newSearch = newSearch.replace("**", "*"); + newSearch = newSearch.replace("***", "*"); + return newSearch; + } + + public static String makeSearcableString(String search) { + if (StringUtils.isBlank(search)) { + return ""; + } + String newSearch = search.replaceAll("[\\t\\n\\r]", " "); + newSearch = newSearch.replaceAll("(?=[]\\[+&|!(){}^~=$/@%?:.\\\\-])", "\\\\"); + + return newSearch.replace(" ", "\\ "); + } + + public static void removeDoubleOrTripleEscapeFromFilters(SolrQuery solrQuery) { + String[] filterQueries = solrQuery.getFilterQueries(); + List<String> newArray = new ArrayList<>(); + if (filterQueries != null && filterQueries.length > 0) { + for (String filterQuery : filterQueries) { + newArray.add(filterQuery.replaceAll("\\\\\\\\\\\\|\\\\\\\\", "\\\\")); + } + } + solrQuery.setFilterQueries(newArray.toArray(new String[0])); + } + + + private static boolean isSolrFieldNumber(Map<String, Object> fieldTypeInfoMap) { + if (MapUtils.isEmpty(fieldTypeInfoMap)) { + return false; + } + String fieldTypeClassName = (String) fieldTypeInfoMap.get("class"); + return fieldTypeClassName.equalsIgnoreCase(TrieIntField.class.getSimpleName()) || + fieldTypeClassName.equalsIgnoreCase(TrieDoubleField.class.getSimpleName()) || + fieldTypeClassName.equalsIgnoreCase(TrieFloatField.class.getSimpleName()) || + fieldTypeClassName.equalsIgnoreCase(TrieLongField.class.getSimpleName()); + } + + public static String putWildCardByType(String str, String fieldType, String fieldTypeMetaData) { + Map<String, Object> fieldTypeInfoMap = getFieldTypeInfoMap(fieldTypeMetaData); + if (StringUtils.isNotBlank(fieldType)) { + if (isSolrFieldNumber(fieldTypeInfoMap)) { + String value = putEscapeCharacterForNumber(str, fieldTypeInfoMap); + if (StringUtils.isNotBlank(value)) { + return value; + } else { + return null; + } + } else if (checkTokenizer(StandardTokenizerFactory.class, fieldTypeInfoMap)) { + return escapeForStandardTokenizer(str); + } else if (checkTokenizer(KeywordTokenizerFactory.class, fieldTypeInfoMap) || "string".equalsIgnoreCase(fieldType)) { + return makeSolrSearchStringWithoutAsterisk(str); + } else if (checkTokenizer(PathHierarchyTokenizerFactory.class, fieldTypeInfoMap)) { + return str; + } else { + return escapeQueryChars(str); + } + } + return str; + } + + private static String putEscapeCharacterForNumber(String str, Map<String, Object> fieldTypeInfoMap) { + if (StringUtils.isNotEmpty(str)) { + str = str.replace("*", ""); + } + String escapeCharSting = parseInputValueAsPerFieldType(str, fieldTypeInfoMap); + if (escapeCharSting == null || escapeCharSting.isEmpty()) { + return null; + } + escapeCharSting = escapeCharSting.replace("-", "\\-"); + return escapeCharSting; + } + + private static String parseInputValueAsPerFieldType(String str, Map<String, Object> fieldTypeInfoMap) { + try { + String className = (String) fieldTypeInfoMap.get("class"); + if (className.equalsIgnoreCase(TrieDoubleField.class.getSimpleName())) { + return "" + Double.parseDouble(str); + } else if (className.equalsIgnoreCase(TrieFloatField.class.getSimpleName())) { + return "" + Float.parseFloat(str); + } else if (className.equalsIgnoreCase(TrieLongField.class.getSimpleName())) { + return "" + Long.parseLong(str); + } else { + return "" + Integer.parseInt(str); + } + } catch (Exception e) { + return null; + } + } + + public static SolrQuery addListFilterToSolrQuery(SolrQuery solrQuery, String fieldName, String fieldValue) { + if (org.apache.commons.lang.StringUtils.isNotEmpty(fieldValue)) { + List<String> clusters = Splitter.on(",").splitToList(fieldValue); + if (clusters.size() > 1) { + solrQuery.addFilterQuery(String.format("%s:(%s)", fieldName, org.apache.commons.lang.StringUtils.join(clusters, " OR "))); + } else { + solrQuery.addFilterQuery(String.format("%s:%s", fieldName, clusters.get(0))); + } + } + return solrQuery; + } + + private static Map<String, Object> getFieldTypeInfoMap(String fieldTypeMetaData) { + HashMap<String, Object> fieldTypeMap = JSONUtil.jsonToMapObject(fieldTypeMetaData); + if (fieldTypeMap == null) { + return new HashMap<>(); + } + String classname = (String) fieldTypeMap.get("class"); + if (StringUtils.isNotBlank(classname)) { + classname = classname.replace("solr.", ""); + fieldTypeMap.put("class", classname); + } + return fieldTypeMap; + } + + //============================================================================================================= + + public static void setFacetField(SolrQuery solrQuery, String facetField) { + solrQuery.setFacet(true); + setRowCount(solrQuery, 0); + solrQuery.set(LogSearchConstants.FACET_FIELD, facetField); + setFacetLimit(solrQuery, -1); + } + + public static void setFacetSort(SolrQuery solrQuery, String sortType) { + solrQuery.setFacet(true); + solrQuery.setFacetSort(sortType); + } + + public static void setFacetPivot(SolrQuery solrQuery, int mincount, String... hirarchy) { + solrQuery.setFacet(true); + setRowCount(solrQuery, 0); + solrQuery.set(LogSearchConstants.FACET_PIVOT, hirarchy); + solrQuery.set(LogSearchConstants.FACET_PIVOT_MINCOUNT, mincount); + setFacetLimit(solrQuery, -1); + } + + private static void setFacetLimit(SolrQuery solrQuery, int limit) { + solrQuery.set("facet.limit", limit); + } + + public static void setRowCount(SolrQuery solrQuery, int rows) { + if (rows > 0) { + solrQuery.setRows(rows); + } else { + solrQuery.setRows(0); + solrQuery.remove(LogSearchConstants.SORT); + } + } + + @SuppressWarnings("unchecked") + private static boolean checkTokenizer(Class<? extends TokenizerFactory> tokenizerFactoryClass, Map<String, Object> fieldTypeInfoMap) { + HashMap<String, Object> analyzer = (HashMap<String, Object>) fieldTypeInfoMap.get("analyzer"); + HashMap<String, Object> tokenizerMap = (HashMap<String, Object>)MapUtils.getObject(analyzer, "tokenizer"); + if (tokenizerMap != null) { + String tokenizerClass = (String) tokenizerMap.get("class"); + if (StringUtils.isNotEmpty(tokenizerClass)) { + tokenizerClass = tokenizerClass.replace("solr.", ""); + return tokenizerClass.equalsIgnoreCase(tokenizerFactoryClass.getSimpleName()); + } + } + + return false; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/WebUtil.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/WebUtil.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/WebUtil.java new file mode 100644 index 0000000..36865ad --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/util/WebUtil.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.util; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WebUtil { + private static final Logger LOG = LoggerFactory.getLogger(WebUtil.class); + + private static final String WEB_RESOURCE_FOLDER = "webapps/app"; + + private WebUtil() { + throw new UnsupportedOperationException(); + } + + public static URI findWebResourceBase() { + URL fileCompleteUrl = Thread.currentThread().getContextClassLoader().getResource(WEB_RESOURCE_FOLDER); + String errorMessage = "Web Resource Folder " + WEB_RESOURCE_FOLDER + " not found in classpath"; + if (fileCompleteUrl != null) { + try { + return fileCompleteUrl.toURI().normalize(); + } catch (URISyntaxException e) { + LOG.error(errorMessage, e); + System.exit(1); + } + } else { + LOG.error(errorMessage); + System.exit(1); + } + throw new IllegalStateException(errorMessage); + } + + public static void checkPort(int port) { + try (ServerSocket serverSocket = new ServerSocket(port)) { + } catch (IOException ex) { + LOG.error(ex.getLocalizedMessage() + " PORT :" + port); + System.exit(1); + } + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/web/authenticate/LogsearchAuthFailureHandler.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/web/authenticate/LogsearchAuthFailureHandler.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/web/authenticate/LogsearchAuthFailureHandler.java new file mode 100644 index 0000000..fdec8d3 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/web/authenticate/LogsearchAuthFailureHandler.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.web.authenticate; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler; + +public class LogsearchAuthFailureHandler extends ExceptionMappingAuthenticationFailureHandler { + private static final Logger logger = Logger.getLogger(LogsearchAuthFailureHandler.class); + + public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) + throws IOException, ServletException { + logger.debug(" AuthFailureHandler + onAuthenticationFailure"); + // TODO UI side handle status and redirect to login page with proper + response.setContentType("application/json"); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + response.getOutputStream().println("{ \"error\": \"" + "login failed !!" + "\" }"); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/b0f1e340/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/web/authenticate/LogsearchAuthSuccessHandler.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/web/authenticate/LogsearchAuthSuccessHandler.java b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/web/authenticate/LogsearchAuthSuccessHandler.java new file mode 100644 index 0000000..cf9d200 --- /dev/null +++ b/ambari-logsearch/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/web/authenticate/LogsearchAuthSuccessHandler.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.logsearch.web.authenticate; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; + +public class LogsearchAuthSuccessHandler implements AuthenticationSuccessHandler { + private static final Logger logger = Logger.getLogger(LogsearchAuthSuccessHandler.class); + + + @Override + public void onAuthenticationSuccess(HttpServletRequest request, + HttpServletResponse response, Authentication authentication) + throws ServletException, IOException { + response.setStatus(HttpServletResponse.SC_OK); + } +}