/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.lofidewanto.demo.server.service.content;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.lofidewanto.demo.server.domain.Attachment;
import com.lofidewanto.demo.shared.DemoGwtServiceEndpoint;

@Service
public class ConfluenceContentServiceImpl implements ConfluenceContentService {

	private static final Logger logger = LoggerFactory
			.getLogger(ConfluenceContentServiceImpl.class);

	private static final String ATTACHMENT_PAGE_ID_PARAM = "{pageId}";

	@Autowired
	private RestTemplate restTemplate;

	@Value("${confluence.url}")
	private String confluenceUrl;

	@Value("${confluence.pageid}")
	private String confluencePageId;

	@Override
	public List<Attachment> getAllAttachments() {
		// Connect to Confluence
		// TODO Handle error

		// TODO Replace {pageId} with confluencePageId
		String confluenceAttachmentList = DemoGwtServiceEndpoint.CONFLUENCE_ATTACHMENT_LIST;

		List<Attachment> attachments = restTemplate
				.getForObject(confluenceUrl + confluenceAttachmentList, List.class);

		return attachments;
	}

	@Override
	public List<Attachment> getAllAttachmentsByPageId(String pageId) {
		return null;
	}

	@Override
	public Attachment getAttachmentById() {
		return null;
	}

}

@@ -850,24 +850,49 @@
 e.content;%0A%0A
+import java.util.Arrays;%0A
 import java.
@@ -1082,16 +1082,240 @@
 .Value;%0A
+import org.springframework.http.HttpEntity;%0Aimport org.springframework.http.HttpHeaders;%0Aimport org.springframework.http.HttpMethod;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.http.ResponseEntity;%0A
 import o
@@ -1404,16 +1404,74 @@
 emplate;
+%0Aimport org.springframework.web.util.UriComponentsBuilder;
 %0A%0Aimport
@@ -2131,57 +2131,8 @@
 or%0A%0A
-%09%09// TODO Replace %7BpageId%7D with confluencePageId%0A
 %09%09St
@@ -2218,15 +2218,405 @@
 ST;%0A
-%0A%09%09List
+%09%09String url = confluenceUrl.concat(confluenceAttachmentList);%0A%0A%09%09// Replace %7BpageId%7D with confluencePageId%0A%09%09UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)%0A%09%09%09%09.queryParam(%22pageId%22, confluencePageId);%0A%0A%09%09HttpHeaders headers = new HttpHeaders();%0A%09%09headers.set(%22Accept%22, MediaType.APPLICATION_JSON_VALUE);%0A%0A%09%09HttpEntity%3C?%3E entity = new HttpEntity%3C%3E(headers);%0A%0A%09%09ResponseEntity
 %3CAtt
@@ -2622,16 +2622,18 @@
 tachment
+%5B%5D
 %3E attach
@@ -2637,18 +2637,37 @@
 achments
+WithResponseEntity
  =
+ 
  restTem
@@ -2675,92 +2675,177 @@
 late
-%0A%09%09%09%09.getForObject(confluenceUrl + confluenceAttachmentList, List.class);%0A%0A%09%09return 
+.exchange(confluenceUrl,%0A%09%09%09%09HttpMethod.GET, entity, Attachment%5B%5D.class);%0A%0A%09%09Attachment%5B%5D attachments = attachmentsWithResponseEntity.getBody();%0A%0A%09%09return Arrays.asList(
 atta
@@ -2851,16 +2851,17 @@
 achments
+)
 ;%0A%09%7D%0A%0A%09@

implement service