Search in sources :

Example 1 with SlotRequest

use of org.jivesoftware.smackx.httpfileupload.element.SlotRequest in project Smack by igniterealtime.

the class SlotRequestCreateTest method checkSlotRequestCreationWithoutContentType.

@Test
public void checkSlotRequestCreationWithoutContentType() throws XmppStringprepException {
    SlotRequest slotRequest = new SlotRequest(JidTestUtil.DOMAIN_BARE_JID_1, "my_romeo.png", 52523);
    Assert.assertEquals("my_romeo.png", slotRequest.getFilename());
    Assert.assertEquals(52523, slotRequest.getSize());
    Assert.assertEquals(null, slotRequest.getContentType());
    Assert.assertEquals(testRequestWithoutContentType, slotRequest.getChildElementXML().toString());
}
Also used : SlotRequest(org.jivesoftware.smackx.httpfileupload.element.SlotRequest) Test(org.junit.Test)

Example 2 with SlotRequest

use of org.jivesoftware.smackx.httpfileupload.element.SlotRequest in project Smack by igniterealtime.

the class HttpFileUploadManager method requestSlot.

/**
     * Request a new upload slot with optional content type from custom upload service.
     *
     * When you get slot you should upload file to PUT URL and share GET URL.
     * Note that this is a synchronous call -- Smack must wait for the server response.
     *
     * @param filename name of file to be uploaded
     * @param fileSize file size in bytes.
     * @param contentType file content-type or null
     * @param uploadServiceAddress the address of the upload service to use or null for default one
     * @return file upload Slot in case of success
     * @throws IllegalArgumentException if fileSize is less than or equal to zero or greater than the maximum size
     *         supported by the service.
     * @throws SmackException
     * @throws InterruptedException
     * @throws XMPPException.XMPPErrorException
     */
public Slot requestSlot(String filename, long fileSize, String contentType, DomainBareJid uploadServiceAddress) throws SmackException, InterruptedException, XMPPException.XMPPErrorException {
    final XMPPConnection connection = connection();
    final UploadService defaultUploadService = this.defaultUploadService;
    // The upload service we are going to use.
    UploadService uploadService;
    if (uploadServiceAddress == null) {
        uploadService = defaultUploadService;
    } else {
        if (defaultUploadService != null && defaultUploadService.getAddress().equals(uploadServiceAddress)) {
            // Avoid performing a service discovery if we already know about the given service.
            uploadService = defaultUploadService;
        } else {
            DiscoverInfo discoverInfo = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(uploadServiceAddress);
            if (!containsHttpFileUploadNamespace(discoverInfo)) {
                throw new IllegalArgumentException("There is no HTTP upload service running at the given address '" + uploadServiceAddress + '\'');
            }
            uploadService = uploadServiceFrom(discoverInfo);
        }
    }
    if (uploadService == null) {
        throw new SmackException("No upload service specified and also none discovered.");
    }
    if (!uploadService.acceptsFileOfSize(fileSize)) {
        throw new IllegalArgumentException("Requested file size " + fileSize + " is greater than max allowed size " + uploadService.getMaxFileSize());
    }
    SlotRequest slotRequest;
    switch(uploadService.getVersion()) {
        case v0_3:
            slotRequest = new SlotRequest(uploadService.getAddress(), filename, fileSize, contentType);
            break;
        case v0_2:
            slotRequest = new SlotRequest_V0_2(uploadService.getAddress(), filename, fileSize, contentType);
            break;
        default:
            throw new AssertionError();
    }
    return connection.createStanzaCollectorAndSend(slotRequest).nextResultOrThrow();
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) SmackException(org.jivesoftware.smack.SmackException) XMPPConnection(org.jivesoftware.smack.XMPPConnection) SlotRequest_V0_2(org.jivesoftware.smackx.httpfileupload.element.SlotRequest_V0_2) SlotRequest(org.jivesoftware.smackx.httpfileupload.element.SlotRequest)

Example 3 with SlotRequest

use of org.jivesoftware.smackx.httpfileupload.element.SlotRequest in project Smack by igniterealtime.

the class SlotRequestCreateTest method checkSlotRequestCreation.

@Test
public void checkSlotRequestCreation() throws XmppStringprepException {
    SlotRequest slotRequest = new SlotRequest(JidTestUtil.DOMAIN_BARE_JID_1, "my_juliet.png", 23456, "image/jpeg");
    Assert.assertEquals("my_juliet.png", slotRequest.getFilename());
    Assert.assertEquals(23456, slotRequest.getSize());
    Assert.assertEquals("image/jpeg", slotRequest.getContentType());
    Assert.assertEquals(testRequest, slotRequest.getChildElementXML().toString());
}
Also used : SlotRequest(org.jivesoftware.smackx.httpfileupload.element.SlotRequest) Test(org.junit.Test)

Aggregations

SlotRequest (org.jivesoftware.smackx.httpfileupload.element.SlotRequest)3 Test (org.junit.Test)2 SmackException (org.jivesoftware.smack.SmackException)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)1 SlotRequest_V0_2 (org.jivesoftware.smackx.httpfileupload.element.SlotRequest_V0_2)1