com.lyncode.test.http.method.PutMethodBuilder Maven / Gradle / Ivy
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lyncode.test.http.method;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
public class PutMethodBuilder extends MethodBuilder {
private String body = null;
private ContentType contentType;
public static PutMethodBuilder put (String relativeUrl) {
return new PutMethodBuilder(relativeUrl);
}
public PutMethodBuilder(String relativeUrl) {
super(relativeUrl);
}
public PutMethodBuilder withBody (String body, ContentType contentType) {
this.body = body;
this.contentType = contentType;
return with(new BasicHeader("content-type", contentType.toString()));
}
@Override
HttpPut build(String url) {
HttpPut httpPut = new HttpPut(url);
if (body != null) {
httpPut.setEntity(new ByteArrayEntity(body.getBytes(), contentType));
}
return httpPut;
}
}