List of usage examples for java.net HttpURLConnection HTTP_UNSUPPORTED_TYPE
int HTTP_UNSUPPORTED_TYPE
To view the source code for java.net HttpURLConnection HTTP_UNSUPPORTED_TYPE.
Click Source Link
From source file:org.cellprofiler.subimager.ImageWriterHandler.java
public void handle(HttpExchange exchange) throws IOException { if (!exchange.getRequestMethod().equals("POST")) { reportError(exchange, HttpURLConnection.HTTP_BAD_METHOD, "<html><body>writeimage only accepts HTTP post</body></html>"); return;/*from ww w.j a v a 2 s.com*/ } final String contentType = exchange.getRequestHeaders().getFirst("Content-Type"); if (contentType == null) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>No Content-type request header</body></html>"); return; } if (!contentType.startsWith(MULTIPART_FORM_DATA)) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Content type must be " + MULTIPART_FORM_DATA + ".</body></html>"); return; } int idx = contentType.indexOf(BOUNDARY_EQUALS, MULTIPART_FORM_DATA.length()); if (idx == -1) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Did not find boundary identifier in multipart/form-data content type.</body></html>"); return; } final String contentEncoding = exchange.getRequestHeaders().getFirst("Content-Encoding"); String contentLengthString = exchange.getRequestHeaders().getFirst("Content-Length"); if (contentLengthString == null) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>No Content-Length request header</body></html>"); return; } try { Integer.parseInt(contentLengthString); } catch (NumberFormatException e) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, String .format("<html><body>Content length was not a number: %s</body></html>", contentLengthString)); return; } final int contentLength = Integer.parseInt(contentLengthString); final InputStream requestBodyStream = exchange.getRequestBody(); FileUpload upload = new FileUpload(); FileItemIterator fileItemIterator; String omeXML = null; URI uri = null; int index = 0; NDImage ndimage = null; String compression = DEFAULT_COMPRESSION; try { fileItemIterator = upload.getItemIterator(new RequestContext() { public String getCharacterEncoding() { return contentEncoding; } public String getContentType() { return contentType; } public int getContentLength() { return contentLength; } public InputStream getInputStream() throws IOException { return requestBodyStream; } }); while (fileItemIterator.hasNext()) { FileItemStream fis = fileItemIterator.next(); String name = fis.getFieldName(); if (name.equals(NAME_IMAGE)) { String imageContentType = fis.getContentType(); if (imageContentType == null) { reportError(exchange, HttpURLConnection.HTTP_UNSUPPORTED_TYPE, "<html><body>Image form-data field must have a content type.</body></html>"); return; } try { InputStream is = SubimagerUtils.getInputStream(exchange, fis); if (is == null) return; ndimage = NDImage.decode(is); } catch (MalformedStreamException e) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Failed to read body for part, " + name + ".</body></html>"); } catch (IOException e) { reportError(exchange, HttpURLConnection.HTTP_INTERNAL_ERROR, "<html><body>Failed to read multipart body data</body></html>"); return; } catch (org.cellprofiler.subimager.NDImage.FormatException e) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Image data was not in correct format: " + e.getMessage() + "</body></html>"); return; } } else { String partDataString = SubimagerUtils.readFully(exchange, fis); if (partDataString == null) return; if (name.equals(NAME_INDEX)) { try { index = Integer.parseInt(partDataString); } catch (NumberFormatException e) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Index form-data field must be a number: " + partDataString + ".</body></html>"); return; } } else if (name.equals(NAME_OMEXML)) { omeXML = partDataString; } else if (name.equals(NAME_URL)) { try { uri = new URI(partDataString); } catch (URISyntaxException e) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Improperly formatted URL: " + partDataString + ".</body></html>"); return; } } else if (name.equals(NAME_COMPRESSION)) { compression = partDataString; } } } if (ndimage == null) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Request did not have an image part</body></html>"); return; } if (uri == null) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Request did not have a URL part</body></html>"); return; } if (omeXML == null) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, "<html><body>Request did not have an omexml part</body></html>"); return; } writeImage(exchange, ndimage, uri, omeXML, index, compression); } catch (FileUploadException e) { reportError(exchange, HttpURLConnection.HTTP_BAD_REQUEST, String .format("<html><body>Parsing error in multipart message: %s</body></html>", e.getMessage())); return; } }
From source file:org.openrdf.http.client.HTTPClient.java
protected void upload(RequestEntity reqEntity, String baseURI, boolean overwrite, Resource... contexts) throws IOException, RDFParseException, RepositoryException, UnauthorizedException { OpenRDFUtil.verifyContextNotNull(contexts); checkRepositoryURL();//from w w w.j a v a2 s . com String uploadURL = Protocol.getStatementsLocation(getRepositoryURL()); // Select appropriate HTTP method EntityEnclosingMethod method; if (overwrite) { method = new PutMethod(uploadURL); } else { method = new PostMethod(uploadURL); } setDoAuthentication(method); // Set relevant query parameters List<NameValuePair> params = new ArrayList<NameValuePair>(5); for (String encodedContext : Protocol.encodeContexts(contexts)) { params.add(new NameValuePair(Protocol.CONTEXT_PARAM_NAME, encodedContext)); } if (baseURI != null && baseURI.trim().length() != 0) { String encodedBaseURI = Protocol.encodeValue(new URIImpl(baseURI)); params.add(new NameValuePair(Protocol.BASEURI_PARAM_NAME, encodedBaseURI)); } method.setQueryString(params.toArray(new NameValuePair[params.size()])); // Set payload method.setRequestEntity(reqEntity); // Send request try { int httpCode = httpClient.executeMethod(method); if (httpCode == HttpURLConnection.HTTP_UNAUTHORIZED) { throw new UnauthorizedException(); } else if (httpCode == HttpURLConnection.HTTP_UNSUPPORTED_TYPE) { throw new UnsupportedRDFormatException(method.getResponseBodyAsString()); } else if (!is2xx(httpCode)) { ErrorInfo errInfo = ErrorInfo.parse(method.getResponseBodyAsString()); if (errInfo.getErrorType() == ErrorType.MALFORMED_DATA) { throw new RDFParseException(errInfo.getErrorMessage()); } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_FILE_FORMAT) { throw new UnsupportedRDFormatException(errInfo.getErrorMessage()); } else { throw new RepositoryException("Failed to upload data: " + errInfo); } } } finally { releaseConnection(method); } }