/** * Creates a source using a local file. * * <p>POST /andromeda/source?username=$BIGML_USERNAME;api_key=$BIGML_API_KEY; HTTP/1.1 Host: * bigml.io Content-Type: multipart/form-data; * * @param fileName file containing your data in csv format. It can be compressed, gzipped, or * zipped. Required multipart/form-data; charset=utf-8 * @param name the name you want to give to the new source. Optional * @param sourceParser set of parameters to parse the source. Optional * @param args set of parameters for the new model. Optional */ public JSONObject createLocalSource( final String fileName, final String name, final JSONObject sourceParser, final JSONObject args) { int code = HTTP_INTERNAL_SERVER_ERROR; String resourceId = null; JSONObject resource = null; String location = ""; JSONObject error = new JSONObject(); JSONObject status = new JSONObject(); status.put("code", code); status.put("message", "The resource couldn't be created"); error.put("status", status); try { MultipartUtility multipartUtility = new MultipartUtility(SOURCE_URL + bigmlAuth, "UTF-8"); multipartUtility.addFilePart("bin", new File(fileName)); if (name != null) { multipartUtility.addFormField("name", name); } // Source parser if (sourceParser != null) { multipartUtility.addFormField( "source_parser", Utils.unescapeJSONString(sourceParser.toJSONString())); } if (args != null) { for (String key : (Set<String>) args.keySet()) { multipartUtility.addFormField( key, Utils.unescapeJSONString(String.valueOf(args.get(key)))); } } HttpURLConnection connection = multipartUtility.finish(); code = connection.getResponseCode(); if (code == HTTP_CREATED) { location = connection.getHeaderField(location); resource = (JSONObject) JSONValue.parse(Utils.inputStreamAsString(connection.getInputStream(), "UTF-8")); resourceId = (String) resource.get("resource"); error = new JSONObject(); } else { if (code == HTTP_BAD_REQUEST || code == HTTP_UNAUTHORIZED || code == HTTP_PAYMENT_REQUIRED || code == HTTP_NOT_FOUND) { error = (JSONObject) JSONValue.parse(Utils.inputStreamAsString(connection.getInputStream(), "UTF-8")); } else { logger.info("Unexpected error (" + code + ")"); code = HTTP_INTERNAL_SERVER_ERROR; } } } catch (Throwable e) { logger.error("Error creating source", e); } // Cache the resource if the resource if ready if (cacheManager != null && resource != null && isResourceReady(resource)) { cacheManager.put(resourceId, null, resource); } JSONObject result = new JSONObject(); result.put("code", code); result.put("resource", resourceId); result.put("location", location); result.put("object", resource); result.put("error", error); return result; }