Repository: hive Updated Branches: refs/heads/master 4fc9d5230 -> 073dc8808
HIVE-18835 : JDBC standalone jar download link in ambari (Miklos Gergely via Thejas Nair) Signed-off-by: Ashutosh Chauhan <hashut...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/073dc880 Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/073dc880 Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/073dc880 Branch: refs/heads/master Commit: 073dc88083650c414f10b9a9511008f1a68e8282 Parents: 4fc9d52 Author: Miklos Gergely <mgerg...@hortonworks.com> Authored: Wed Mar 7 15:20:56 2018 -0800 Committer: Ashutosh Chauhan <hashut...@apache.org> Committed: Wed Mar 7 15:20:56 2018 -0800 ---------------------------------------------------------------------- .../hive/http/JdbcJarDownloadServlet.java | 91 ++++++++++++++++++++ .../apache/hive/service/server/HiveServer2.java | 2 + .../hive/http/TestJdbcJarDownloadServlet.java | 46 ++++++++++ 3 files changed, 139 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/073dc880/service/src/java/org/apache/hive/http/JdbcJarDownloadServlet.java ---------------------------------------------------------------------- diff --git a/service/src/java/org/apache/hive/http/JdbcJarDownloadServlet.java b/service/src/java/org/apache/hive/http/JdbcJarDownloadServlet.java new file mode 100644 index 0000000..d3bfd1b --- /dev/null +++ b/service/src/java/org/apache/hive/http/JdbcJarDownloadServlet.java @@ -0,0 +1,91 @@ +/* + * 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.hive.http; + +import java.io.File; +import java.io.FileFilter; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class JdbcJarDownloadServlet extends HttpServlet { + + private static final Log LOG = LogFactory.getLog(JdbcJarDownloadServlet.class); + + private static final String JDBC_JAR_DIR = System.getenv("HIVE_HOME") + "/jdbc/"; + private static final String JDBC_JAR_PATTERN = "hive-jdbc-*-standalone.jar"; + private static final String JAR_CONTENT_DISPOSITION = "attachment; filename=hive-jdbc-standalone.jar"; + private static final String JAR_CONTENT_TYPE = "application/java-archive"; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + + LOG.info("Requesting jdbc standalone jar download"); + + response.setHeader("Content-disposition", JAR_CONTENT_DISPOSITION); + response.setContentType(JAR_CONTENT_TYPE); + + File dir = new File(JDBC_JAR_DIR); + FileFilter fileFilter = new WildcardFileFilter(JDBC_JAR_PATTERN); + File[] files = dir.listFiles(fileFilter); + if (files == null || files.length != 1) { + handleError(files); + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } + File file = files[0]; + LOG.info("Jdbc standalone jar found: " + file.getAbsolutePath()); + + try (FileInputStream in = new FileInputStream(file); + OutputStream out = response.getOutputStream()) { + byte[] buffer = new byte[4096]; + int length; + while ((length = in.read(buffer)) > 0) { + out.write(buffer, 0, length); + } + } catch (Exception e) { + LOG.error("Exception during downloading standalone jdbc jar", e); + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + + LOG.info("Jdbc standalone jar " + file.getAbsolutePath() + " was downloaded"); + } + + private void handleError(File[] files) { + if (ArrayUtils.isEmpty(files)) { + LOG.error("No jdbc standalone jar found in the directory " + JDBC_JAR_DIR); + } else { + StringBuilder fileNames = new StringBuilder(); + for (File file : files) { + fileNames.append("\t" + file.getAbsolutePath() + "\n"); + } + LOG.error("Multiple jdbc standalone jars exist in the directory " + JDBC_JAR_DIR + ":\n" + fileNames); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/hive/blob/073dc880/service/src/java/org/apache/hive/service/server/HiveServer2.java ---------------------------------------------------------------------- diff --git a/service/src/java/org/apache/hive/service/server/HiveServer2.java b/service/src/java/org/apache/hive/service/server/HiveServer2.java index 86c9c2b..b7ece2b 100644 --- a/service/src/java/org/apache/hive/service/server/HiveServer2.java +++ b/service/src/java/org/apache/hive/service/server/HiveServer2.java @@ -77,6 +77,7 @@ import org.apache.hive.common.util.HiveStringUtils; import org.apache.hive.common.util.HiveVersionInfo; import org.apache.hive.common.util.ShutdownHookManager; import org.apache.hive.http.HttpServer; +import org.apache.hive.http.JdbcJarDownloadServlet; import org.apache.hive.http.LlapServlet; import org.apache.hive.http.security.PamAuthenticator; import org.apache.hive.service.CompositeService; @@ -281,6 +282,7 @@ public class HiveServer2 extends CompositeService { } } builder.addServlet("llap", LlapServlet.class); + builder.addServlet("jdbcjar", JdbcJarDownloadServlet.class); builder.setContextRootRewriteTarget("/hiveserver2.jsp"); webServer = builder.build(); http://git-wip-us.apache.org/repos/asf/hive/blob/073dc880/service/src/test/org/apache/hive/http/TestJdbcJarDownloadServlet.java ---------------------------------------------------------------------- diff --git a/service/src/test/org/apache/hive/http/TestJdbcJarDownloadServlet.java b/service/src/test/org/apache/hive/http/TestJdbcJarDownloadServlet.java new file mode 100644 index 0000000..f7ad1fd --- /dev/null +++ b/service/src/test/org/apache/hive/http/TestJdbcJarDownloadServlet.java @@ -0,0 +1,46 @@ +/* + * 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.hive.http; + +import java.io.IOException; + +import javax.servlet.http.HttpServletResponse; + +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import junit.framework.TestCase; + +public class TestJdbcJarDownloadServlet extends TestCase { + + @Test + public void testNoFileFound() throws IOException { + HttpServletResponse mockResponse = mock(HttpServletResponse.class); + JdbcJarDownloadServlet servlet = new JdbcJarDownloadServlet(); + servlet.doGet(null, mockResponse); + + verify(mockResponse, times(1)).setHeader( + "Content-disposition","attachment; filename=hive-jdbc-standalone.jar"); + verify(mockResponse, times(1)).setContentType("application/java-archive"); + verify(mockResponse, times(1)).sendError(HttpServletResponse.SC_NOT_FOUND); + } +} \ No newline at end of file