This is an automated email from the ASF dual-hosted git repository.
cschneider pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git
The following commit(s) were added to refs/heads/master by this push:
new 076065e SLING-9259 - Remove package browser as packages are now json
076065e is described below
commit 076065e4882e22a476bc4212397287c67e0bfe43
Author: Christian Schneider <[email protected]>
AuthorDate: Mon Jun 22 10:56:23 2020 +0200
SLING-9259 - Remove package browser as packages are now json
---
.../journal/impl/shared/PackageBrowser.java | 102 -------------
.../journal/impl/shared/PackageViewerPlugin.java | 149 -------------------
.../journal/impl/subscriber/PackageHandler.java | 31 +++-
.../journal/impl/shared/PackageBrowserTest.java | 97 -------------
.../impl/shared/PackageViewerPluginTest.java | 160 ---------------------
5 files changed, 29 insertions(+), 510 deletions(-)
diff --git
a/src/main/java/org/apache/sling/distribution/journal/impl/shared/PackageBrowser.java
b/src/main/java/org/apache/sling/distribution/journal/impl/shared/PackageBrowser.java
deleted file mode 100644
index b02d33a..0000000
---
a/src/main/java/org/apache/sling/distribution/journal/impl/shared/PackageBrowser.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * 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.sling.distribution.journal.impl.shared;
-
-import static java.util.Collections.singletonMap;
-import static org.apache.sling.api.resource.ResourceResolverFactory.SUBSERVICE;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.time.Duration;
-import java.util.List;
-
-import javax.annotation.Nonnull;
-import javax.jcr.Binary;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-import javax.jcr.ValueFactory;
-
-import org.apache.commons.io.IOUtils;
-import org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary;
-import org.apache.sling.api.resource.LoginException;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.distribution.common.DistributionException;
-import org.apache.sling.distribution.journal.FullMessage;
-import org.apache.sling.distribution.journal.JournalAvailable;
-import org.apache.sling.distribution.journal.MessagingProvider;
-import org.apache.sling.distribution.journal.messages.PackageMessage;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
-
-@Component(service = PackageBrowser.class)
-public class PackageBrowser {
-
- @Reference
- private JournalAvailable journalAvailable;
-
- @Reference
- private MessagingProvider messagingProvider;
-
- @Reference
- private Topics topics;
-
- @Reference
- ResourceResolverFactory resolverFactory;
-
- public List<FullMessage<PackageMessage>> getMessages(long startOffset,
long numMessages, Duration timeout) {
- LimitPoller poller = new LimitPoller(messagingProvider,
topics.getPackageTopic(), startOffset, numMessages);
- return poller.fetch(timeout);
- }
-
- public void writeTo(PackageMessage pkgMsg, OutputStream os) throws
IOException {
- try (ResourceResolver resolver = getServiceResolver("importer")) {
- InputStream is = pkgStream(resolver, pkgMsg);
- IOUtils.copy(is, os);
- } catch (LoginException | DistributionException e) {
- throw new IOException(e.getMessage(), e);
- }
- }
-
- @Nonnull
- public static InputStream pkgStream(ResourceResolver resolver,
PackageMessage pkgMsg) throws DistributionException {
- if (pkgMsg.getPkgBinary() != null) {
- return new ByteArrayInputStream(pkgMsg.getPkgBinary());
- } else {
- String pkgBinRef = pkgMsg.getPkgBinaryRef();
- try {
- Session session = resolver.adaptTo(Session.class);
- if (session == null) {
- throw new DistributionException("Unable to get Oak
session");
- }
- ValueFactory factory = session.getValueFactory();
- Binary binary = factory.createValue(new
SimpleReferenceBinary(pkgBinRef)).getBinary();
- return binary.getStream();
- } catch (RepositoryException e) {
- throw new DistributionException(e.getMessage(), e);
- }
- }
- }
-
- private ResourceResolver getServiceResolver(String subService) throws
LoginException {
- return
resolverFactory.getServiceResourceResolver(singletonMap(SUBSERVICE,
subService));
- }
-}
diff --git
a/src/main/java/org/apache/sling/distribution/journal/impl/shared/PackageViewerPlugin.java
b/src/main/java/org/apache/sling/distribution/journal/impl/shared/PackageViewerPlugin.java
deleted file mode 100644
index 308cd61..0000000
---
a/src/main/java/org/apache/sling/distribution/journal/impl/shared/PackageViewerPlugin.java
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * 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.sling.distribution.journal.impl.shared;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.time.Duration;
-import java.util.List;
-import java.util.Optional;
-
-import javax.servlet.Servlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.felix.webconsole.AbstractWebConsolePlugin;
-import org.apache.felix.webconsole.WebConsoleConstants;
-import org.apache.sling.distribution.journal.FullMessage;
-import org.apache.sling.distribution.journal.messages.PackageMessage;
-import org.apache.sling.distribution.journal.messages.PackageMessage.ReqType;
-import org.osgi.framework.Constants;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Component(service = Servlet.class,
- property = {
- Constants.SERVICE_DESCRIPTION + "=" + "Package viewer for content
distribution",
- WebConsoleConstants.PLUGIN_LABEL + "=" + PackageViewerPlugin.LABEL,
- WebConsoleConstants.PLUGIN_TITLE + "=" + PackageViewerPlugin.TITLE
-})
-public class PackageViewerPlugin extends AbstractWebConsolePlugin {
- private static final Duration BROWSE_TIMEOUT = Duration.ofMillis(1000);
- private static final Duration DOWNLOAD_TIMEOUT = Duration.ofMillis(20000);
- private static final int NOT_FOUND = 404;
- private static final int MAX_NUM_MESSAGES = 100;
- private static final long serialVersionUID = -3113699912185558439L;
- protected static final String LABEL = "distpackages";
- protected static final String TITLE = "Distribution Package Viewer";
-
- private final Logger log = LoggerFactory.getLogger(this.getClass());
-
- @Reference
- PackageBrowser packageBrowser; //NOSONAR
-
- @Override
- public String getLabel() {
- return LABEL;
- }
-
- @Override
- public String getTitle() {
- return TITLE;
- }
-
- @Override
- public String getCategory() {
- return "Sling";
- }
-
- @Override
- protected void renderContent(HttpServletRequest req, HttpServletResponse
res)
- throws IOException {
- Optional<Long> offset = getOffset(req);
- if (!offset.isPresent()) {
- String startOffsetSt = req.getParameter("startOffset");
- long startOffset = startOffsetSt != null ?
Long.parseLong(startOffsetSt) : 0;
- renderPackageList(startOffset, res.getWriter());
- } else {
- writePackage(offset.get(), res);
- }
- }
-
- private void renderPackageList(long startOffset, PrintWriter writer) {
- writer.println("<table class=\"tablesorter nicetable noauto\">");
-
writer.println("<tr><th>Id</th><th>Offset</th><th>Type</th><th>Paths</th></tr>");
- List<FullMessage<PackageMessage>> msgs =
packageBrowser.getMessages(startOffset, MAX_NUM_MESSAGES, BROWSE_TIMEOUT);
-
msgs.stream().filter(this::notTestMessage).map(this::writeMsg).forEach(writer::println);
- writer.println("</table>");
- if (msgs.size() == MAX_NUM_MESSAGES) {
- FullMessage<PackageMessage> lastMsg = msgs.get(msgs.size() - 1);
- long nextOffset = lastMsg.getInfo().getOffset() + 1;
- writer.println(String.format("<p><a href
=\"%s?startOffset=%d\">next page</a>",
- LABEL,
- nextOffset));
- }
- }
-
- private String writeMsg(FullMessage<PackageMessage> msg) {
- return String.format("<tr><td><a
href=\"%s/%d\">%s</a></td><td>%d</td><td>%s</td><td>%s</td></tr>",
- LABEL,
- msg.getInfo().getOffset(),
- msg.getMessage().getPkgId(),
- msg.getInfo().getOffset(),
- msg.getMessage().getReqType(),
- msg.getMessage().getPaths().toString());
- }
-
- private void writePackage(Long offset, HttpServletResponse res) throws
IOException {
- log.info("Retrieving package with offset {}", offset);
- List<FullMessage<PackageMessage>> msgs =
packageBrowser.getMessages(offset, 1, DOWNLOAD_TIMEOUT);
- if (!msgs.isEmpty()) {
- PackageMessage msg = msgs.iterator().next().getMessage();
- res.setHeader("Content-Type", "application/octet-stream");
- String filename = msg.getPkgId() + ".zip";
- res.setHeader("Content-Disposition" , "inline; filename=\"" +
filename + "\"");
- packageBrowser.writeTo(msg, res.getOutputStream());
- } else {
- res.setStatus(NOT_FOUND);
- }
- }
-
- @Override
- protected boolean isHtmlRequest(HttpServletRequest request) {
- return !getOffset(request).isPresent();
- }
-
- private Optional<Long> getOffset(HttpServletRequest req) {
- int startIndex = LABEL.length() + 2;
- if (startIndex <= req.getPathInfo().length()) {
- String offsetSt = req.getPathInfo().substring(startIndex);
- return Optional.of(Long.valueOf(offsetSt));
- } else {
- return Optional.empty();
- }
- }
-
- private boolean notTestMessage(FullMessage<PackageMessage> msg) {
- return msg.getMessage().getReqType() != ReqType.TEST;
- }
-
-
-}
\ No newline at end of file
diff --git
a/src/main/java/org/apache/sling/distribution/journal/impl/subscriber/PackageHandler.java
b/src/main/java/org/apache/sling/distribution/journal/impl/subscriber/PackageHandler.java
index bccf474..c63ef91 100644
---
a/src/main/java/org/apache/sling/distribution/journal/impl/subscriber/PackageHandler.java
+++
b/src/main/java/org/apache/sling/distribution/journal/impl/subscriber/PackageHandler.java
@@ -20,14 +20,21 @@ package
org.apache.sling.distribution.journal.impl.subscriber;
import static java.lang.String.format;
+import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import javax.annotation.Nonnull;
+import javax.jcr.Binary;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.ValueFactory;
+
import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.distribution.common.DistributionException;
-import org.apache.sling.distribution.journal.impl.shared.PackageBrowser;
import org.apache.sling.distribution.journal.messages.PackageMessage;
import org.apache.sling.distribution.packaging.DistributionPackageBuilder;
import org.slf4j.Logger;
@@ -66,7 +73,7 @@ public class PackageHandler {
LOG.info("Importing paths {}",pkgMsg.getPaths());
InputStream pkgStream = null;
try {
- pkgStream = PackageBrowser.pkgStream(resolver, pkgMsg);
+ pkgStream = stream(resolver, pkgMsg);
packageBuilder.installPackage(resolver, pkgStream);
extractor.handle(resolver, pkgMsg.getPaths());
} finally {
@@ -74,6 +81,26 @@ public class PackageHandler {
}
}
+
+ @Nonnull
+ public static InputStream stream(ResourceResolver resolver, PackageMessage
pkgMsg) throws DistributionException {
+ if (pkgMsg.getPkgBinary() != null) {
+ return new ByteArrayInputStream(pkgMsg.getPkgBinary());
+ } else {
+ String pkgBinRef = pkgMsg.getPkgBinaryRef();
+ try {
+ Session session = resolver.adaptTo(Session.class);
+ if (session == null) {
+ throw new DistributionException("Unable to get Oak
session");
+ }
+ ValueFactory factory = session.getValueFactory();
+ Binary binary = factory.createValue(new
SimpleReferenceBinary(pkgBinRef)).getBinary();
+ return binary.getStream();
+ } catch (RepositoryException e) {
+ throw new DistributionException(e.getMessage(), e);
+ }
+ }
+ }
private void installDeletePackage(ResourceResolver resolver,
PackageMessage pkgMsg)
throws PersistenceException {
diff --git
a/src/test/java/org/apache/sling/distribution/journal/impl/shared/PackageBrowserTest.java
b/src/test/java/org/apache/sling/distribution/journal/impl/shared/PackageBrowserTest.java
deleted file mode 100644
index c19f633..0000000
---
a/src/test/java/org/apache/sling/distribution/journal/impl/shared/PackageBrowserTest.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * 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.sling.distribution.journal.impl.shared;
-
-import static org.hamcrest.Matchers.equalTo;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.when;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-
-import javax.jcr.Binary;
-import javax.jcr.Session;
-import javax.jcr.Value;
-import javax.jcr.ValueFactory;
-
-import org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.distribution.journal.messages.PackageMessage;
-import org.apache.sling.distribution.journal.messages.PackageMessage.ReqType;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class PackageBrowserTest {
- private static final String DATA = "test";
-
- @Mock
- ResourceResolverFactory resolverFactory;
-
- @Mock
- ResourceResolver resolver;
-
- @Mock
- Session session;
-
- @Mock
- ValueFactory valueFactory;
-
- @Mock
- Value value;
-
- @InjectMocks
- PackageBrowser packageBrowser;
-
- @Mock
- Binary binary;
-
- @Test
- public void testWriteTo() throws Exception {
-
when(resolverFactory.getServiceResourceResolver(Mockito.any())).thenReturn(resolver);
- when(resolver.adaptTo(Session.class)).thenReturn(session);
- when(session.getValueFactory()).thenReturn(valueFactory);
-
when(valueFactory.createValue(Mockito.any(SimpleReferenceBinary.class))).thenReturn(value);
- when(value.getBinary()).thenReturn(binary);
- ByteArrayInputStream is = new
ByteArrayInputStream(DATA.getBytes(StandardCharsets.UTF_8));
- when(binary.getStream()).thenReturn(is);
- PackageMessage pkgMsg = createPackageMsg(0L);
- ByteArrayOutputStream bao = new ByteArrayOutputStream();
- packageBrowser.writeTo(pkgMsg, bao);
- String resultSt = bao.toString("utf-8");
- assertThat(resultSt, equalTo(DATA));
- }
-
- private PackageMessage createPackageMsg(long offset) throws Exception {
- return PackageMessage.builder()
- .pubSlingId("")
- .reqType(ReqType.ADD)
- .paths(Arrays.asList("/content"))
- .pkgId("pkgid")
- .pkgType("some_type")
- .pkgBinaryRef("myref").build();
- }
-}
diff --git
a/src/test/java/org/apache/sling/distribution/journal/impl/shared/PackageViewerPluginTest.java
b/src/test/java/org/apache/sling/distribution/journal/impl/shared/PackageViewerPluginTest.java
deleted file mode 100644
index 36009ba..0000000
---
a/src/test/java/org/apache/sling/distribution/journal/impl/shared/PackageViewerPluginTest.java
+++ /dev/null
@@ -1,160 +0,0 @@
-/*
- * 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.sling.distribution.journal.impl.shared;
-
-import static java.util.Collections.emptyList;
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.equalTo;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.nio.charset.Charset;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import javax.servlet.ServletException;
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.sling.distribution.journal.FullMessage;
-import org.apache.sling.distribution.journal.MessageInfo;
-import org.apache.sling.distribution.journal.MessagingProvider;
-import org.apache.sling.distribution.journal.messages.PackageMessage;
-import org.apache.sling.distribution.journal.messages.PackageMessage.ReqType;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.Spy;
-import org.mockito.runners.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class PackageViewerPluginTest {
-
- @Spy
- Topics topics = new Topics();
-
- @Mock
- private HttpServletRequest req;
-
- @Mock
- private HttpServletResponse res;
-
- @Mock
- MessagingProvider messagingProvider;
-
- @Mock
- ServletOutputStream servletOutputStream;
-
- @Mock
- PackageBrowser packageBrowser;
-
- @InjectMocks
- PackageViewerPlugin viewer;
-
- private StringWriter outWriter;
-
- @Before
- public void before() throws IOException {
- FullMessage<PackageMessage> msg1 = createPackageMsg(1L);
- List<FullMessage<PackageMessage>> messages =
Collections.singletonList(msg1);
- doReturn(messages).when(packageBrowser).getMessages(Mockito.eq(1L),
Mockito.anyLong(), Mockito.any());
- doReturn(messages).when(packageBrowser).getMessages(Mockito.eq(0L),
Mockito.anyLong(), Mockito.any());
- doReturn(emptyList()).when(packageBrowser).getMessages(Mockito.eq(2L),
Mockito.anyLong(), Mockito.any());
-
- outWriter = new StringWriter();
- when(res.getWriter()).thenReturn(new PrintWriter(outWriter));
- when(res.getOutputStream()).thenReturn(servletOutputStream);
- }
-
- @Test
- public void testSimple() {
- assertThat(viewer.getLabel(), equalTo("distpackages"));
- assertThat(viewer.getTitle(), equalTo("Distribution Package Viewer"));
- assertThat(viewer.getCategory(), equalTo("Sling"));
- }
-
- @Test
- public void testPackageList() throws ServletException, IOException {
- when(req.getPathInfo()).thenReturn("/distpackages");
-
- viewer.renderContent(req, res);
-
- String outString = outWriter.getBuffer().toString();
- System.out.println(outString);
- assertThat(outString,
- containsString("<tr><td><a
href=\"distpackages/1\">pkgid</a></td><td>1</td><td>ADD</td><td>[/content]</td></tr>"));
- }
-
- @Test
- public void testGetPackage() throws ServletException, IOException {
- when(req.getPathInfo()).thenReturn("/distpackages/1");
-
- viewer.renderContent(req, res);
-
- verify(packageBrowser).getMessages(Mockito.eq(1L), Mockito.eq(1L),
Mockito.any());
- }
-
- @Test
- public void testGetPackageNotFound() throws ServletException, IOException {
- when(req.getPathInfo()).thenReturn("/distpackages/2");
-
- viewer.renderContent(req, res);
-
- verify(res).setStatus(Mockito.eq(404));
- verify(packageBrowser).getMessages(Mockito.eq(2L), Mockito.eq(1L),
Mockito.any());
- }
-
- @Test
- public void testIsHtmlPackage() throws ServletException, IOException {
- when(req.getPathInfo()).thenReturn("/distpackages/1");
-
- assertThat(viewer.isHtmlRequest(req), equalTo(false));
- }
-
- @Test
- public void testIsHtmlMain() throws ServletException, IOException {
- when(req.getPathInfo()).thenReturn("/distpackages");
-
- assertThat(viewer.isHtmlRequest(req), equalTo(true));
- }
-
- private FullMessage<PackageMessage> createPackageMsg(long offset) {
- MessageInfo info = new TestMessageInfo("topic", 0 , offset, 0L);
- PackageMessage message = PackageMessage.builder()
- .pubSlingId("")
- .reqType(ReqType.ADD)
- .paths(Arrays.asList("/content"))
- .pkgId("pkgid")
- .pkgType("some_type")
- .pkgBinary("package
content".getBytes(Charset.defaultCharset()))
- .build();
- return new FullMessage<>(info, message);
- }
-
-}