/** * Builder pattern method to return this {@link MultiPart} after * additional configuration. * * @param type media type to set for this {@link MultiPart}. */ @Override public MultiPart type(MediaType type) { setMediaType(type); return this; }
/** * Disables changing the media type to anything other than {@code multipart/form-data}. * * @param mediaType the proposed media type. * @throws IllegalArgumentException if the proposed media type is not {@code multipart/form-data}. */ @Override public void setMediaType(MediaType mediaType) { if (!MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE)) { throw new IllegalArgumentException(LocalizationMessages.FORM_DATA_MULTIPART_CANNOT_CHANGE_MEDIATYPE()); } super.setMediaType(mediaType); }
public class Slimclient { private static final String TARGET_URL = "http://localhost:49158/rest/service/upload"; public Slimclient() { Client client = ClientBuilder.newBuilder() .register(MultiPartFeature.class).build(); WebTarget webTarget = client.target(TARGET_URL); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", new File("C:/Users/Nicklas/Desktop/aab.txt"), MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.bodyPart(fileDataBodyPart); Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(multiPart, multiPart.getMediaType())); System.out.println(response.getStatus() + " " + response.getStatusInfo() + " " + response); } public static void main(String[] args) { new Slimclient(); } }
multiPart.setMediaType(mediaType); fileNameFix = false; } else {
/** * Builder pattern method to return this {@link MultiPart} after * additional configuration. * * @param type media type to set for this {@link MultiPart}. */ @Override public MultiPart type(MediaType type) { setMediaType(type); return this; }
/** * Builder pattern method to return this {@link MultiPart} after * additional configuration. * * @param type media type to set for this {@link MultiPart}. */ @Override public MultiPart type(MediaType type) { setMediaType(type); return this; }
/** * Builder pattern method to return this {@link MultiPart} after * additional configuration. * * @param type media type to set for this {@link MultiPart}. */ @Override public MultiPart type(MediaType type) { setMediaType(type); return this; }
/** * Builder pattern method to return this {@link MultiPart} after * additional configuration. * * @param type media type to set for this {@link MultiPart}. */ @Override public MultiPart type(MediaType type) { setMediaType(type); return this; }
/** * Disables changing the media type to anything other than {@code multipart/form-data}. * * @param mediaType the proposed media type. * @throws IllegalArgumentException if the proposed media type is not {@code multipart/form-data}. */ @Override public void setMediaType(MediaType mediaType) { if (!MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE)) { throw new IllegalArgumentException(LocalizationMessages.FORM_DATA_MULTIPART_CANNOT_CHANGE_MEDIATYPE()); } super.setMediaType(mediaType); }
/** * Disables changing the media type to anything other than {@code multipart/form-data}. * * @param mediaType the proposed media type. * @throws IllegalArgumentException if the proposed media type is not {@code multipart/form-data}. */ @Override public void setMediaType(MediaType mediaType) { if (!MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE)) { throw new IllegalArgumentException(LocalizationMessages.FORM_DATA_MULTIPART_CANNOT_CHANGE_MEDIATYPE()); } super.setMediaType(mediaType); }
/** * Disables changing the media type to anything other than {@code multipart/form-data}. * * @param mediaType the proposed media type. * @throws IllegalArgumentException if the proposed media type is not {@code multipart/form-data}. */ @Override public void setMediaType(MediaType mediaType) { if (!MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE)) { throw new IllegalArgumentException(LocalizationMessages.FORM_DATA_MULTIPART_CANNOT_CHANGE_MEDIATYPE()); } super.setMediaType(mediaType); }
/** * Disables changing the media type to anything other than {@code multipart/form-data}. * * @param mediaType the proposed media type. * @throws IllegalArgumentException if the proposed media type is not {@code multipart/form-data}. */ @Override public void setMediaType(MediaType mediaType) { if (!MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE)) { throw new IllegalArgumentException(LocalizationMessages.FORM_DATA_MULTIPART_CANNOT_CHANGE_MEDIATYPE()); } super.setMediaType(mediaType); }
Client client = ClientBuilder.newClient(); client.register(MultiPartFeature.class); WebTarget target = client.target("https://apisomeurl.com"); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); //Image here FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("image", new File("/some/img/path/img.png")); multiPart.bodyPart(fileDataBodyPart); //MediaType.APPLICATION_JSON_TYPE because I'm expecting a JSON response from the server String str = target.queryParam("param1", "1") .queryParam("param2", "2") .queryParam("param3", "3") .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(multiPart, multiPart.getMediaType()), String.class);
@Nullable String checkApiKey() { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_CHECK_API_KEY); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); JSONObject jObj = postData(webTarget, multiPart); if (jObj == null) { return null; } return Boolean.toString(jObj.getBoolean(CoordConsts.SVC_KEY_API_KEY)); } catch (Exception e) { _logger.errorf("exception: "); _logger.error(Throwables.getStackTraceAsString(e) + "\n"); return null; } }
boolean delAnalysis(String networkName, String analysisName) { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_ANALYSIS); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_ANALYSIS_NAME, analysisName); return postData(webTarget, multiPart) != null; } catch (Exception e) { _logger.errorf( "Exception when deleting analysis to %s using (%s, %s): %s\n", _coordWorkMgr, networkName, analysisName, Throwables.getStackTraceAsString(e)); return false; } }
boolean queueWork(WorkItem wItem) { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_QUEUE_WORK); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart( multiPart, CoordConsts.SVC_KEY_WORKITEM, BatfishObjectMapper.writeString(wItem)); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); JSONObject jObj = postData(webTarget, multiPart); return jObj != null; } catch (Exception e) { _logger.errorf("exception: "); _logger.error(Throwables.getStackTraceAsString(e) + "\n"); return false; } }
boolean delSnapshot(String networkName, String snapshotName) { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_SNAPSHOT); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_SNAPSHOT_NAME, snapshotName); JSONObject jObj = postData(webTarget, multiPart); return jObj != null; } catch (Exception e) { _logger.errorf("Exception in delSnapshot for network %s:\n", networkName); _logger.error(Throwables.getStackTraceAsString(e) + "\n"); return false; } }
boolean syncSnapshotsSyncNow(String pluginId, String networkName, boolean force) { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_SYNC_SNAPSHOTS_SYNC_NOW); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_VERSION, Version.getVersion()); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_PLUGIN_ID, pluginId); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_FORCE, String.valueOf(force)); JSONObject jObj = postData(webTarget, multiPart); return jObj != null; } catch (Exception e) { _logger.errorf("Exception syncing snapshots in network %s:\n", networkName); _logger.error(Throwables.getStackTraceAsString(e) + "\n"); return false; } }
boolean delQuestion(String networkName, String snapshotName, String questionName) { try { WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_DEL_QUESTION); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_SNAPSHOT_NAME, snapshotName); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_QUESTION_NAME, questionName); JSONObject jObj = postData(webTarget, multiPart); return jObj != null; } catch (Exception e) { _logger.errorf("exception: "); _logger.error(Throwables.getStackTraceAsString(e) + "\n"); return false; } }
boolean syncSnapshotsUpdateSettings( String pluginId, String networkName, Map<String, String> settings) { try { String settingsStr = BatfishObjectMapper.writePrettyString(settings); WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_SYNC_SNAPSHOTS_UPDATE_SETTINGS); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_VERSION, Version.getVersion()); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_PLUGIN_ID, pluginId); addTextMultiPart(multiPart, CoordConsts.SVC_KEY_SETTINGS, settingsStr); JSONObject jObj = postData(webTarget, multiPart); return jObj != null; } catch (Exception e) { _logger.errorf("Exception syncing snapshots in network %s:\n", networkName); _logger.error(Throwables.getStackTraceAsString(e) + "\n"); return false; } }