Anonymous avatar Anonymous committed 21eb62a

Initial commit of sources.

Comments (0)

Files changed (4)

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>br.usp.ime.vision.vision.dataset</groupId>
+    <artifactId>visiondataset-client</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>visiondataset-client</name>
+    <url>http://maven.apache.org</url>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <repositories>
+        <repository>
+            <id>maven2-repository.java.net</id>
+            <name>Java.net Repository for Maven</name>
+            <url>http://download.java.net/maven/2/</url>
+            <layout>default</layout>
+        </repository> 
+    </repositories>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>1.7.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.sun.jersey</groupId>
+            <artifactId>jersey-client</artifactId>
+            <version>1.8</version>
+        </dependency>
+        <dependency>
+            <groupId>com.sun.jersey.contribs</groupId>
+            <artifactId>jersey-multipart</artifactId>
+            <version>1.8</version>
+        </dependency>
+
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.2</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

src/main/java/br/usp/ime/vision/dataset/ws/client/Attachment.java

+package br.usp.ime.vision.dataset.ws.client;
+
+import java.util.Date;
+
+public class Attachment {
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return String.format("%s", name);
+    }
+
+    private int id;
+
+    private Date dateCreation;
+
+    private String name;
+    
+    private int imageId;
+    
+    private String uri;
+    
+    
+    
+
+    /**
+     * @return the uri
+     */
+    public String getUri() {
+        return uri;
+    }
+
+    /**
+     * @param uri the uri to set
+     */
+    public void setUri(String uri) {
+        this.uri = uri;
+    }
+
+    /**
+     * @return the id
+     */
+    public int getId() {
+        return id;
+    }
+
+    /**
+     * @param id the id to set
+     */
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    /**
+     * @return the dateCreation
+     */
+    public Date getDateCreation() {
+        return dateCreation;
+    }
+
+    /**
+     * @param dateCreation the dateCreation to set
+     */
+    public void setDateCreation(Date dateCreation) {
+        this.dateCreation = dateCreation;
+    }
+
+    /**
+     * @return the name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * @param name the name to set
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * @return the imageId
+     */
+    public int getImageId() {
+        return imageId;
+    }
+
+    /**
+     * @param imageId the imageId to set
+     */
+    public void setImageId(int imageId) {
+        this.imageId = imageId;
+    } 
+    
+
+}

src/main/java/br/usp/ime/vision/dataset/ws/client/NotFoundException.java

+package br.usp.ime.vision.dataset.ws.client;
+
+public class NotFoundException extends Exception {
+
+    /**
+     * 
+     */
+    private static final long serialVersionUID = -3599225106601387409L;
+
+}

src/main/java/br/usp/ime/vision/dataset/ws/client/VisionDatasetClient.java

+package br.usp.ime.vision.dataset.ws.client;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.ws.rs.core.MediaType;
+
+import com.google.gson.Gson;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.multipart.FormDataMultiPart;
+
+public class VisionDatasetClient {
+    private final Client client;
+    private final Gson gson;
+
+    /**
+     * Client for the visionDataset webservice.
+     */
+    public VisionDatasetClient() {
+        client = Client.create();
+        gson = new Gson();
+    }
+
+    /**
+     * Gets the attachment list of an image.
+     * 
+     * @param imageUri
+     *            <code>id</code> of the image.
+     * @return a list of {@link Attachment}
+     * @throws NotFoundException
+     */
+    public List<Attachment> attachmentList(String imageUri) throws NotFoundException {
+        final List<Attachment> attachments = new ArrayList<Attachment>();
+        final WebResource resource = getImageResource(imageUri);
+        final String attachmentsString = resource.path("/attachments/").get(String.class);
+        if (attachmentsString == null) {
+            throw new NotFoundException();
+        }
+        final List<String> list = Arrays.asList(gson.fromJson(attachmentsString, String[].class));
+        for (final String uri : list) {
+            attachments.add(getAttachment(uri));
+
+        }
+        return attachments;
+    }
+
+    /**
+     * Create a new attachment.
+     * 
+     * @param imageUri
+     *            URI of the image to attach the file.
+     * @param name
+     *            name of the file to be attached.
+     * @return the Attachment created.
+     * @throws NotFoundException
+     */
+    public Attachment createAttachment(String imageUri, String name) throws NotFoundException {
+        final WebResource imageResource = getImageResource(imageUri);
+        final String jsonName = gson.toJson(name);
+        final String jsonAttachment = imageResource.type(MediaType.APPLICATION_JSON_TYPE).post(String.class,
+                jsonName);
+        final String attachmentUri = gson.fromJson(jsonAttachment, String.class);
+        return getAttachment(attachmentUri);
+    }
+
+    /**
+     * Get attachment from the uri.
+     * 
+     * @param uri
+     *            <code>URI</code> of the attachment.
+     * @return the {@link Attachment}
+     */
+    private Attachment getAttachment(String uri) {
+        final String attachmentJson = getAttachmentResource(uri).get(String.class);
+        final Attachment attachment = gson.fromJson(attachmentJson, Attachment.class);
+        attachment.setUri(uri);
+        return attachment;
+    }
+
+    /**
+     * Get the file of attachment.
+     * 
+     * @param attachment
+     *            attachment with an URI.
+     * @return the attached {@link File}
+     * @throws NotFoundException
+     *             if the attachment was not found.
+     */
+    public File getAttachmentFile(Attachment attachment) throws NotFoundException {
+        return this.getAttachmentFile(attachment.getUri());
+
+    }
+
+    /**
+     * Get the attachment's file by its URI.
+     * 
+     * @param attachmentUri
+     *            <code>URI</code> of the attachment on remote system.
+     * @return the attachment {@link File}.
+     * @throws NotFoundException
+     */
+    public File getAttachmentFile(String attachmentUri) throws NotFoundException {
+        final File file = getAttachmentResource(attachmentUri).path("/file/").get(File.class);
+        return file;
+    }
+
+    private WebResource getAttachmentResource(String uri) {
+        return client.resource(uri);
+    }
+
+    /**
+     * Get the image from the <code>URI</code> on remote system.
+     * 
+     * 
+     * @param imageUri
+     *            <code>URI</code> of the image.
+     * @return a {@link File} of the image Stream.
+     * @throws NotFoundException
+     */
+    public File getImage(String imageUri) throws NotFoundException {
+        final WebResource imageResource = getImageResource(imageUri);
+        final File imageFile = imageResource.get(File.class);
+        if (imageFile == null) {
+            throw new NotFoundException();
+        }
+        return imageFile;
+    }
+
+    private WebResource getImageResource(String imageUri) {
+        return client.resource(imageUri);
+    }
+
+    /**
+     * Overwrite the attachment file.
+     * 
+     * @param attachmentUri
+     *            <code>URI</code> of the attachment.
+     * @param file
+     *            the new {@link File} of the image attachment.
+     * @throws NotFoundException
+     */
+    public void updateAttachment(String attachmentUri, File file) throws NotFoundException {
+        final WebResource r = getAttachmentResource(attachmentUri);
+        final FormDataMultiPart form = new FormDataMultiPart();
+        form.field("file", file, MediaType.MULTIPART_FORM_DATA_TYPE);
+        r.type(MediaType.MULTIPART_FORM_DATA).put(form);
+
+    }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.