Java Code Examples for org.apache.http.entity.BasicHttpEntity#setContentEncoding()

The following examples show how to use org.apache.http.entity.BasicHttpEntity#setContentEncoding() . 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: StoryWebServiceControllerTest.java    From ezScrum with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testAddExistedTask() throws Exception {
	// create 3 tasks in project
	TaskObject task1 = new TaskObject(mProject.getId());
	task1.setName("TASK_NAME1").save();
	
	TaskObject task2 = new TaskObject(mProject.getId());
	task2.setName("TASK_NAME2").save();
	
	TaskObject task3 = new TaskObject(mProject.getId());
	task3.setName("TASK_NAME3").save();
	
	// initial request data,add task#1 & #3 to story#1
	StoryObject story = mASTS.getStories().get(0);
	String taskIdJsonString = String.format("[%s, %s]", task1.getId(), task2.getId());
	String URL = String.format(API_URL, mProjectName, story.getId() + "/add-existed-task", mUsername, mPassword);
	BasicHttpEntity entity = new BasicHttpEntity();
	entity.setContent(new ByteArrayInputStream(taskIdJsonString.getBytes()));
	entity.setContentEncoding("utf-8");
	HttpPost httpPost = new HttpPost(URL);
	httpPost.setEntity(entity);
	mHttpClient.execute(httpPost);
	
	story.reload();
	assertEquals(2, story.getTasks().size());
}
 
Example 2
Source File: HurlStack.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 3
Source File: HurlStack.java    From volley_demo with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 4
Source File: StoryWebServiceControllerTest.java    From ezScrum with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testUpdateStory() throws Exception {
	StoryObject story = mASTS.getStories().get(0);
	// initial request data
	JSONObject storyJson = new JSONObject();
	storyJson.put("id", story.getId()).put("name", "顆顆").put("notes", "崩潰")
			.put("how_to_demo", "做不完").put("importance", 99)
			.put("value", 15).put("estimate", 21).put("status", 0)
			.put("sprint_id", -1).put("tags", "");
	String URL = String.format(API_URL, mProjectName, "update", mUsername, mPassword);
	BasicHttpEntity entity = new BasicHttpEntity();
	entity.setContent(new ByteArrayInputStream(storyJson.toString().getBytes()));
	entity.setContentEncoding("utf-8");
	HttpPut httpPut = new HttpPut(URL);
	httpPut.setEntity(entity);
	String result = EntityUtils.toString(mHttpClient.execute(httpPut).getEntity(), StandardCharsets.UTF_8);
	JSONObject response = new JSONObject(result);
	
	assertEquals(storyJson.getLong("id"), response.getLong("id"));
	assertEquals(storyJson.getString("name"), response.getString("name"));
	assertEquals(storyJson.getString("notes"), response.getString("notes"));
	assertEquals(storyJson.getString("how_to_demo"), response.getString("how_to_demo"));
	assertEquals(storyJson.getInt("importance"), response.getInt("importance"));
	assertEquals(storyJson.getInt("value"), response.getInt("value"));
	assertEquals(storyJson.getInt("estimate"), response.getInt("estimate"));
	assertEquals(storyJson.getInt("status"), response.getInt("status"));
	assertEquals(storyJson.getLong("sprint_id"), response.getLong("sprint_id"));
	assertEquals(9, response.getJSONArray("histories").length());
	assertEquals(0, response.getJSONArray("tags").length());
}
 
Example 5
Source File: HurlStack.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 6
Source File: HurlStack.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 7
Source File: HurlStack.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 8
Source File: HurlStack.java    From okulus with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 9
Source File: StoryWebServiceControllerTest.java    From ezScrum with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCreateStory() throws Exception {
	// 預設已經新增五個 stories
	ArrayList<StoryObject> stories = mProject.getStories();
	assertEquals(mStoryCount, stories.size());
	
	// initial request data
	JSONObject storyJson = new JSONObject();
	storyJson.put("name", "TEST_NAME").put("notes", "TEST_NOTES")
			.put("how_to_demo", "TEST_HOW_TO_DEMO").put("importance", 99)
			.put("value", 15).put("estimate", 21).put("status", 0)
			.put("sprint_id", -1).put("tags", "");
	String URL = String.format(API_URL, mProjectName, "create", mUsername, mPassword);
	BasicHttpEntity entity = new BasicHttpEntity();
	entity.setContent(new ByteArrayInputStream(storyJson.toString().getBytes()));
	entity.setContentEncoding("utf-8");
	HttpPost httpPost = new HttpPost(URL);
	httpPost.setEntity(entity);
	String result = EntityUtils.toString(mHttpClient.execute(httpPost).getEntity(), StandardCharsets.UTF_8);
	JSONObject response = new JSONObject(result);
	
	// 新增一個 story,project 內的 story 要有六個
	stories = mProject.getStories();
	assertEquals(mStoryCount + 1, stories.size());
	// 對回傳的 JSON 做 assert
	assertEquals("SUCCESS", response.getString("status"));
	assertEquals(stories.get(stories.size()-1).getId(), response.getLong("storyId"));
}
 
Example 10
Source File: TaskApiTest.java    From ezScrum with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPut() throws JSONException, ParseException, ClientProtocolException, IOException {
	TaskObject task = mATTS.getTasks().get(0);
	
	// test data
	String TEST_TASK_NAME = "TEST_TASK_NAME_NEW";
	String TEST_TASK_NOTES = "TEST_TASK_NOTES_NEW";
	long TEST_TASK_STORY_ID = -1;
	int TEST_TASK_ESTIMATE = 8;
	int TEST_TASK_REMAIN = 8;

	// initial request data
	JSONObject taskJson = new JSONObject();
	taskJson.put(TaskEnum.ID, task.getId())
	        .put(TaskEnum.NAME, TEST_TASK_NAME)
	        .put(TaskEnum.NOTES, TEST_TASK_NOTES)
	        .put(TaskEnum.ESTIMATE, TEST_TASK_ESTIMATE)
	        .put(TaskEnum.STORY_ID, TEST_TASK_STORY_ID)
	        .put(TaskEnum.REMAIN, TEST_TASK_REMAIN)
	        .put(TaskEnum.HANDLER_ID, mAccountId)
	        .put("project_name", mProject.getName());
	
	BasicHttpEntity entity = new BasicHttpEntity();
	entity.setContent(new ByteArrayInputStream(taskJson.toString().getBytes()));
	entity.setContentEncoding(StandardCharsets.UTF_8.name());
	HttpPut httpPut = new HttpPut(API_URL + "/" + task.getId());
	httpPut.setEntity(entity);
	setHeaders(httpPut, mAccountId, mPlatformType);
	String result = EntityUtils.toString(mClient.execute(httpPut).getEntity(), StandardCharsets.UTF_8);
	JSONObject response = new JSONObject(result);
	
	// assert
	assertEquals(TEST_TASK_NAME, response.getString(TaskEnum.NAME));
	assertEquals(TEST_TASK_NOTES, response.getString(TaskEnum.NOTES));
	assertEquals(TEST_TASK_ESTIMATE, response.getInt(TaskEnum.ESTIMATE));
	assertEquals(TEST_TASK_REMAIN, response.getInt(TaskEnum.REMAIN));
}
 
Example 11
Source File: HurlStack.java    From jus with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 12
Source File: StoryApiTest.java    From ezScrum with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPut() throws JSONException, ParseException,
		ClientProtocolException, IOException {
	StoryObject story = mCPB.getStories().get(0);
	// initial request data
	JSONObject storyJson = new JSONObject();
	storyJson.put("id", story.getId()).put("name", "顆顆").put("notes", "崩潰")
			.put("how_to_demo", "做不完").put("importance", 99)
			.put("value", 15).put("estimate", 21).put("status", 0)
			.put("sprint_id", -1).put("tags", "")
			.put("project_name", mProject.getName());

	BasicHttpEntity entity = new BasicHttpEntity();
	entity.setContent(new ByteArrayInputStream(storyJson.toString()
			.getBytes()));
	entity.setContentEncoding("utf-8");
	HttpPut httpPut = new HttpPut(API_URL + "/" + story.getId());
	httpPut.setEntity(entity);
	setHeaders(httpPut, mAccountId, mPlatformType);
	HttpResponse httpResponse = mClient.execute(httpPut);
	String result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
	
	System.out.println(result);
	JSONObject response = new JSONObject(result);

	assertEquals(storyJson.getLong("id"), response.getLong("id"));
	assertEquals(storyJson.getString("name"), response.getString("name"));
	assertEquals(storyJson.getString("notes"), response.getString("notes"));
	assertEquals(storyJson.getString("how_to_demo"),
			response.getString("how_to_demo"));
	assertEquals(storyJson.getInt("importance"),
			response.getInt("importance"));
	assertEquals(storyJson.getInt("value"), response.getInt("value"));
	assertEquals(storyJson.getInt("estimate"), response.getInt("estimate"));
	assertEquals(storyJson.getInt("status"), response.getInt("status"));
	assertEquals(storyJson.getLong("sprint_id"),
			response.getLong("sprint_id"));
	assertEquals(7, response.getJSONArray("histories").length());
	assertEquals(0, response.getJSONArray("tags").length());
}
 
Example 13
Source File: HurlStack.java    From FeedListViewDemo with MIT License 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 14
Source File: OkHttpStack.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
private static HttpEntity entityFromOkHttpResponse(com.squareup.okhttp.Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 15
Source File: OkHttpStack.java    From openshop.io-android with MIT License 5 votes vote down vote up
private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 16
Source File: ProductBacklogWebServiceControllerTest.java    From ezScrum with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCreateStory() throws Exception {
	JSONObject storyJson = new JSONObject();
	storyJson
		.put("name", "TEST_STORY")
		.put("importance", 100)
		.put("estimate", 2)
		.put("value", 50)
		.put("how_to_demo", "TEST_STORY_DEMO")
		.put("notes", "TEST_STORY_NOTE")
		.put("status", 1)
		.put("sprint_id", -1)
		.put("tags", "");
	
	// send request to create a story
	String URL = String.format(API_URL, mProjectName, "create", mUsername, mPassword);
	HttpPost httpPost = new HttpPost(URL);
	BasicHttpEntity entity = new BasicHttpEntity();
	entity.setContent(new ByteArrayInputStream(storyJson.toString().getBytes(StandardCharsets.UTF_8)));
	entity.setContentEncoding("utf-8");
	httpPost.setEntity(entity);
	HttpResponse httpResponse = mHttpClient.execute(httpPost);
	String response = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
	
	// assert result
	ProductBacklogHelper productBacklogHelper = new ProductBacklogHelper(mProject);
	ArrayList<StoryObject> stories = productBacklogHelper.getStories();
	assertEquals(4, stories.size());
	
	JSONObject responseJson = new JSONObject(response);
	assertEquals("SUCCESS", responseJson.getString("status"));
	assertEquals(stories.get(stories.size() - 1).getId(), responseJson.getLong("storyId"));
}
 
Example 17
Source File: OkHttpURLConnectionStack.java    From AndroidProjects with MIT License 5 votes vote down vote up
private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 18
Source File: HurlStack.java    From pearl with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 19
Source File: HurlStack.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 20
Source File: SprintPlanWebServiceControllerTest.java    From ezScrum with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCreateSprint() throws Exception {
	// get existed sprints
	ArrayList<SprintObject> sprints = mProject.getSprints();
	// assert
	assertEquals(mSprintCount, sprints.size());
	
	// Test Data
	int interval = 2;
	int members = 4;
	int availableHours = 100;
	int focusFactor = 50;
	String sprintGoal = "TEST_SPRINT_GOAL";
	String startDate = "2015/07/01";
	String demoDate = "2015/07/15";
	String dueDate = "2015/07/15";
	String demoPlace = "Lab1321";
	String dailyInfo = "TEST_DAILY_INFO";
	
	// prepare request data
	JSONObject sprintJson = new JSONObject();
	sprintJson.put(SprintEnum.INTERVAL, interval)
	          .put(SprintEnum.TEAM_SIZE, members)
	          .put(SprintEnum.AVAILABLE_HOURS, availableHours)
	          .put(SprintEnum.FOCUS_FACTOR, focusFactor)
	          .put(SprintEnum.GOAL, sprintGoal)
	          .put(SprintEnum.START_DATE, startDate)
	          .put(SprintEnum.DEMO_DATE, demoDate)
	          .put(SprintEnum.DEMO_PLACE, demoPlace)
	          .put(SprintEnum.DAILY_INFO, dailyInfo)
	          .put(SprintEnum.DUE_DATE, dueDate);
	
	// Assemble URL
	String URL = String.format(API_URL, mProjectName, "create", mUsername, mPassword);
	
	// Send Http Request
	BasicHttpEntity entity = new BasicHttpEntity();
	entity.setContent(new ByteArrayInputStream(sprintJson.toString().getBytes()));
	entity.setContentEncoding(StandardCharsets.UTF_8.name());
	HttpPost httpPost = new HttpPost(URL);
	httpPost.setEntity(entity);
	String result = EntityUtils.toString(mHttpClient.execute(httpPost).getEntity(), StandardCharsets.UTF_8);
	String expectedJsonString = ConvertSprintBacklog.getSprintBacklogJsonString(mProject.getCurrentSprint());
	// assert
	assertEquals(expectedJsonString, result);
}