Java Code Examples for org.apache.commons.httpclient.methods.PutMethod#setRequestHeader()

The following examples show how to use org.apache.commons.httpclient.methods.PutMethod#setRequestHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
protected PutMethod internalCreateOrUpdate(String encodedHref, byte[] mimeContent) throws IOException {
    PutMethod putmethod = new PutMethod(encodedHref);
    putmethod.setRequestHeader("Translate", "f");
    putmethod.setRequestHeader("Overwrite", "f");
    if (etag != null) {
        putmethod.setRequestHeader("If-Match", etag);
    }
    if (noneMatch != null) {
        putmethod.setRequestHeader("If-None-Match", noneMatch);
    }
    putmethod.setRequestHeader("Content-Type", "message/rfc822");
    putmethod.setRequestEntity(new ByteArrayRequestEntity(mimeContent, "message/rfc822"));
    try {
        httpClient.executeMethod(putmethod);
    } finally {
        putmethod.releaseConnection();
    }
    return putmethod;
}
 
Example 2
Source File: AbstractSubmarineServerTest.java    From submarine with Apache License 2.0 5 votes vote down vote up
protected static PutMethod httpPut(String path, String body, String user, String pwd) throws IOException {
  LOG.info("Connecting to {}", URL + path);
  HttpClient httpClient = new HttpClient();
  PutMethod putMethod = new PutMethod(URL + path);
  putMethod.addRequestHeader("Origin", URL);
  putMethod.setRequestHeader("Content-type", "application/yaml");
  RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8"));
  putMethod.setRequestEntity(entity);
  if (userAndPasswordAreNotBlank(user, pwd)) {
    putMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
  }
  httpClient.executeMethod(putMethod);
  LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText());
  return putMethod;
}
 
Example 3
Source File: PutTest.java    From scim2-compliance-test-suite with Apache License 2.0 5 votes vote down vote up
private PutMethod getMethod(Resource resource, String path, String encoding) {
    PutMethod method = new PutMethod(this.csp.getUrl() + this.csp.getVersion() + path + resource.getId());

    ComplienceUtils.configureMethod(method);
    method.setRequestHeader(new Header("Accept", "application/" + encoding));
    if (resource.getMeta() != null && !resource.getMeta().getVersion().isEmpty()) {
        method.setRequestHeader(new Header("If-Match", resource.getMeta().getVersion()));
    }
    return method;
}
 
Example 4
Source File: Tr069DMAdapter.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void callPutFileApi(String fileType, String url, String filePath, String oui, String prodClass, String version) throws HttpException, IOException, HitDMException {
	org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
	
	PutMethod putMethod = new PutMethod(url);
	File input = new File(filePath);
	RequestEntity entity = new FileRequestEntity(input, "Content-Length: 1431");
	putMethod.setRequestEntity(entity);
	putMethod.setRequestHeader("fileType", fileType);
	putMethod.setRequestHeader("oui", oui);
	putMethod.setRequestHeader("productClass", prodClass);
	putMethod.setRequestHeader("version", version);
	
	client.executeMethod(putMethod);
	
	if (putMethod.getStatusCode() != 200) {
		String debugStr = "DM Server return:"+ putMethod.getStatusCode();
		byte[] body;
		try {
			body = putMethod.getResponseBody();
			if (body != null) {
				debugStr += "\r\nBody:"+new String(body);
			}
		} catch (Exception e) {
			debugStr += e.getMessage();
		}
		
		throw new HitDMException(putMethod.getStatusCode(), debugStr);
	}
	
}
 
Example 5
Source File: AbstractTestRestApi.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
protected static PutMethod httpPut(String path, String body, String user, String pwd)
        throws IOException {
  LOG.info("Connecting to {}", URL + path);
  HttpClient httpClient = new HttpClient();
  PutMethod putMethod = new PutMethod(URL + path);
  putMethod.addRequestHeader("Origin", URL);
  RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8"));
  putMethod.setRequestEntity(entity);
  if (userAndPasswordAreNotBlank(user, pwd)) {
    putMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
  }
  httpClient.executeMethod(putMethod);
  LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText());
  return putMethod;
}
 
Example 6
Source File: ZeppelinServerMock.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
protected static PutMethod httpPut(String path, String body, String user, String pwd)
    throws IOException {
  LOG.info("Connecting to {}", URL + path);
  HttpClient httpClient = new HttpClient();
  PutMethod putMethod = new PutMethod(URL + path);
  putMethod.addRequestHeader("Origin", URL);
  RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8"));
  putMethod.setRequestEntity(entity);
  if (userAndPasswordAreNotBlank(user, pwd)) {
    putMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
  }
  httpClient.executeMethod(putMethod);
  LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText());
  return putMethod;
}
 
Example 7
Source File: Tr069DMAdapter.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void callPutFileApi(String fileType, String url, String filePath, String oui, String prodClass, String version) throws HttpException, IOException, HitDMException {
	org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
	
	PutMethod putMethod = new PutMethod(url);
	File input = new File(filePath);
	RequestEntity entity = new FileRequestEntity(input, "Content-Length: 1431");
	putMethod.setRequestEntity(entity);
	putMethod.setRequestHeader("fileType", fileType);
	putMethod.setRequestHeader("oui", oui);
	putMethod.setRequestHeader("productClass", prodClass);
	putMethod.setRequestHeader("version", version);
	
	client.executeMethod(putMethod);
	
	if (putMethod.getStatusCode() != 200) {
		String debugStr = "DM Server return:"+ putMethod.getStatusCode();
		byte[] body;
		try {
			body = putMethod.getResponseBody();
			if (body != null) {
				debugStr += "\r\nBody:"+new String(body);
			}
		} catch (Exception e) {
			debugStr += e.getMessage();
		}
		
		throw new HitDMException(putMethod.getStatusCode(), debugStr);
	}
	
}
 
Example 8
Source File: DifidoClient.java    From difido-reports with Apache License 2.0 5 votes vote down vote up
public void updateMachine(int executionId, int machineId, MachineNode machine) throws Exception {
	PutMethod method = new PutMethod(baseUri + "executions/" + executionId + "/machines/" + machineId);
	method.setRequestHeader(new Header("Content-Type", "application/json"));
	final ObjectMapper mapper = new ObjectMapper();
	final String json = mapper.writeValueAsString(machine);
	final RequestEntity entity = new StringRequestEntity(json,"application/json","UTF-8");
	method.setRequestEntity(entity);
	int responseCode = client.executeMethod(method);
	handleResponseCode(method, responseCode);
}
 
Example 9
Source File: DifidoClient.java    From difido-reports with Apache License 2.0 5 votes vote down vote up
public void updateMachine(int executionId, int machineId, MachineNode machine) throws Exception {
	PutMethod method = new PutMethod(baseUri + "executions/" + executionId + "/machines/" + machineId);
	method.setRequestHeader(new Header("Content-Type", "application/json"));
	final ObjectMapper mapper = new ObjectMapper();
	final String json = mapper.writeValueAsString(machine);
	final RequestEntity entity = new StringRequestEntity(json,"application/json","UTF-8");
	method.setRequestEntity(entity);
	int responseCode = client.executeMethod(method);
	handleResponseCode(method, responseCode);
}
 
Example 10
Source File: ChatRoomDecorator.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void uploadFile(File file, UploadRequest response, ChatRoom room, Message.Type type)
{
    Log.warning("uploadFile request " + room.getRoomJid() + " " + response.putUrl);
    URLConnection urlconnection = null;

    try {
        PutMethod put = new PutMethod(response.putUrl);
        int port = put.getURI().getPort();
        if (port > 0)
        {
            Protocol.registerProtocol( "https", new Protocol( "https", new EasySSLProtocolSocketFactory(), port ) );
        }
        
        HttpClient client = new HttpClient();
        RequestEntity entity = new FileRequestEntity(file, "application/binary");
        put.setRequestEntity(entity);
        put.setRequestHeader("User-Agent", "Spark HttpFileUpload");
        client.executeMethod(put);

        int statusCode = put.getStatusCode();
        String responseBody = put.getResponseBodyAsString();

        Log.warning("uploadFile response " + statusCode + " " + responseBody);

        if ((statusCode >= 200) && (statusCode <= 202))
        {
            broadcastUploadUrl(room.getRoomJid(), response.getUrl, type);
        }

    } catch (Exception e) {
        Log.error("uploadFile error", e);
    }
}
 
Example 11
Source File: DifidoClient.java    From difido-reports with Apache License 2.0 4 votes vote down vote up
public void endExecution(int executionId) throws Exception {
	final PutMethod method = new PutMethod(baseUri + "executions/" + executionId + "?active=false");
	method.setRequestHeader(new Header("Content-Type", "text/plain"));
	final int responseCode = client.executeMethod(method);
	handleResponseCode(method, responseCode);
}
 
Example 12
Source File: DifidoClient.java    From difido-reports with Apache License 2.0 4 votes vote down vote up
public void endExecution(int executionId) throws Exception {
	final PutMethod method = new PutMethod(baseUri + "executions/" + executionId + "?active=false");
	method.setRequestHeader(new Header("Content-Type", "text/plain"));
	final int responseCode = client.executeMethod(method);
	handleResponseCode(method, responseCode);
}
 
Example 13
Source File: SystemRegistrationWorker.java    From olat with Apache License 2.0 4 votes vote down vote up
protected boolean doTheWork(String registrationData, String url, String version) {
    String registrationKey = propertyService.getStringProperty(PropertyLocator.SYSTEM_REG_SECRET_KEY);
    boolean regStatus = false;
    if (StringHelper.containsNonWhitespace(registrationData)) {
        // only send when there is something to send
        final HttpClient client = HttpClientFactory.getHttpClientInstance();
        client.getParams().setParameter("http.useragent", "OLAT Registration Agent ; " + version);

        log.info("URL:" + url, null);
        final PutMethod method = new PutMethod(url);
        if (registrationKey != null) {
            // updating
            method.setRequestHeader("Authorization", registrationKey);
            if (log.isDebugEnabled()) {
                log.debug("Authorization: " + registrationKey, null);
            } else {
                log.debug("Authorization: EXISTS", null);
            }
        } else {
            log.info("Authorization: NONE", null);
        }
        method.setRequestHeader("Content-Type", "application/xml; charset=utf-8");
        try {
            method.setRequestEntity(new StringRequestEntity(registrationData, "application/xml", "UTF8"));
            client.executeMethod(method);
            final int status = method.getStatusCode();
            if (status == HttpStatus.SC_NOT_MODIFIED || status == HttpStatus.SC_OK) {
                log.info("Successfully registered OLAT installation on olat.org server, thank you for your support!", null);
                registrationKey = method.getResponseBodyAsString();
                propertyService.setProperty(PropertyLocator.SYSTEM_REG_SECRET_KEY, registrationKey);
                regStatus = true;
            } else if (method.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                log.error("File could be created not on registration server::" + method.getStatusLine().toString(), null);
                regStatus = false;
            } else if (method.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
                log.info(method.getResponseBodyAsString() + method.getStatusText());
                regStatus = false;
            } else {
                log.error("Unexpected HTTP Status::" + method.getStatusLine().toString() + " during registration call", null);
                regStatus = false;
            }
        } catch (final Exception e) {
            log.error("Unexpected exception during registration call", e);
            regStatus = false;
        }
    } else {
        log.warn(
                "****************************************************************************************************************************************************************************",
                null);
        log.warn(
                "* This OLAT installation is not registered. Please, help us with your statistical data and register your installation under Adminisration - Systemregistration. THANK YOU! *",
                null);
        log.warn(
                "****************************************************************************************************************************************************************************",
                null);
    }
    return regStatus;
}
 
Example 14
Source File: SystemRegistrationWorker.java    From olat with Apache License 2.0 4 votes vote down vote up
protected boolean doTheWork(String registrationData, String url, String version) {
    String registrationKey = propertyService.getStringProperty(PropertyLocator.SYSTEM_REG_SECRET_KEY);
    boolean regStatus = false;
    if (StringHelper.containsNonWhitespace(registrationData)) {
        // only send when there is something to send
        final HttpClient client = HttpClientFactory.getHttpClientInstance();
        client.getParams().setParameter("http.useragent", "OLAT Registration Agent ; " + version);

        log.info("URL:" + url, null);
        final PutMethod method = new PutMethod(url);
        if (registrationKey != null) {
            // updating
            method.setRequestHeader("Authorization", registrationKey);
            if (log.isDebugEnabled()) {
                log.debug("Authorization: " + registrationKey, null);
            } else {
                log.debug("Authorization: EXISTS", null);
            }
        } else {
            log.info("Authorization: NONE", null);
        }
        method.setRequestHeader("Content-Type", "application/xml; charset=utf-8");
        try {
            method.setRequestEntity(new StringRequestEntity(registrationData, "application/xml", "UTF8"));
            client.executeMethod(method);
            final int status = method.getStatusCode();
            if (status == HttpStatus.SC_NOT_MODIFIED || status == HttpStatus.SC_OK) {
                log.info("Successfully registered OLAT installation on olat.org server, thank you for your support!", null);
                registrationKey = method.getResponseBodyAsString();
                propertyService.setProperty(PropertyLocator.SYSTEM_REG_SECRET_KEY, registrationKey);
                regStatus = true;
            } else if (method.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                log.error("File could be created not on registration server::" + method.getStatusLine().toString(), null);
                regStatus = false;
            } else if (method.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
                log.info(method.getResponseBodyAsString() + method.getStatusText());
                regStatus = false;
            } else {
                log.error("Unexpected HTTP Status::" + method.getStatusLine().toString() + " during registration call", null);
                regStatus = false;
            }
        } catch (final Exception e) {
            log.error("Unexpected exception during registration call", e);
            regStatus = false;
        }
    } else {
        log.warn(
                "****************************************************************************************************************************************************************************",
                null);
        log.warn(
                "* This OLAT installation is not registered. Please, help us with your statistical data and register your installation under Adminisration - Systemregistration. THANK YOU! *",
                null);
        log.warn(
                "****************************************************************************************************************************************************************************",
                null);
    }
    return regStatus;
}