// Path: frostillicus-blog/frostillicus-blog-shared/src/main/java/bean/UserInfoBean.java
// @RequestScoped
// @Named("userInfo")
// public class UserInfoBean {
// 	public static final String ROLE_ADMIN = "admin"; //$NON-NLS-1$
// 
// 	@Inject @Named("darwinoContext")
// 	DarwinoContext context;
// 
// 	@SneakyThrows
// 	public String getImageUrl(final String userName) {
// 		String md5 = StringUtil.md5Hex(StringUtil.toString(userName).toLowerCase());
// 		return StringUtil.format(DarwinoHttpConstants.SOCIAL_USERS_PATH + "/users/{0}/content/photo", URLEncoder.encode(md5, "UTF-8")); //$NON-NLS-1$ //$NON-NLS-2$
// 	}
// 
// 	public boolean isAdmin() {
// 		return context.getUser().hasRole(ROLE_ADMIN);
// 	}
// 
// 	public boolean isAnonymous() {
// 		return context.getUser().isAnonymous();
// 	}
// 
// 	public String getCn() {
// 		return context.getUser().getCn();
// 	}
// 
// 	public String getDn() {
// 		return context.getUser().getDn();
// 	}
// 
// 	public String getEmailAddress() throws UserException {
// 		Object mail = context.getUser().getAttribute(User.ATTR_EMAIL);
// 		return StringUtil.toString(mail);
// 	}
// }
// 
// Path: frostillicus-blog/frostillicus-blog-shared/src/main/java/model/Media.java
// @Entity @Data @NoArgsConstructor
// public class Media {
// 	@Id @Column private String id;
// 	@Column private String name;
// 	@Column(EntityConverter.ATTACHMENT_FIELD) private List<EntityAttachment> attachments;
// 	@Column(Document.SYSTEM_META_MDATE) private Date lastModificationDate;
// 	@Column(Document.SYSTEM_META_CUSER) private String creationUser;
// 	@Column private boolean isConflict;
// }
// 
// Path: frostillicus-blog/frostillicus-blog-shared/src/main/java/model/MediaRepository.java
// @RepositoryProvider(AppDatabaseDef.STORE_MEDIA)
// public interface MediaRepository extends DarwinoRepository<Media, String> {
// 	Optional<Media> findByName(String name);
// 
// 	Stream<Media> findAll();
// }


import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.ResourceBundle;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.eclipse.jnosql.diana.driver.attachment.EntityAttachment;
import org.w3c.dom.Element;
import com.darwino.commons.util.DateTimeISO8601;
import com.darwino.commons.util.PathUtil;
import com.darwino.commons.xml.DomUtil;
import bean.UserInfoBean;
import lombok.SneakyThrows;
import model.Media;
import model.MediaRepository;
    @Context
    HttpServletRequest request;

    @Inject
    MediaRepository mediaRepository;

    @GET
    @Produces("application/atom+xml")
    public String list() {
        var xml = DomUtil.createDocument();
        var feed = DomUtil.createRootElement(xml, "feed"); //$NON-NLS-1$
        feed.setAttribute("xmlns", "http://www.w3.org/2005/Atom"); //$NON-NLS-1$ //$NON-NLS-2$

        mediaRepository.findAll().forEach(m -> {
        	var entry = DomUtil.createElement(feed, "entry"); //$NON-NLS-1$
            entry.setAttribute("xmlns", "http://www.w3.org/2005/Atom"); //$NON-NLS-1$ //$NON-NLS-2$
            populateAtomXml(entry, m);
        });

        return DomUtil.getXMLString(xml);
    }

    @POST
    @Produces("application/atom+xml")
    public Response uploadMedia(final byte[] data) throws URISyntaxException {
        var contentType = request.getContentType();
        var name = request.getHeader("Slug"); //$NON-NLS-1$

        // TODO make sure it's not already there
        // This could use the name as the UNID, but it's kind of nice having a "real" UNID behind the scenes
// Path: frostillicus-blog/frostillicus-blog-shared/src/main/java/bean/UserInfoBean.java
// @RequestScoped
// @Named("userInfo")
// public class UserInfoBean {
// 	public static final String ROLE_ADMIN = "admin"; //$NON-NLS-1$
// 
// 	@Inject @Named("darwinoContext")
// 	DarwinoContext context;
// 
// 	@SneakyThrows
// 	public String getImageUrl(final String userName) {
// 		String md5 = StringUtil.md5Hex(StringUtil.toString(userName).toLowerCase());
// 		return StringUtil.format(DarwinoHttpConstants.SOCIAL_USERS_PATH + "/users/{0}/content/photo", URLEncoder.encode(md5, "UTF-8")); //$NON-NLS-1$ //$NON-NLS-2$
// 	}
// 
// 	public boolean isAdmin() {
// 		return context.getUser().hasRole(ROLE_ADMIN);
// 	}
// 
// 	public boolean isAnonymous() {
// 		return context.getUser().isAnonymous();
// 	}
// 
// 	public String getCn() {
// 		return context.getUser().getCn();
// 	}
// 
// 	public String getDn() {
// 		return context.getUser().getDn();
// 	}
// 
// 	public String getEmailAddress() throws UserException {
// 		Object mail = context.getUser().getAttribute(User.ATTR_EMAIL);
// 		return StringUtil.toString(mail);
// 	}
// }
// 
// Path: frostillicus-blog/frostillicus-blog-shared/src/main/java/model/Media.java
// @Entity @Data @NoArgsConstructor
// public class Media {
// 	@Id @Column private String id;
// 	@Column private String name;
// 	@Column(EntityConverter.ATTACHMENT_FIELD) private List<EntityAttachment> attachments;
// 	@Column(Document.SYSTEM_META_MDATE) private Date lastModificationDate;
// 	@Column(Document.SYSTEM_META_CUSER) private String creationUser;
// 	@Column private boolean isConflict;
// }
// 
// Path: frostillicus-blog/frostillicus-blog-shared/src/main/java/model/MediaRepository.java
// @RepositoryProvider(AppDatabaseDef.STORE_MEDIA)
// public interface MediaRepository extends DarwinoRepository<Media, String> {
// 	Optional<Media> findByName(String name);
// 
// 	Stream<Media> findAll();
// }


// Path: frostillicus-blog/frostillicus-blog-j2ee/src/main/java/api/atompub/MediaResource.java
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.ResourceBundle;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.eclipse.jnosql.diana.driver.attachment.EntityAttachment;
import org.w3c.dom.Element;
import com.darwino.commons.util.DateTimeISO8601;
import com.darwino.commons.util.PathUtil;
import com.darwino.commons.xml.DomUtil;
import bean.UserInfoBean;
import lombok.SneakyThrows;
import model.Media;
import model.MediaRepository;

    @Context
    HttpServletRequest request;

    @Inject
    MediaRepository mediaRepository;

    @GET
    @Produces("application/atom+xml")
    public String list() {
        var xml = DomUtil.createDocument();
        var feed = DomUtil.createRootElement(xml, "feed"); //$NON-NLS-1$
        feed.setAttribute("xmlns", "http://www.w3.org/2005/Atom"); //$NON-NLS-1$ //$NON-NLS-2$

        mediaRepository.findAll().forEach(m -> {
        	var entry = DomUtil.createElement(feed, "entry"); //$NON-NLS-1$
            entry.setAttribute("xmlns", "http://www.w3.org/2005/Atom"); //$NON-NLS-1$ //$NON-NLS-2$
            populateAtomXml(entry, m);
        });

        return DomUtil.getXMLString(xml);
    }

    @POST
    @Produces("application/atom+xml")
    public Response uploadMedia(final byte[] data) throws URISyntaxException {
        var contentType = request.getContentType();
        var name = request.getHeader("Slug"); //$NON-NLS-1$

        // TODO make sure it's not already there
        // This could use the name as the UNID, but it's kind of nice having a "real" UNID behind the scenes
        var media = mediaRepository.findByName(name).orElseGet(Media::new);