Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring: extracted SDK classes #19

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/artipie/git/TmpResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public TmpResource(final Supplier<? extends Path> factory) {
* @param <T> Result type
* @return Operation result
*/
<T> CompletableFuture<? extends T> with(
public <T> CompletableFuture<? extends T> with(
final Function<? super Storage, ? extends CompletionStage<T>> func
) {
return CompletableFuture.supplyAsync(this.factory).thenCompose(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.http;

import java.io.IOException;
import java.io.Writer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.http;

import com.artipie.asto.Storage;
import com.artipie.asto.fs.FileStorage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.http;

import com.artipie.asto.ArtipieIOException;
import com.artipie.asto.Content;
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/artipie/git/http/LsRefsSlice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git.http;

import com.artipie.asto.Content;
import com.artipie.asto.Storage;
import com.artipie.git.sdk.Git;
import com.artipie.http.Response;
import com.artipie.http.Slice;
import com.artipie.http.async.AsyncResponse;
import com.artipie.http.rs.RsWithBody;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.Map.Entry;
import org.reactivestreams.Publisher;

/**
* Slice to handle {@code ls-refs} command.
*
* @since 1.0
*/
final class LsRefsSlice implements Slice {

/**
* Repository storage.
*/
private final Storage storage;

/**
* New slice.
*
* @param storage Repo storage
*/
LsRefsSlice(final Storage storage) {
this.storage = storage;
}

@Override
public Response response(final String line, final Iterable<Entry<String, String>> headers,
final Publisher<ByteBuffer> body) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
return new AsyncResponse(
new Git(this.storage).lsRefs(baos)
.thenApply(none -> new RsWithBody(new Content.From(baos.toByteArray())))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.http;

import com.artipie.http.Slice;
import com.artipie.http.rs.RsStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.http;

import com.artipie.asto.Storage;
import com.artipie.asto.ext.PublisherAs;
import com.artipie.git.sdk.GitRequest;
import com.artipie.http.Response;
import com.artipie.http.Slice;
import com.artipie.http.async.AsyncResponse;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/artipie/git/http/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/

/**
* Git smart-http implementation.
* @since 1.0
*/
package com.artipie.git.http;
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,55 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.sdk;

import com.artipie.asto.ArtipieIOException;
import com.artipie.asto.Content;
import com.artipie.asto.Copy;
import com.artipie.asto.Storage;
import com.artipie.http.Response;
import com.artipie.http.Slice;
import com.artipie.http.async.AsyncResponse;
import com.artipie.http.rs.RsWithBody;
import java.io.ByteArrayOutputStream;
import com.artipie.asto.misc.UncheckedSupplier;
import com.artipie.git.TmpResource;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.PacketLineOut;
import org.eclipse.jgit.transport.RefAdvertiser;
import org.reactivestreams.Publisher;

/**
* Slice to handle {@code ls-refs} command.
*
* Git SDK.
* @since 1.0
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @checkstyle MethodBodyCommentsCheck (500 lines)
*/
final class LsRefsSlice implements Slice {
public final class Git {

/**
* Repository storage.
*/
private final Storage storage;

/**
* New slice.
*
* @param storage Repo storage
* New git SDK.
* @param storage Repository storage
*/
LsRefsSlice(final Storage storage) {
public Git(final Storage storage) {
this.storage = storage;
}

@Override
/**
* Perform ls-refs command on a git reposirory.
* @param out Output stream for response
* @return Status future
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public Response response(final String line, final Iterable<Entry<String, String>> headers,
final Publisher<ByteBuffer> body) {
final Path tmp;
try {
tmp = Files.createTempDirectory(LsRefsSlice.class.getName());
} catch (final IOException iex) {
throw new ArtipieIOException(iex);
}
return new AsyncResponse(
new TmpResource(tmp).<Response>with(
tsto -> new Copy(this.storage).copy(tsto).thenApply(
public CompletableFuture<? extends Void> lsRefs(final OutputStream out) {
return CompletableFuture.supplyAsync(
new UncheckedSupplier<>(() -> Files.createTempDirectory(Git.class.getName()))
).thenCompose(
tmp -> new TmpResource(tmp).<Void>with(
tsto -> new Copy(this.storage).copy(tsto).<Void>thenApply(
none -> {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
final Repository repo = new FileRepository(
tmp.toAbsolutePath().toFile()
Expand All @@ -72,20 +61,20 @@ public Response response(final String line, final Iterable<Entry<String, String>
if (repo.resolve("HEAD") == null) {
repo.create(true);
}
final PacketLineOut out = new PacketLineOut(baos);
final PacketLineOut plout = new PacketLineOut(out);
final RefAdvertiser adv =
new RefAdvertiser.PacketLineOutRefAdvertiser(out);
new RefAdvertiser.PacketLineOutRefAdvertiser(plout);
adv.init(repo);
adv.setDerefTags(true);
adv.send(repo.getRefDatabase().getRefsByPrefix("HEAD"));
adv.send(repo.getRefDatabase().getRefsByPrefix("refs/"));
out.end();
plout.end();
} catch (final IOException iex) {
throw new ArtipieIOException(iex);
}
return new Content.From(baos.toByteArray());
return (Void) null;
}
).thenApply(RsWithBody::new)
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.sdk;

import com.artipie.ArtipieException;
import java.util.ArrayList;
Expand All @@ -16,7 +16,7 @@
* @checkstyle MagicNumberCheck (300 lines)
* @checkstyle MethodBodyCommentsCheck (300 lines)
*/
final class GitRequest {
public final class GitRequest {
/**
* Request lines.
*/
Expand All @@ -37,7 +37,7 @@ final class GitRequest {
* Parse command.
* @return Command if found
*/
Optional<String> command() {
public Optional<String> command() {
return this.lines.stream().filter(line -> line.startsWith("command="))
.map(line -> line.substring(8)).findFirst().map(String::trim);
}
Expand All @@ -46,16 +46,18 @@ Optional<String> command() {
* Parsed parts.
* @return Immutable list
*/
List<String> parts() {
public List<String> parts() {
return Collections.unmodifiableList(this.lines);
}

/**
* Parse raw request data.
* @param raw Request data
* @return Request data accessor
* @throws ArtipieException In case of request is invalid
*/
static GitRequest parse(final String raw) {
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static GitRequest parse(final String raw) {
String rest = raw;
final List<String> lines = new ArrayList<>(10);
while (!rest.isEmpty()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/artipie/git/sdk/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/

/**
* Git SDK objects.
* @since 1.0
*/
package com.artipie.git.sdk;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.http;

import com.artipie.asto.misc.UncheckedConsumer;
import java.io.ByteArrayOutputStream;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/artipie/git/http/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/

/**
* Tests for HTTP layer.
* @since 1.0
*/
package com.artipie.git.http;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.it;

import com.artipie.asto.misc.UncheckedConsumer;
import com.jcabi.log.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.it;

import com.artipie.asto.fs.FileStorage;
import com.artipie.git.http.GitSlice;
import com.artipie.http.slice.LoggingSlice;
import com.artipie.vertx.VertxSliceServer;
import com.jcabi.log.Logger;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/artipie/git/it/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/

/**
* Integration tests.
* @since 1.0
*/
package com.artipie.git.it;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/
package com.artipie.git;
package com.artipie.git.sdk;

import java.util.Arrays;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -13,7 +14,7 @@
*
* @since 1.0
*/
class GitRequestTest {
final class GitRequestTest {

@Test
void parseParts() {
Expand Down Expand Up @@ -42,7 +43,7 @@ void skipSmallLengths() {
@Test
void parseCommand() {
MatcherAssert.assertThat(
GitRequest.parse("0010command=foo\n0000").command().get(),
new GitRequest(Arrays.asList("command=foo")).command().get(),
Matchers.equalTo("foo")
);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/artipie/git/sdk/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* The MIT License (MIT) Copyright (c) 2020-2021 artipie.com
* https://github.com/artipie/git-adapter/LICENSE.txt
*/

/**
* Tests for git SDK.
* @since 1.0
*/
package com.artipie.git.sdk;