Skip to content

Maven Downloader #12

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

Merged
merged 7 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package de.upb.cs.swt.delphi.crawler.preprocessing

import akka.actor.{Actor, ActorLogging}
import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import de.upb.cs.swt.delphi.crawler.discovery.maven.MavenIdentifier
import de.upb.cs.swt.delphi.crawler.preprocessing.DispatchActor.{DownloadJar, DownloadPom}

class DispatchActor(ref: ActorRef) extends Actor with ActorLogging {
override def receive: Receive = {
case DownloadJar(id: MavenIdentifier) => {
log.info("Downloading jar file " + id.artifactId)
val downloader=new MavenDownloader(id)
ref forward downloader.downloadJar()
}
case DownloadPom(id: MavenIdentifier) => {
log.info("Downloading pom file " + id.artifactId)
val downloader=new MavenDownloader(id)
ref forward downloader.downloadPom()
}
}

class DispatchActor extends Actor with ActorLogging {
override def receive: Receive = ???
}

object DispatchActor {
def props(actorRef:ActorRef) = Props(new DispatchActor(actorRef))

case class DownloadJar(id: MavenIdentifier)

case class DownloadPom(id: MavenIdentifier)

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package de.upb.cs.swt.delphi.crawler.preprocessing

import java.io.InputStream

import scala.concurrent.ExecutionContext.Implicits.global
import java.net.URI

import de.upb.cs.swt.delphi.crawler.BuildInfo
import de.upb.cs.swt.delphi.crawler.discovery.maven.{HttpResourceHandler, MavenIdentifier}

trait MavenDownloader {

def populate(identifier: MavenIdentifier): MavenArtifact = {
val http = new HttpResourceHandler(constructArtifactBaseUri(identifier))
val pomResource = http.locate(pomFilename(identifier))
val jarResource = http.locate(jarFilename(identifier))

MavenArtifact(identifier)
}

def constructArtifactBaseUri(identifier: MavenIdentifier): URI =
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder

import scala.concurrent.Future
import scala.io.Source

class MavenDownloader(identifier: MavenIdentifier) {
val http = new HttpResourceHandler(constructArtifactBaseUri())
val pomResource = http.locate(pomFilename(identifier))
val jarResource = http.locate(jarFilename(identifier))

/**
* Construct url from maven identifier
* @return Base URI
*/
def constructArtifactBaseUri(): URI =
new URI(identifier.repository)
.resolve(identifier.groupId.replace('.', '/') + "/")
.resolve(identifier.artifactId + "/")
Expand All @@ -26,4 +34,12 @@ trait MavenDownloader {

def jarFilename(identifier: MavenIdentifier): String =
identifier.artifactId + "-" + identifier.version + ".jar"

def downloadJar(): JarFile = {
JarFile(pomResource.read())
}

def downloadPom(): PomFile= {
PomFile(pomResource.read())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.upb.cs.swt.delphi.crawler

import java.io.InputStream


/**
* @author Hariharan.
*/
package object preprocessing {

/**
* Used for identifcation (Pattern matching) of jar file
* @param is jar file stream
*/
case class JarFile(is: InputStream)

/**
* Used for identification (Pattern matching) of pom file
* @param is pom file stream
*/
case class PomFile(is: InputStream)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.upb.cs.swt.delphi.crawler.preprocessing

import java.io.{ByteArrayOutputStream, InputStream}
import java.nio.file.{Files, Paths}

/**
* @author Hariharan.
*/
object Common {
def inputStreamToBytes(stream: InputStream): Array[Byte] = {
val buffer = new ByteArrayOutputStream()
val bytes = new Array[Byte] (4096)
var len: Int = 0
while ( {
len = stream.read(bytes);
len != -1
}) {
buffer.write(bytes, 0, len)
}
buffer.flush()
buffer.toByteArray
}
def checkJar(is:InputStream):Unit={
val jarBytes = inputStreamToBytes(is)
val tmpDir = System.getProperty("java.io.tmpdir")
val jarPath = Paths.get(tmpDir).resolve("junit.jar")
Files.write(jarPath, jarBytes)
assert(jarPath.toFile.exists())
assert(jarPath.toFile.length() > 0)
}
def checkPom(is:InputStream):Unit={
val pomBytes = inputStreamToBytes(is)
val tmpDir = System.getProperty("java.io.tmpdir")
val pomPath = Paths.get(tmpDir).resolve("pom.xml")
Files.write(pomPath, pomBytes)
assert(pomPath.toFile.exists())
assert(pomPath.toFile.length() > 0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.upb.cs.swt.delphi.crawler.preprocessing

import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestKit}
import de.upb.cs.swt.delphi.crawler.discovery.maven.MavenIdentifier
import de.upb.cs.swt.delphi.crawler.preprocessing.DispatchActor.{DownloadJar, DownloadPom}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import scala.concurrent.duration._
import de.upb.cs.swt.delphi.crawler.preprocessing.Common._
/**
* @author Hariharan.
*/
class DispatchActorSpec extends TestKit(ActorSystem("DispatchActorSpec")) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll {
override def afterAll {
TestKit.shutdownActorSystem(system)
}

"A Dispatch actor" must {
"download jar file" in {
val mavenIdentifier = new MavenIdentifier("http://central.maven.org/maven2/", "junit", "junit", "4.12")
val dispatchActor = system.actorOf(DispatchActor.props(testActor))
dispatchActor ! DownloadJar(mavenIdentifier)
val msg=receiveOne(2.seconds)
assert(msg.isInstanceOf[JarFile])
val jarFile=msg.asInstanceOf[JarFile]
checkJar(jarFile.is)
}
}
"A Dispatch actor" must {
"download pom file" in {
val mavenIdentifier = new MavenIdentifier("http://central.maven.org/maven2/", "junit", "junit", "4.12")
val dispatchActor = system.actorOf(DispatchActor.props(testActor))
dispatchActor ! DownloadPom(mavenIdentifier)
val msg=receiveOne(2.seconds)
assert(msg.isInstanceOf[PomFile])
val pomFile=msg.asInstanceOf[PomFile]
checkPom(pomFile.is)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import de.upb.cs.swt.delphi.crawler.discovery.maven.MavenIdentifier
import de.upb.cs.swt.delphi.crawler.preprocessing.MavenDownloader
import org.scalatest.{FlatSpec, Matchers}
import de.upb.cs.swt.delphi.crawler.preprocessing.Common._
class MavenDownloaderSpec extends FlatSpec with Matchers {
"MavenDownloader" should "save jar file" in {
val mavenIdentifier = new MavenIdentifier("http://central.maven.org/maven2/", "junit", "junit", "4.12")
val downloader = new MavenDownloader(mavenIdentifier)
val jarStream = downloader.downloadJar()
checkJar(jarStream.is)
}
"MavenDownloader" should "save pom file" in {
val mavenIdentifier = new MavenIdentifier("http://central.maven.org/maven2/", "junit", "junit", "4.12")
val downloader = new MavenDownloader(mavenIdentifier)
val pomStream = downloader.downloadPom()
checkPom(pomStream.is)
}

}