various refactoring
added haslist added separate config file for client and server
This commit is contained in:
@@ -42,14 +42,14 @@ task deployWildfly(dependsOn: 'war') << {
|
||||
// client.jar
|
||||
task clientJar(type: Jar) {
|
||||
from(sourceSets.main.output) {
|
||||
archiveName project.name + "-client.jar"
|
||||
include "model/**"
|
||||
include "pacbase/**"
|
||||
archiveName project.name + "-common.jar"
|
||||
include 'org/jpacrepo/model/**', 'org/jpacrepo/pacbase/**', 'org/jpacrepo/context/ApplicationContext.class'
|
||||
}
|
||||
}
|
||||
|
||||
// server.jar
|
||||
task serverJar(type: Jar) {
|
||||
from(sourceSets.main.output) {
|
||||
|
||||
}
|
||||
}
|
@@ -51,5 +51,12 @@
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: org.apache.httpcomponents:httpcore:4.2.5" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: commons-logging:commons-logging:1.1.1" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: commons-codec:commons-codec:1.6" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: org.jboss.resteasy:resteasy-jaxb-provider:3.0.11.Final" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: com.sun.xml.bind:jaxb-impl:2.2.7" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: com.sun.xml.bind:jaxb-core:2.2.7" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: com.sun.xml.fastinfoset:FastInfoset:1.2.12" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: javax.xml.bind:jaxb-api:2.2.7" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: com.sun.istack:istack-commons-runtime:2.16" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Gradle: javax.xml.bind:jsr173_api:1.0" level="project" />
|
||||
</component>
|
||||
</module>
|
@@ -1,17 +0,0 @@
|
||||
package client;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
|
||||
/**
|
||||
* Created by walter on 29/03/15.
|
||||
*/
|
||||
public class MainClient
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target("http://www.myserver.com/book");
|
||||
}
|
||||
}
|
@@ -1,12 +1,15 @@
|
||||
package service;
|
||||
package org.jpacrepo.context;
|
||||
|
||||
import model.PkgData;
|
||||
import org.jpacrepo.model.PkgData;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Qualifier;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
@@ -16,18 +19,23 @@ import java.util.Properties;
|
||||
@ApplicationScoped
|
||||
public class ApplicationContext
|
||||
{
|
||||
public String getRepoFolder()
|
||||
{
|
||||
return repoFolder;
|
||||
}
|
||||
|
||||
private Properties systemProperties;
|
||||
|
||||
private String repoFolder;
|
||||
|
||||
public ApplicationContext()
|
||||
public ApplicationContext(String propertyFile)
|
||||
{
|
||||
systemProperties = new Properties();
|
||||
InputStream input = null;
|
||||
|
||||
try
|
||||
{
|
||||
input = new FileInputStream("/etc/jpacrepo/config.properties");
|
||||
input = new FileInputStream(propertyFile);
|
||||
// load a properties file
|
||||
systemProperties.load(input);
|
||||
// get the property value and print it out
|
18
src/main/java/org/jpacrepo/context/ContextProducer.java
Normal file
18
src/main/java/org/jpacrepo/context/ContextProducer.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.jpacrepo.context;
|
||||
|
||||
import javax.enterprise.inject.Produces;
|
||||
|
||||
/**
|
||||
* Created by walter on 04/04/15.
|
||||
*/
|
||||
|
||||
|
||||
public class ContextProducer
|
||||
{
|
||||
@Produces
|
||||
@DefaultConfiguration
|
||||
public ApplicationContext produce()
|
||||
{
|
||||
return new ApplicationContext("/etc/jpacrepo/server.properties");
|
||||
}
|
||||
}
|
18
src/main/java/org/jpacrepo/context/DefaultConfiguration.java
Normal file
18
src/main/java/org/jpacrepo/context/DefaultConfiguration.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.jpacrepo.context;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Created by walter on 04/04/15.
|
||||
*/
|
||||
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
|
||||
public @interface DefaultConfiguration
|
||||
{
|
||||
}
|
47
src/main/java/org/jpacrepo/model/HashList.java
Normal file
47
src/main/java/org/jpacrepo/model/HashList.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package org.jpacrepo.model;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlSeeAlso;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by walter on 29/03/15.
|
||||
*/
|
||||
|
||||
@XmlRootElement
|
||||
public class HashList extends ArrayList<String>
|
||||
{
|
||||
|
||||
// ======================================
|
||||
// = Constructors =
|
||||
// ======================================
|
||||
|
||||
public HashList()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public HashList(Collection<? extends String> c)
|
||||
{
|
||||
super(c);
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// = Getters & Setters =
|
||||
// ======================================
|
||||
|
||||
@XmlElement(name = "hash")
|
||||
public List<String> getPackages()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setPackages(List<String> pkgs)
|
||||
{
|
||||
this.clear();
|
||||
this.addAll(pkgs);
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package model;
|
||||
package org.jpacrepo.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
@@ -13,11 +13,12 @@ import java.util.List;
|
||||
@Entity
|
||||
@XmlRootElement
|
||||
@NamedQuery(name="searchById", query = "SELECT p FROM PkgData p WHERE p.id = :id")
|
||||
@Table(indexes = { @Index(columnList = "md5sum", unique = true), @Index(columnList = "fileName", unique = true) })
|
||||
public class PkgData
|
||||
{
|
||||
@Id
|
||||
@GeneratedValue
|
||||
Integer id;
|
||||
public Integer id;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.PERSIST)
|
||||
public PkgName name;
|
@@ -1,4 +1,4 @@
|
||||
package model;
|
||||
package org.jpacrepo.model;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
@@ -1,4 +1,4 @@
|
||||
package model;
|
||||
package org.jpacrepo.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
61
src/main/java/org/jpacrepo/pacbase/Hasher.java
Normal file
61
src/main/java/org/jpacrepo/pacbase/Hasher.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package org.jpacrepo.pacbase;
|
||||
|
||||
import org.apache.commons.compress.archivers.ArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
|
||||
import org.jpacrepo.model.PkgData;
|
||||
import org.jpacrepo.model.PkgName;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by walter on 05/04/15.
|
||||
*/
|
||||
public class Hasher
|
||||
{
|
||||
static private final MessageDigest md5;
|
||||
|
||||
static
|
||||
{
|
||||
try
|
||||
{
|
||||
md5 = MessageDigest.getInstance("MD5");
|
||||
}
|
||||
catch (NoSuchAlgorithmException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String computeMD5(InputStream is) throws IOException
|
||||
{
|
||||
md5.reset();
|
||||
while(is.available()>0)
|
||||
{
|
||||
byte[] buffer2 = new byte[1000000];
|
||||
is.read(buffer2, 0, 1000000);
|
||||
md5.update(buffer2);
|
||||
}
|
||||
is.close();
|
||||
return bytesToHex(md5.digest());
|
||||
}
|
||||
|
||||
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
public static String bytesToHex(byte[] bytes) {
|
||||
char[] hexChars = new char[bytes.length * 2];
|
||||
for ( int j = 0; j < bytes.length; j++ ) {
|
||||
int v = bytes[j] & 0xFF;
|
||||
hexChars[j * 2] = hexArray[v >>> 4];
|
||||
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
||||
}
|
||||
return new String(hexChars);
|
||||
}
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
package pacbase;
|
||||
package org.jpacrepo.pacbase;
|
||||
|
||||
import model.PkgData;
|
||||
import model.PkgName;
|
||||
import org.jpacrepo.model.PkgData;
|
||||
import org.jpacrepo.model.PkgName;
|
||||
import org.apache.commons.compress.archivers.ArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
|
||||
@@ -20,19 +20,6 @@ import java.util.*;
|
||||
*/
|
||||
public class Parser
|
||||
{
|
||||
static private final MessageDigest md5;
|
||||
|
||||
static
|
||||
{
|
||||
try
|
||||
{
|
||||
md5 = MessageDigest.getInstance("MD5");
|
||||
}
|
||||
catch (NoSuchAlgorithmException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static PkgData parseFile(File file) throws Exception
|
||||
{
|
||||
@@ -138,7 +125,7 @@ public class Parser
|
||||
|
||||
}
|
||||
}
|
||||
data.md5sum = computeMD5(new FileInputStream(file));
|
||||
data.md5sum = Hasher.computeMD5(new FileInputStream(file));
|
||||
data.fileName = file.getName();
|
||||
return data;
|
||||
}
|
||||
@@ -149,28 +136,4 @@ public class Parser
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String computeMD5(InputStream is) throws IOException
|
||||
{
|
||||
md5.reset();
|
||||
while(is.available()>0)
|
||||
{
|
||||
byte[] buffer2 = new byte[1000000];
|
||||
is.read(buffer2, 0, 1000000);
|
||||
md5.update(buffer2);
|
||||
}
|
||||
is.close();
|
||||
return bytesToHex(md5.digest());
|
||||
}
|
||||
|
||||
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
public static String bytesToHex(byte[] bytes) {
|
||||
char[] hexChars = new char[bytes.length * 2];
|
||||
for ( int j = 0; j < bytes.length; j++ ) {
|
||||
int v = bytes[j] & 0xFF;
|
||||
hexChars[j * 2] = hexArray[v >>> 4];
|
||||
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
||||
}
|
||||
return new String(hexChars);
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package persistence;
|
||||
package org.jpacrepo.persistence;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
@@ -1,4 +1,4 @@
|
||||
package service;
|
||||
package org.jpacrepo.service;
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
@@ -1,4 +1,4 @@
|
||||
package service;
|
||||
package org.jpacrepo.service;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
@@ -1,11 +1,13 @@
|
||||
package service;
|
||||
package org.jpacrepo.service;
|
||||
|
||||
import model.PkgData;
|
||||
import model.PkgName;
|
||||
import org.jpacrepo.context.ApplicationContext;
|
||||
import org.jpacrepo.context.DefaultConfiguration;
|
||||
import org.jpacrepo.model.PkgData;
|
||||
import org.jpacrepo.model.PkgName;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.filefilter.DirectoryFileFilter;
|
||||
import org.apache.commons.io.filefilter.RegexFileFilter;
|
||||
import pacbase.Parser;
|
||||
import org.jpacrepo.pacbase.Parser;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
@@ -35,16 +37,14 @@ public class PacmanServiceEJB implements PacmanService
|
||||
private EntityManager em;
|
||||
|
||||
@Inject
|
||||
@DefaultConfiguration
|
||||
private ApplicationContext ctx;
|
||||
|
||||
private Logger log = Logger.getLogger(PacmanServiceEJB.class.getName());
|
||||
private Logger logger = Logger.getLogger(PacmanServiceEJB.class.getName());
|
||||
|
||||
|
||||
private String nameQuery = "SELECT pname FROM PkgName pname WHERE id = :name";
|
||||
private String fileQuery = "SELECT pdata FROM PkgData pdata WHERE name.id = :name AND version = :version AND arch = :arch";
|
||||
private String fileNameQuery = "SELECT pdata FROM PkgData pdata WHERE fileName = :fileName";
|
||||
private String hashQuery = "SELECT pdata FROM PkgData pdata WHERE md5sum = :md5sum";
|
||||
private String idQuery = "SELECT pdata FROM PkgData pdata WHERE id = :id";
|
||||
|
||||
@PostConstruct
|
||||
public void syncDB()
|
||||
@@ -62,7 +62,7 @@ public class PacmanServiceEJB implements PacmanService
|
||||
File file = ctx.getFile(p);
|
||||
if (!file.exists())
|
||||
{
|
||||
log.log(Level.INFO, String.format("Removing package %s", file.getName()));
|
||||
logger.log(Level.INFO, String.format("Removing package %s", file.getName()));
|
||||
em.remove(p);
|
||||
}
|
||||
}
|
||||
@@ -74,10 +74,12 @@ public class PacmanServiceEJB implements PacmanService
|
||||
|
||||
//Aggiunge sul DB i pacchetti presenti nel filesystem
|
||||
Collection<File> ls = FileUtils.listFiles(new File(ctx.getSystemProperties().getProperty("RepoFolder")), new RegexFileFilter(".*\\.pkg\\.tar\\.xz"), DirectoryFileFilter.DIRECTORY);
|
||||
File f = null;
|
||||
try
|
||||
{
|
||||
for (File file : ls)
|
||||
{
|
||||
f = file;
|
||||
if (!knownPkg.contains(file.getAbsolutePath()))
|
||||
{
|
||||
ut.begin();
|
||||
@@ -85,8 +87,18 @@ public class PacmanServiceEJB implements PacmanService
|
||||
ut.commit();
|
||||
}
|
||||
}
|
||||
} catch (Exception e)
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.log(Level.ALL, String.format("Error parsing %s", f.getAbsolutePath()));
|
||||
try
|
||||
{
|
||||
ut.rollback();
|
||||
}
|
||||
catch (SystemException e1)
|
||||
{
|
||||
throw new RuntimeException(e1);
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@@ -119,7 +131,7 @@ public class PacmanServiceEJB implements PacmanService
|
||||
data.name = savedName.get(0);
|
||||
}
|
||||
em.persist(data);
|
||||
log.log(Level.INFO, String.format("Persisting package %s", file.getName()));
|
||||
logger.log(Level.INFO, String.format("Persisting package %s", file.getName()));
|
||||
}
|
||||
|
||||
}
|
@@ -1,22 +1,28 @@
|
||||
package service;
|
||||
package org.jpacrepo.service;
|
||||
|
||||
import model.PkgData;
|
||||
import model.PkgList;
|
||||
import model.PkgName;
|
||||
import org.jpacrepo.context.ApplicationContext;
|
||||
import org.jpacrepo.context.*;
|
||||
import org.jpacrepo.model.HashList;
|
||||
import org.jpacrepo.model.PkgData;
|
||||
import org.jpacrepo.model.PkgList;
|
||||
import org.jpacrepo.model.PkgName;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import pacbase.Parser;
|
||||
import persistence.QueryEngine;
|
||||
import org.jpacrepo.pacbase.Hasher;
|
||||
import org.jpacrepo.pacbase.Parser;
|
||||
import org.jpacrepo.persistence.QueryEngine;
|
||||
|
||||
import javax.ejb.Singleton;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.*;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.*;
|
||||
import javax.ws.rs.core.Context;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Path("/pkg")
|
||||
@@ -36,18 +42,13 @@ public class PacmanWebService
|
||||
|
||||
|
||||
private String nameQuery = "SELECT pname FROM PkgName pname WHERE id = :name";
|
||||
private String fileQuery = "SELECT pdata FROM PkgData pdata";
|
||||
private String fileNameQuery = "SELECT pdata FROM PkgData pdata WHERE fileName = :fileName";
|
||||
private String hashQuery = "SELECT pdata FROM PkgData pdata WHERE md5sum = :md5sum";
|
||||
private String idQuery = "SELECT pdata FROM PkgData pdata WHERE id = :id";
|
||||
|
||||
@Inject
|
||||
@DefaultConfiguration
|
||||
private ApplicationContext ctx;
|
||||
|
||||
/**
|
||||
* JSON : curl -X GET -H "Accept: application/json" http://localhost:8080/chapter15-service-1.0/rs/book/1 -v
|
||||
* XML : curl -X GET -H "Accept: application/xml" http://localhost:8080/chapter15-service-1.0/rs/book/1 -v
|
||||
*/
|
||||
@GET
|
||||
@Path("search")
|
||||
public Response getPackage(@QueryParam("name") String name,
|
||||
@@ -68,12 +69,10 @@ public class PacmanWebService
|
||||
{
|
||||
QueryEngine qe = new QueryEngine(PkgData.class);
|
||||
qe.select();
|
||||
TypedQuery<PkgData> fquery = em.createQuery(fileQuery, PkgData.class);
|
||||
if (name != null) qe.where("name", "=", name);
|
||||
if (version != null) qe.where("version", "=", version);
|
||||
if (arch != null) qe.where("arch", "=", arch);
|
||||
String query = qe.build();
|
||||
// log.log(Level.INFO, query);
|
||||
return manageQueryResult(em.createQuery(query).getResultList());
|
||||
}
|
||||
else
|
||||
@@ -82,6 +81,16 @@ public class PacmanWebService
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("hashes")
|
||||
public Response getHashes()
|
||||
{
|
||||
Query query = em.createQuery("SELECT p.md5sum FROM PkgData p");
|
||||
List<String> hashes = query.getResultList();
|
||||
HashList hl = new HashList(hashes);
|
||||
return Response.ok(hl).build();
|
||||
}
|
||||
|
||||
private Response getPackageByHash(String md5sum)
|
||||
{
|
||||
TypedQuery<PkgData> hquery = em.createQuery(hashQuery, PkgData.class);
|
||||
@@ -109,16 +118,22 @@ public class PacmanWebService
|
||||
StreamingOutput stream = (OutputStream output) ->
|
||||
{
|
||||
FileInputStream input = new FileInputStream(ctx.getFile(pkg));
|
||||
try {
|
||||
try
|
||||
{
|
||||
int bytes;
|
||||
while ((bytes = input.read()) != -1) {
|
||||
while ((bytes = input.read()) != -1)
|
||||
{
|
||||
output.write(bytes);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new WebApplicationException(e);
|
||||
} finally {
|
||||
if (output != null) output.close();
|
||||
if (input != null) input.close();
|
||||
}
|
||||
finally
|
||||
{
|
||||
output.close();
|
||||
input.close();
|
||||
}
|
||||
};
|
||||
return Response.ok(stream).header("Content-Length", ctx.getFile(pkg).length()).build();
|
||||
@@ -145,12 +160,12 @@ public class PacmanWebService
|
||||
if (filename == null)
|
||||
throw new BadRequestException();
|
||||
|
||||
File file = new File("/tmp/"/*ctx.getSystemProperties().getProperty("RepoFolder")*/, filename);
|
||||
File file = new File(ctx.getRepoFolder(), filename);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
IOUtils.copy(input, fos);
|
||||
fos.close();
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
String hash = Parser.computeMD5(fis);
|
||||
String hash = Hasher.computeMD5(fis);
|
||||
TypedQuery<PkgData> hquery = em.createQuery(hashQuery, PkgData.class);
|
||||
hquery.setParameter("md5sum", hash);
|
||||
|
||||
@@ -158,7 +173,7 @@ public class PacmanWebService
|
||||
|
||||
if (savedFiles.size() > 0)
|
||||
{
|
||||
file.delete();
|
||||
Files.delete(file.toPath());
|
||||
return Response.notModified().build();
|
||||
}
|
||||
else
|
||||
@@ -183,6 +198,7 @@ public class PacmanWebService
|
||||
}
|
||||
|
||||
em.persist(pkg);
|
||||
log.log(Level.INFO, String.format("Persisiting package %s", pkg.fileName));
|
||||
URI pkgUri = uriInfo.getAbsolutePathBuilder().path(pkg.fileName).build();
|
||||
return Response.created(pkgUri).build();
|
||||
}
|
||||
@@ -218,58 +234,4 @@ public class PacmanWebService
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// = Public Methods =
|
||||
// ======================================
|
||||
|
||||
/**
|
||||
* curl -X POST --data-binary "<book><description>Science fiction comedy book</description><illustrations>false</illustrations><isbn>1-84023-742-2</isbn><nbOfPage>354</nbOfPage><price>12.5</price><title>The Hitchhiker's Guide to the Galaxy</title></book>" -H "Content-Type: application/xml" http://localhost:8080/chapter15-service-1.0/rs/book -v
|
||||
* curl -X POST --data-binary "{\"description\":\"Science fiction comedy book\",\"illustrations\":false,\"isbn\":\"1-84023-742-2\",\"nbOfPage\":354,\"price\":12.5,\"title\":\"The Hitchhiker's Guide to the Galaxy\"}" -H "Content-Type: application/json" http://localhost:8080/chapter15-service-1.0/rs/book -v
|
||||
*/
|
||||
// @POST
|
||||
// public Response createBook(Book book) {
|
||||
// if (book == null)
|
||||
// throw new BadRequestException();
|
||||
//
|
||||
// em.persist(book);
|
||||
// URI bookUri = uriInfo.getAbsolutePathBuilder().path(book.getId()).build();
|
||||
// return Response.created(bookUri).build();
|
||||
// }
|
||||
|
||||
// @PUT
|
||||
// public Response updateBook(Book book) {
|
||||
// if (book == null)
|
||||
// throw new BadRequestException();
|
||||
//
|
||||
// em.merge(book);
|
||||
// return Response.ok().build();
|
||||
// }
|
||||
|
||||
/**
|
||||
* curl -X DELETE http://localhost:8080/chapter15-service-1.0/rs/book/1 -v
|
||||
*/
|
||||
// @DELETE
|
||||
// @Path("{id}")
|
||||
// public Response deleteBook(@PathParam("id") String id) {
|
||||
// Book book = em.find(Book.class, id);
|
||||
//
|
||||
// if (book == null)
|
||||
// throw new NotFoundException();
|
||||
//
|
||||
// em.remove(book);
|
||||
//
|
||||
// return Response.noContent().build();
|
||||
// }
|
||||
|
||||
/**
|
||||
* JSON : curl -X GET -H "Accept: application/json" http://localhost:8080/chapter15-service-1.0/rs/book -v
|
||||
* XML : curl -X GET -H "Accept: application/xml" http://localhost:8080/chapter15-service-1.0/rs/book -v
|
||||
*/
|
||||
// @GET
|
||||
// public Response getAllBooks() {
|
||||
// TypedQuery<Book> query = em.createNamedQuery(Book.FIND_ALL, Book.class);
|
||||
// Books books = new Books(query.getResultList());
|
||||
// return Response.ok(books).build();
|
||||
// }
|
||||
}
|
0
src/main/resources/META-INF/beans.xml
Normal file
0
src/main/resources/META-INF/beans.xml
Normal file
@@ -1,29 +1,17 @@
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import model.PkgData;
|
||||
import org.jboss.resteasy.annotations.interception.ServerInterceptor;
|
||||
import org.jboss.resteasy.core.ResourceMethodInvoker;
|
||||
import org.jboss.resteasy.core.ServerResponse;
|
||||
import org.jpacrepo.model.PkgData;
|
||||
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
|
||||
import org.jboss.resteasy.plugins.providers.jackson.ResteasyJacksonProvider;
|
||||
import org.jboss.resteasy.spi.Failure;
|
||||
import org.jboss.resteasy.spi.HttpRequest;
|
||||
import org.jboss.resteasy.spi.ResteasyProviderFactory;
|
||||
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;
|
||||
import org.jboss.resteasy.spi.metadata.ResourceMethod;
|
||||
import org.junit.Test;
|
||||
import pacbase.Parser;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.client.*;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by walter on 29/03/15.
|
||||
@@ -56,23 +44,17 @@ public class ClientTest
|
||||
{
|
||||
ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();
|
||||
RegisterBuiltin.register(instance);
|
||||
instance.registerProvider(ResteasyJacksonProvider.class);
|
||||
Client client = ClientBuilder.newClient();
|
||||
UriBuilder builder = UriBuilder.fromUri("http://localhost:8080/").path("jpacrepo-1.0/rest/pkg/upload");
|
||||
builder.matrixParam("filename", "k290-fnkeyctl-1.2-1-x86_64.pkg.tar.xz");
|
||||
WebTarget target = client.target(builder.build());
|
||||
FileInputStream fis = new FileInputStream(new File("/tmp/k290-fnkeyctl-1.2-1-x86_64.pkg.tar.xz"));
|
||||
FileInputStream fis = new FileInputStream(new File("/tmp/ciao/k290-fnkeyctl-1.2-1-x86_64.pkg.tar.xz"));
|
||||
|
||||
byte[] data = new byte[fis.available()];
|
||||
fis.read(data);
|
||||
Entity<byte[]> e = Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM);
|
||||
Entity<FileInputStream> e = Entity.entity(fis, MediaType.APPLICATION_OCTET_STREAM);
|
||||
Invocation invocation = target.request().buildPost(e);
|
||||
System.out.println(target.request().toString());
|
||||
Response response = invocation.invoke();
|
||||
System.out.println(response.getStatusInfo());
|
||||
if (response.getStatusInfo() == Response.Status.OK)
|
||||
{
|
||||
System.out.println(response.getStatusInfo());
|
||||
}
|
||||
assert Response.Status.CREATED.getStatusCode() == response.getStatus();
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,10 @@
|
||||
import model.PkgData;
|
||||
import org.jpacrepo.model.PkgData;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.filefilter.DirectoryFileFilter;
|
||||
import org.apache.commons.io.filefilter.RegexFileFilter;
|
||||
import org.junit.Test;
|
||||
import pacbase.Parser;
|
||||
import service.PacmanService;
|
||||
import org.jpacrepo.pacbase.Parser;
|
||||
import org.jpacrepo.service.PacmanService;
|
||||
|
||||
import javax.naming.*;
|
||||
import java.io.File;
|
||||
|
Reference in New Issue
Block a user