Example usage for java.net UnknownServiceException UnknownServiceException

List of usage examples for java.net UnknownServiceException UnknownServiceException

Introduction

In this page you can find the example usage for java.net UnknownServiceException UnknownServiceException.

Prototype

public UnknownServiceException(String msg) 

Source Link

Document

Constructs a new UnknownServiceException with the specified detail message.

Usage

From source file:org.modelio.vbasic.net.ApacheUriConnection.java

@objid("f8e1a3e4-45b3-4065-8838-90de7fe64eaa")
private void openConnection() throws IOException, IllegalStateException {
    this.context = HttpClientContext.create();

    CredentialsProvider credsProvider = new SystemDefaultCredentialsProvider();
    this.context.setCredentialsProvider(credsProvider);

    if (this.auth != null) {
        switch (this.auth.getSchemeId()) {
        case UserPasswordAuthData.USERPASS_SCHEME_ID:
            UserPasswordAuthData authData = (UserPasswordAuthData) this.auth;

            if (authData.getUser() == null)
                throw new ClientProtocolException(this.uri + ": User name may not be null.");

            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(authData.getUser(),
                    authData.getPassword());
            AuthScope authscope = new AuthScope(this.uri.getHost(), AuthScope.ANY_PORT);
            credsProvider.setCredentials(authscope, credentials);

            break;
        case NoneAuthData.AUTH_NONE_SCHEME_ID:
            break;

        default:/*from   w  w w .  j a  v a 2s .c o  m*/
            throw new UnknownServiceException(this.auth + " not supported for " + this.uri);
        }
    }

    /** support different proxy */
    configProxy(credsProvider);

    getRequest().setConfig(this.configBuilder.build());

    try {
        this.res = httpclient.execute(getRequest(), this.context);
        int statusCode = this.res.getStatusLine().getStatusCode();

        if (statusCode >= 200 && statusCode < 300) {
            // Try to get content now to get an exception on failure immediately
            this.res.getEntity().getContent();
        } else {
            handleConnectionFailure();
        }

    } catch (ClientProtocolException e) {
        throw new IOException(e.getLocalizedMessage(), e);
    }
}

From source file:org.apache.shindig.protocol.multipart.DefaultMultipartFormParser.java

public Collection<FormDataItem> parse(HttpServletRequest servletRequest) throws IOException {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);

    try {//from  w  ww .j  a v a 2s  .  c  o m
        @SuppressWarnings("unchecked")
        List<FileItem> fileItems = upload.parseRequest(servletRequest);
        return convertToFormData(fileItems);
    } catch (FileUploadException e) {
        UnknownServiceException use = new UnknownServiceException("File upload error.");
        use.initCause(e);
        throw use;
    }
}

From source file:typicalnerd.musicarchive.client.network.FileUploader.java

/**
 * Uploads the file to the web service./* w  w  w. jav a2  s.co m*/
 * 
 * @param file
 * The file's location in the file system.
 * 
 * @return
 * Returns the {@link FileUploadResult} in case of a successful upload.
 * 
 * @throws IOException
 */
private FileUploadResult uploadFile(Path file) throws IOException {
    URL url = new URL(baseUrl + "/files");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/octet-stream");
    connection.setRequestProperty("Content-Length", String.valueOf(Files.size(file)));
    connection.setRequestProperty("Accept", "application/json");
    connection.setDoOutput(true);
    connection.setDoInput(true);

    try (InputStream in = new BufferedInputStream(new FileInputStream(file.toFile()))) {
        try (OutputStream out = connection.getOutputStream()) {
            // Lazy as we are, we use one of the many nice functions in the Apache
            // Commons library.
            IOUtils.copy(in, out);
        }
    }

    // Now it's time to read the first result. If all goes well, this was a 
    // HTTP 201 - CREATED. If the file is already known, e.g. because hash and
    // name match an existing object, then HTTP 200 is returned and nothing was
    // changed on the server. In that case we query if there is meta data and if
    // so skip the upload in with the assumption that the latest version is already 
    // on the server. If not, we continue as planned.
    connection.connect();
    int result = connection.getResponseCode();

    if (200 != result || 201 != result) {
        try (InputStream in = connection.getInputStream()) {
            ErrorResponse e = mapper.readValue(in, ErrorResponse.class);
            throw new UnknownServiceException("Upload of file failed. " + e.getMessage());
        }
    }

    // Parse the response to get the location of where to put the meta data.
    // We expect a JSON response so let Jackson parse it into an expected response
    // object.
    FileUploadResult uploadResult = new FileUploadResult(result);
    try (InputStream in = connection.getInputStream()) {
        ObjectReader reader = mapper.readerForUpdating(uploadResult);
        reader.readValue(in);
    }
    return uploadResult;
}

From source file:typicalnerd.musicarchive.client.network.FileUploader.java

/**
 * Upload a file's meta data to the web service.
 * /*from www.j  a v  a2s .  c o m*/
 * @param metaData
 * The meta data of the file.
 * 
 * @param uploadResult
 * The result of the file upload. This contains the location where to POST the meta
 * data.
 * 
 * @return
 * Returns the {@link MetaUploadResult} in case of a successful upload.
 * 
 * @throws IOException
 */
private MetaUploadResult uploadMeta(TrackMetaData metaData, FileUploadResult uploadResult) throws IOException {
    // This time we need to turn things around and prepare the data first. Only then
    // can we specify the "Content-Length" header of the request. It is not necessary
    // per se, but it makes us a good citizen if we can tell the server how big the
    // data will be. In theory this would allow to calculate an ETA and other stats
    // that are not very relevant for this small data.
    String json = mapper.writeValueAsString(metaData);
    byte[] binary = json.getBytes(StandardCharsets.UTF_8);

    // Now we can do the URL setup dance.
    URL url = new URL(baseUrl + uploadResult.getMetaUrl());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Content-Length", String.valueOf(json.length()));
    connection.setRequestProperty("Accept", "application/json");
    connection.setDoOutput(true);
    connection.setDoInput(true);

    try (ByteArrayInputStream in = new ByteArrayInputStream(binary)) {
        try (OutputStream out = connection.getOutputStream()) {
            // Still lazy ;-)
            IOUtils.copy(in, out);
        }
    }

    connection.connect();
    int result = connection.getResponseCode();

    if (HttpURLConnection.HTTP_CREATED != result) {
        try (InputStream in = connection.getInputStream()) {
            ErrorResponse e = mapper.readValue(in, ErrorResponse.class);
            throw new UnknownServiceException("Upload of meta data failed. " + e.getMessage());
        }
    }

    // Fanfare! We're done.
    return new MetaUploadResult(result);
}