Changeset 2268
- Timestamp:
- 09/28/12 16:32:49 (12 years ago)
- Location:
- ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/RssCreator.java
r2259 r2268 1 1 package clarin.cmdi.componentregistry; 2 2 3 import clarin.cmdi.componentregistry.model.AbstractDescription; 3 4 import clarin.cmdi.componentregistry.rss.Category; 4 5 import clarin.cmdi.componentregistry.rss.Cloud; … … 16 17 import java.text.SimpleDateFormat; 17 18 import java.util.ArrayList; 19 import java.util.Comparator; 18 20 import java.util.Date; 19 21 import java.util.List; … … 26 28 public abstract class RssCreator<T> { // extends nothing so far, throuw nothing // make it abstract 27 29 28 29 protected String uriForGuid; // MUST 30 31 // all the fields below are not mandatory, no crashes will happen if they are not set 30 32 31 private String title; 33 32 private String link; … … 50 49 private SkipDaysList skipDays; 51 50 private double version = 2.0; // of an rss 52 53 //////////////////// 54 55 public RssCreator(String uri){ 56 this.uriForGuid = uri; 57 } 58 51 59 52 60 53 … … 81 74 } 82 75 83 public String getTitle() {76 public String getTitle() { 84 77 return this.title; 85 78 } 86 79 87 80 /** 88 81 * Sets the value of the link property. … … 95 88 } 96 89 97 public String getLink() {90 public String getLink() { 98 91 return this.link; 99 92 } 100 93 101 94 /** 102 95 * Sets the value of the description property. … … 329 322 return (makeRssChannel(makeListOfRssItems(objs))); 330 323 } 331 332 333 protected String getRFCDateTime(String datestring) throws ParseException { 334 335 Date date = DatatypeConverter.parseDateTime(datestring).getTime(); 336 SimpleDateFormat RFC822DATEFORMAT = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z"); 337 return RFC822DATEFORMAT.format(date); 338 } 339 340 341 protected Guid makeGuid(String href){ 324 325 // Helping stuff 326 protected Guid makeGuid(String href) { 342 327 Guid result = new Guid(); 343 328 // result.setIsPermaLink(null); … … 345 330 return result; 346 331 } 332 333 /// Helping stuff: for working with dates 334 private static Date parseWorks(String dateString) { 335 Date d = null; 336 try { 337 d = AbstractDescription.getDate(dateString); 338 } catch (ParseException pe) { 339 return null; 340 }; 341 return d; 342 } 343 344 protected String getRFCDateTime(String dateString) { 345 346 Date date = parseWorks(dateString); 347 348 if (date == null) { 349 return dateString; 350 } else { 351 SimpleDateFormat RFC822DATEFORMAT = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z"); 352 return RFC822DATEFORMAT.format(date); 353 } 354 } 355 347 356 357 /////// 358 // returns 1 if ds1 is older (before) than ds2, returns -1 if ds1 is younger (after) than ds2 359 public static int compareDateStrings(String ds1, String ds2) { 360 361 int result = 0; 362 363 Date d1 = parseWorks(ds1); 364 Date d2 = parseWorks(ds2); 365 366 if (d1 == null) { 367 if (d2 == null) { 368 result = 0; 369 } else { 370 result = 1; 371 } 372 } else { 373 if (d2 == null) { 374 result = -1; 375 } else { 376 // cpmparteTo reurns: 377 // a value less than 0 if d2 is before d1 (d1 is younger than d2); 378 // a value greater than 0 if d2 is after d1 (d1 is older than d2) 379 result = d2.compareTo(d1); 380 } 381 382 } 383 return result; 384 } 348 385 } -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/RssCreatorComments.java
r2256 r2268 16 16 public class RssCreatorComments extends RssCreator<Comment> { 17 17 18 19 private boolean isFromProfile; 20 21 public RssCreatorComments(String uri){ 22 super(uri); 23 } 24 18 25 19 // creator method, comment to rssItem, ovverrides the dummy method of the RssCreator class 26 20 // ?? is there a better way than boolean flag to arrange switch beween comment for profiles and commentss for components 27 21 28 public void setFlagIsFromProfile(boolean isFromProfile) { 29 this.isFromProfile = isFromProfile; 30 } 22 31 23 32 24 @Override … … 35 27 36 28 RssItem retval = new RssItem(); 37 String hrefPostfix = "&view=comments&commentId=";38 String hrefInfix = "?item=";39 29 40 String descId;41 if (isFromProfile) {descId=comm.getProfileDescriptionId();}42 else{descId=comm.getComponentDescriptionId();};43 44 45 String link = uriForGuid+hrefInfix+descId+hrefPostfix+comm.getId();46 30 47 31 //The content … … 50 34 51 35 //Guid 52 retval.setGuid(makeGuid(link));36 //retval.setGuid(makeGuid(link)); 53 37 54 38 // link 55 retval.setLink( link);39 retval.setLink(super.getLink()+"&commentId="+comm.getId()+"#"); 56 40 57 41 … … 71 55 protected String makeCommentTitle(String commentId, String user){ 72 56 73 return("The comment "+commentId +" by "+user );57 return("The comment "+commentId +" by "+user+" "); 74 58 75 59 } -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/RssCreatorDescriptions.java
r2256 r2268 14 14 public class RssCreatorDescriptions<T extends AbstractDescription> extends RssCreator<T> { 15 15 16 public RssCreatorDescriptions(String uri){ 17 super(uri); 18 } 19 16 20 17 // creator method, desc to rssItem, ovverrides the dummy method of the RssCreatorClass 21 18 @Override … … 25 22 26 23 RssItem retval = new RssItem(); 27 String hrefInfix = "?item="; 28 String link = uriForGuid+hrefInfix+desc.getId(); 29 24 30 25 //Description (blah-blah) 31 26 retval.setDescription(desc.getDescription()); … … 34 29 35 30 //Guid 36 retval.setGuid(makeGuid(link));31 //retval.setGuid(makeGuid(link)); 37 32 38 33 // link 39 retval.setLink( link);34 retval.setLink(super.getLink()+"?item="+desc.getId()+"#"); 40 35 41 36 //time-date … … 51 46 52 47 protected String makeDescriptionTitle(String name, String creatorname, String group, String domain){ 53 String retval = name+"by user "+creatorname+", group "+group+",domain "+domain;48 String retval = name+"by user "+creatorname+", group "+group+",domain "+domain; 54 49 55 50 return retval; -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/AbstractDescription.java
r2259 r2268 1 1 package clarin.cmdi.componentregistry.model; 2 2 3 import java.text.DateFormat;3 import clarin.cmdi.componentregistry.RssCreator; 4 4 import java.text.ParseException; 5 5 import java.util.Comparator; 6 6 import java.util.Date; 7 import javax.xml.bind.DatatypeConverter;8 7 9 8 import javax.xml.bind.annotation.XmlAccessType; … … 16 15 import org.apache.commons.lang.time.DateFormatUtils; 17 16 import org.apache.commons.lang.time.DateUtils; 17 import org.slf4j.Logger; 18 import org.slf4j.LoggerFactory; 18 19 19 20 @XmlRootElement(name = "description") … … 33 34 private String groupName; 34 35 private int commentsCount; 36 private final static Logger LOG = LoggerFactory.getLogger(AbstractDescription.class); 35 37 36 38 public void setId(String id) { 37 39 this.id = id; 38 40 } 39 41 40 42 public String getId() { 41 43 return id; 42 44 } 43 45 44 46 public void setDescription(String description) { 45 47 this.description = description; 46 48 } 47 49 48 50 public String getDescription() { 49 51 return description; 50 52 } 51 53 52 54 public void setName(String name) { 53 55 this.name = name; 54 56 } 55 57 56 58 public String getName() { 57 59 return name; 58 60 } 59 61 60 62 public void setRegistrationDate(String registrationDate) { 61 63 this.registrationDate = registrationDate; 62 64 } 63 65 64 66 public String getRegistrationDate() { 65 67 return registrationDate; 66 68 } 67 69 68 70 public void setCreatorName(String creatorName) { 69 71 this.creatorName = creatorName; 70 72 } 71 73 72 74 public String getCreatorName() { 73 75 return creatorName; 74 76 } 75 77 76 78 public void setUserId(String userId) { 77 79 this.userId = userId; 78 80 } 79 81 80 82 /** 81 * MD5 string representation of the user id. Storing the hash because the userId can be the email address which we don't want to make82 * public.83 * MD5 string representation of the user id. Storing the hash because the 84 * userId can be the email address which we don't want to make public. 83 85 */ 84 86 public String getUserId() { 85 87 return userId; 86 88 } 87 89 88 90 public void setDomainName(String domainName) { 89 91 this.domainName = domainName; 90 92 } 91 93 92 94 public String getDomainName() { 93 95 return domainName; 94 96 } 95 97 96 98 public void setHref(String href) { 97 99 this.href = href; 98 100 } 99 101 100 102 public String getHref() { 101 103 return href; 102 104 } 103 105 104 106 public void setGroupName(String groupName) { 105 107 this.groupName = groupName; 106 108 } 107 109 108 110 public String getGroupName() { 109 111 return groupName; 110 112 } 111 113 … … 114 116 */ 115 117 public int getCommentsCount() { 116 118 return commentsCount; 117 119 } 118 120 … … 121 123 */ 122 124 public void setCommentsCount(int commentsCount) { 123 125 this.commentsCount = commentsCount; 124 126 } 125 127 126 128 @Override 127 129 public String toString() { 128 130 return "Name=" + getName() + ", id=" + getId() + ", creatorName=" + getCreatorName() + ", userId=" + getUserId(); 129 131 } 130 132 131 133 public boolean isProfile() { 132 134 return this instanceof ProfileDescription; 133 135 } 134 136 135 137 public String getType() { 136 138 return isProfile() ? "profile" : "component"; 137 139 } 138 140 139 141 public static String createNewDate() { 140 142 return createNewDate(new Date().getTime()); 141 143 } 142 144 143 145 public static String createNewDate(long time) { 144 146 return DateFormatUtils.formatUTC(time, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); 145 147 } 146 148 147 149 /** 148 150 * Helper method. 149 * @param userId normal string which will be checked to see if it matches the md5 hash of the stored userId 151 * 152 * @param userId normal string which will be checked to see if it matches 153 * the md5 hash of the stored userId 150 154 */ 151 155 public boolean isThisTheOwner(String userId) { 152 153 156 String userHash = DigestUtils.md5Hex(userId); 157 return userHash.equals(getUserId()); 154 158 } 155 159 156 160 public static Date getDate(String registrationDate) throws ParseException { 157 161 return DateUtils.parseDate(registrationDate, new String[]{DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()}); 158 162 } 159 163 160 164 public static String createPublicHref(String href) { 161 162 163 164 165 166 167 168 165 String result = href; 166 if (href != null) { 167 int index = href.indexOf("?"); 168 if (index != -1) { //strip off query params the rest should be the public href. 169 result = href.substring(0, index); 170 } 171 } 172 return result; 169 173 } 170 171 174 public static final Comparator<? super AbstractDescription> COMPARE_ON_NAME = new Comparator<AbstractDescription>() { 172 173 public int compare(AbstractDescription o1, AbstractDescription o2) { 174 int result = 0; 175 if (o1.getName() != null && o2.getName() != null) { 176 result = o1.getName().compareToIgnoreCase(o2.getName()); 177 } 178 if (o1.getId() != null && result == 0) { 179 result = o1.getId().compareTo(o2.getId()); 180 } 181 return result; 182 } 183 }; 184 185 public static final Comparator<? super AbstractDescription> COMPARE_ON_DATE = new Comparator<AbstractDescription>() { 186 187 //compare two components by the date of registration 188 // the dates, which are strings accrording to ISO 8601, compared lexicographically 189 @Override 175 @Override 190 176 public int compare(AbstractDescription o1, AbstractDescription o2) { 191 192 177 int result = 0; 193 194 Date d1 = DatatypeConverter.parseDateTime(o1.getRegistrationDate()).getTime();195 Date d2 = DatatypeConverter.parseDateTime(o2.getRegistrationDate()).getTime();196 197 result = d2.compareTo(d1);198 178 if (o1.getName() != null && o2.getName() != null) { 179 result = o1.getName().compareToIgnoreCase(o2.getName()); 180 } 181 if (o1.getId() != null && result == 0) { 182 result = o1.getId().compareTo(o2.getId()); 183 } 199 184 return result; 200 185 } 201 186 }; 202 187 public static final Comparator<? super AbstractDescription> COMPARE_ON_DATE = new Comparator<AbstractDescription>() { 188 //compare two components by the date of registration 189 // returns 1 if o11 is older than o2, returns -1 if o1 is younger than o2 190 @Override 191 public int compare(AbstractDescription o1, AbstractDescription o2) { 192 193 return (RssCreator.compareDateStrings(o1.getRegistrationDate(), o2.getRegistrationDate())); 194 } 195 }; 203 196 } -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/Comment.java
r2259 r2268 1 1 package clarin.cmdi.componentregistry.model; 2 2 3 import clarin.cmdi.componentregistry.RssCreator; 3 4 import java.text.DateFormat; 4 5 import java.text.ParseException; … … 12 13 import org.apache.commons.lang.time.DateFormatUtils; 13 14 import org.apache.commons.lang.time.DateUtils; 15 import org.slf4j.Logger; 16 import org.slf4j.LoggerFactory; 14 17 15 18 /** … … 31 34 @XmlTransient // this prevents userId from being serialized to XML and thus exposed (which is useless and undesirable) 32 35 private String userId; 36 private final static Logger LOG = LoggerFactory.getLogger(Comment.class); 33 37 34 38 public void setComment(String comment) { 35 39 this.comments = comment; 36 40 } 37 41 38 42 public String getComment() { 39 43 return comments; 40 44 } 41 45 42 46 public void setCommentDate(String commentDate) { 43 47 this.commentDate = commentDate; 44 48 } 45 49 46 50 public String getCommentDate() { 47 51 return commentDate; 48 52 } 49 53 50 54 public void setId(String commentId) { 51 55 this.id = commentId; 52 56 } 53 57 54 58 public String getId() { 55 59 return id; 56 60 } 57 61 58 62 public String getComponentDescriptionId() { 59 63 return componentDescriptionId; 60 64 } 61 65 62 66 public void setComponentDescriptionId(String ComponentDescriptionId) { 63 67 this.componentDescriptionId = ComponentDescriptionId; 64 68 } 65 69 66 70 public void setProfileDescriptionId(String profileDescriptionId) { 67 71 this.profileDescriptionId = profileDescriptionId; 68 72 } 69 73 70 74 public String getProfileDescriptionId() { 71 75 return profileDescriptionId; 72 76 } 73 77 74 78 public void setUserId(String userId) { 75 79 this.userId = userId; 76 80 } 77 81 78 82 public String getUserId() { 79 83 return userId; 80 84 } 81 85 … … 85 89 */ 86 90 public String getUserName() { 87 91 return userName; 88 92 } 89 93 … … 92 96 */ 93 97 public void setUserName(String userName) { 94 98 this.userName = userName; 95 99 } 96 100 … … 99 103 */ 100 104 public boolean isCanDelete() { 101 105 return canDelete; 102 106 } 103 107 … … 106 110 */ 107 111 public void setCanDelete(boolean canDelete) { 108 112 this.canDelete = canDelete; 109 113 } 110 114 111 115 public static Date getDate(String registrationDate) throws ParseException { 112 116 return DateUtils.parseDate(registrationDate, new String[]{DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()}); 113 117 } 114 118 115 116 117 119 /* 118 120 * Helper method to set the Date 119 121 */ 120 122 public static String createNewDate() { 121 123 return createNewDate(new Date().getTime()); 122 124 } 123 125 … … 127 129 */ 128 130 public static String createNewDate(long time) { 129 131 return DateFormatUtils.formatUTC(time, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); 130 132 } 131 133 132 134 public static Comment createANewComment() { 133 134 135 135 Comment com = new Comment(); 136 com.setCommentDate(createNewDate()); 137 return com; 136 138 } 137 139 138 139 140 public static final Comparator<Comment> COMPARE_ON_DATE = new Comparator<Comment>() { 140 141 141 //compare two commments by the date of registration 142 // the dates, which are strings accrording to ISO 8601, compared lexicographically 143 @Override 144 public int compare(Comment o1, Comment o2) { 145 146 int result = 0; 147 148 Date d1 = DatatypeConverter.parseDateTime(o1.getCommentDate()).getTime(); 149 Date d2 = DatatypeConverter.parseDateTime(o2.getCommentDate()).getTime(); 150 151 result = d2.compareTo(d1); 152 153 return result; 142 // returns 1 if o11 is older than o2, returns -1 if o1 is younger than o2 143 @Override 144 public int compare(Comment o1, Comment o2) { 145 146 return (RssCreator.compareDateStrings(o1.getCommentDate(), o2.getCommentDate())); 154 147 } 155 148 }; -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r2259 r2268 35 35 import java.util.Arrays; 36 36 import java.util.Collections; 37 import java.util.Date; 37 38 import java.util.List; 38 39 import javax.servlet.ServletContext; … … 56 57 import javax.ws.rs.core.StreamingOutput; 57 58 import javax.ws.rs.core.UriInfo; 59 import javax.xml.bind.DatatypeConverter; 58 60 import javax.xml.bind.JAXBException; 59 61 import org.slf4j.Logger; … … 87 89 * Converts userspace boolean to component status. Temporary solution!!! 88 90 * 89 * TODO: Replace all calls to getRegistry that use this by calls using ComponentStatus 91 * TODO: Replace all calls to getRegistry that use this by calls using 92 * ComponentStatus 90 93 * 91 94 * @param userSpace 92 95 * @return 93 * @deprecated All calls should go directly to {@link #getRegistry(clarin.cmdi.componentregistry.ComponentStatus)} 96 * @deprecated All calls should go directly to 97 * {@link #getRegistry(clarin.cmdi.componentregistry.ComponentStatus)} 94 98 */ 95 99 @Deprecated 96 100 private static ComponentStatus getStatus(boolean userSpace) { 97 98 99 100 101 101 if (userSpace) { 102 return ComponentStatus.DEVELOPMENT; 103 } else { 104 return ComponentStatus.PUBLIC; 105 } 102 106 } 103 107 104 108 private ComponentRegistry getRegistry(ComponentStatus status) { 105 106 107 109 Principal userPrincipal = security.getUserPrincipal(); 110 UserCredentials userCredentials = getUserCredentials(userPrincipal); 111 return getRegistry(status, null, userCredentials); 108 112 } 109 113 110 114 private ComponentRegistry getRegistry(ComponentStatus status, Owner owner, UserCredentials userCredentials) { 111 112 113 114 115 116 115 try { 116 return componentRegistryFactory.getComponentRegistry(status, owner, userCredentials); 117 } catch (UserUnauthorizedException uuEx) { 118 //TODO: Throw actual exception and catch nicely 119 throw new RuntimeException("Cannot access requested registry", uuEx); 120 } 117 121 } 118 122 … … 123 127 */ 124 128 private Principal checkAndGetUserPrincipal() throws UserUnauthorizedException { 125 126 127 128 129 129 Principal principal = security.getUserPrincipal(); 130 if (principal == null) { 131 throw new UserUnauthorizedException("no user principal found."); 132 } 133 return principal; 130 134 } 131 135 132 136 private UserCredentials getUserCredentials(Principal userPrincipal) { 133 134 135 136 137 137 UserCredentials userCredentials = null; 138 if (userPrincipal != null) { 139 userCredentials = new UserCredentials(userPrincipal); 140 } 141 return userCredentials; 138 142 } 139 143 … … 142 146 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 143 147 public List<ComponentDescription> getRegisteredComponents(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 144 145 146 147 148 148 long start = System.currentTimeMillis(); 149 List<ComponentDescription> components = getRegistry(getStatus(userspace)).getComponentDescriptions(); 150 LOG.info("Releasing " + components.size() + " registered components into the world (" + (System.currentTimeMillis() - start) 151 + " millisecs)"); 152 return components; 149 153 } 150 154 … … 153 157 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 154 158 public List<ProfileDescription> getRegisteredProfiles(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, 155 156 157 158 159 160 161 162 163 164 165 166 159 @QueryParam(METADATA_EDITOR_PARAM) @DefaultValue("false") boolean metadataEditor) throws ComponentRegistryException { 160 long start = System.currentTimeMillis(); 161 List<ProfileDescription> profiles; 162 if (metadataEditor) { 163 profiles = getRegistry(getStatus(userspace)).getProfileDescriptionsForMetadaEditor(); 164 } else { 165 profiles = getRegistry(getStatus(userspace)).getProfileDescriptions(); 166 } 167 168 LOG.info("Releasing " + profiles.size() + " registered profiles into the world (" + (System.currentTimeMillis() - start) 169 + " millisecs)"); 170 return profiles; 167 171 } 168 172 … … 171 175 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 172 176 public Response getRegisteredComponent(@PathParam("componentId") String componentId, 173 174 175 176 177 178 179 180 177 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 178 LOG.info("Component with id: " + componentId + " is requested."); 179 CMDComponentSpec mdComponent = getRegistry(getStatus(userspace)).getMDComponent(componentId); 180 if (mdComponent == null) { 181 return Response.status(Status.NOT_FOUND).build(); 182 } else { 183 return Response.ok(mdComponent).build(); 184 } 181 185 } 182 186 … … 185 189 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML}) 186 190 public Response getRegisteredComponentRawType(@PathParam("componentId") final String componentId, @PathParam("rawType") String rawType) { 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 191 LOG.info("Component with id: " + componentId + " and rawType:" + rawType + " is requested."); 192 StreamingOutput result = null; 193 try { 194 final ComponentRegistry registry = findRegistry(componentId, new ComponentClosure()); 195 if (registry == null) { 196 return Response.status(Status.NOT_FOUND).entity("Id: " + componentId + " is not registered, cannot create data.").build(); 197 } 198 ComponentDescription desc = registry.getComponentDescription(componentId); 199 checkAndThrowDescription(desc, componentId); 200 String fileName = desc.getName() + "." + rawType; 201 if ("xml".equalsIgnoreCase(rawType)) { 202 result = new StreamingOutput() { 203 @Override 204 public void write(OutputStream output) throws IOException, WebApplicationException { 205 try { 206 registry.getMDComponentAsXml(componentId, output); 207 } catch (ComponentRegistryException e) { 208 LOG.info("Could not retrieve component", e); 209 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 210 } 211 } 212 }; 213 } else if ("xsd".equalsIgnoreCase(rawType)) { 214 result = new StreamingOutput() { 215 @Override 216 public void write(OutputStream output) throws IOException, WebApplicationException { 217 try { 218 registry.getMDComponentAsXsd(componentId, output); 219 } catch (ComponentRegistryException e) { 220 LOG.info("Could not retrieve component", e); 221 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 222 } 223 224 } 225 }; 226 } else { 227 throw new WebApplicationException(Response.serverError().entity( 228 "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); 229 } 230 return createDownloadResponse(result, fileName); 231 } catch (ComponentRegistryException e) { 232 LOG.info("Could not retrieve component", e); 233 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 234 } 231 235 } 232 236 233 237 public ComponentRegistry findRegistry(String id, RegistryClosure<? extends AbstractDescription> clos) throws ComponentRegistryException { 234 235 236 237 238 239 240 241 242 243 244 245 246 247 238 AbstractDescription desc = null; 239 ComponentRegistry result = getRegistry(getStatus(false)); 240 desc = clos.getDescription(result, id); 241 if (desc == null) { 242 List<ComponentRegistry> userRegs = componentRegistryFactory.getAllUserRegistries(); 243 for (ComponentRegistry reg : userRegs) { 244 desc = clos.getDescription(reg, id); 245 if (desc != null) { 246 result = reg; 247 break; 248 } 249 } 250 } 251 return result; 248 252 } 249 253 … … 252 256 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 253 257 public Response getRegisteredProfile(@PathParam("profileId") String profileId, 254 255 256 257 258 259 260 261 258 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 259 LOG.info("Profile with id: " + profileId + " is requested."); 260 CMDComponentSpec mdProfile = getRegistry(getStatus(userspace)).getMDProfile(profileId); 261 if (mdProfile == null) { 262 return Response.status(Status.NOT_FOUND).build(); 263 } else { 264 return Response.ok(mdProfile).build(); 265 } 262 266 } 263 267 … … 266 270 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 267 271 public List<AbstractDescription> getComponentUsage(@PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("true") boolean userspace) throws ComponentRegistryException { 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 272 try { 273 final long start = System.currentTimeMillis(); 274 ComponentRegistry registry = getRegistry(getStatus(userspace)); 275 List<ComponentDescription> components = registry.getUsageInComponents(componentId); 276 List<ProfileDescription> profiles = registry.getUsageInProfiles(componentId); 277 278 LOG.info("Found " + components.size() + " components and " + profiles.size() + " profiles that use component " + componentId 279 + " (" + (System.currentTimeMillis() - start) + " millisecs)"); 280 281 List<AbstractDescription> usages = new ArrayList<AbstractDescription>(components.size() + profiles.size()); 282 usages.addAll(components); 283 usages.addAll(profiles); 284 285 return usages; 286 } catch (ComponentRegistryException e) { 287 LOG.info("Could not retrieve profile usage", e); 288 throw e; 289 } 286 290 } 287 291 … … 290 294 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 291 295 public List<Comment> getCommentsFromProfile(@PathParam("profileId") String profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 292 293 294 295 296 297 296 long start = System.currentTimeMillis(); 297 final Principal principal = security.getUserPrincipal(); 298 List<Comment> comments = getRegistry(getStatus(userspace)).getCommentsInProfile(profileId, principal); 299 LOG.info("Releasing " + comments.size() + " registered comments in Profile into the world (" + (System.currentTimeMillis() - start) 300 + " millisecs)"); 301 return comments; 298 302 } 299 303 … … 302 306 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 303 307 public List<Comment> getCommentsFromComponent(@PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 304 305 306 307 308 309 308 long start = System.currentTimeMillis(); 309 final Principal principal = security.getUserPrincipal(); 310 List<Comment> comments = getRegistry(getStatus(userspace)).getCommentsInComponent(componentId, principal); 311 LOG.info("Releasing " + comments.size() + " registered comments in Component into the world (" + (System.currentTimeMillis() - start) 312 + " millisecs)"); 313 return comments; 310 314 } 311 315 … … 314 318 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 315 319 public Comment getSpecifiedCommentFromProfile(@PathParam("profileId") String profileId, @PathParam("commentId") String commentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 316 317 318 320 LOG.info(" Comments of component with id" + commentId + " are requested."); 321 final Principal principal = security.getUserPrincipal(); 322 return getRegistry(getStatus(userspace)).getSpecifiedCommentInProfile(profileId, commentId, principal); 319 323 } 320 324 … … 323 327 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 324 328 public Comment getSpecifiedCommentFromComponent(@PathParam("componentId") String componentId, @PathParam("commentId") String commentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 325 326 327 329 LOG.info(" Comments of component with id" + commentId + " are requested."); 330 final Principal principal = security.getUserPrincipal(); 331 return getRegistry(getStatus(userspace)).getSpecifiedCommentInComponent(componentId, commentId, principal); 328 332 } 329 333 330 334 /** 331 335 * 332 * Purely helper method for my front-end (FLEX) which only does post/get requests. The query param is checked and the "proper" method is333 * called.336 * Purely helper method for my front-end (FLEX) which only does post/get 337 * requests. The query param is checked and the "proper" method is called. 334 338 * 335 339 * @param profileId … … 340 344 @Path("/profiles/{profileId}") 341 345 public Response manipulateRegisteredProfile(@PathParam("profileId") String profileId, @FormParam("method") String method, 342 343 344 345 346 347 346 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 347 if ("delete".equalsIgnoreCase(method)) { 348 return deleteRegisteredProfile(profileId, userspace); 349 } else { 350 return Response.ok().build(); 351 } 348 352 } 349 353 … … 351 355 @Path("/profiles/{profileId}/comments/{commentId}") 352 356 public Response manipulateCommentFromProfile(@PathParam("profileId") String profileId, @PathParam("commentId") String commentId, @FormParam("method") String method, 353 354 355 356 357 358 357 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 358 if ("delete".equalsIgnoreCase(method)) { 359 return deleteCommentFromProfile(profileId, commentId, userspace); 360 } else { 361 return Response.ok().build(); 362 } 359 363 } 360 364 … … 362 366 @Path("/components/{componentId}/comments/{commentId}") 363 367 public Response manipulateCommentFromComponent(@PathParam("componentId") String componentId, @PathParam("commentId") String commentId, @FormParam("method") String method, 364 365 366 367 368 369 368 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 369 if ("delete".equalsIgnoreCase(method)) { 370 return deleteCommentFromComponent(componentId, commentId, userspace); 371 } else { 372 return Response.ok().build(); 373 } 370 374 } 371 375 … … 374 378 @Consumes("multipart/form-data") 375 379 public Response publishRegisteredProfile(@PathParam("profileId") String profileId, @FormDataParam(DATA_FORM_FIELD) InputStream input, 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 380 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 381 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 382 try { 383 Principal principal = checkAndGetUserPrincipal(); 384 ProfileDescription desc = getRegistry(getStatus(true)).getProfileDescription(profileId); 385 if (desc != null) { 386 updateDescription(desc, name, description, domainName, group); 387 return register(input, desc, getUserCredentials(principal), true, new PublishAction(principal)); 388 } else { 389 LOG.error("Update of nonexistent id (" + profileId + ") failed."); 390 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 391 } 392 } catch (ComponentRegistryException e) { 393 LOG.info("Could not retrieve component", e); 394 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 395 } catch (UserUnauthorizedException ex) { 396 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 397 } 394 398 } 395 399 … … 398 402 @Consumes("multipart/form-data") 399 403 public Response updateRegisteredProfile(@PathParam("profileId") String profileId, 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 404 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @FormDataParam(DATA_FORM_FIELD) InputStream input, 405 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 406 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 407 try { 408 Principal principal = checkAndGetUserPrincipal(); 409 UserCredentials userCredentials = getUserCredentials(principal); 410 ProfileDescription desc = getRegistry(getStatus(userspace)).getProfileDescription(profileId); 411 if (desc != null) { 412 updateDescription(desc, name, description, domainName, group); 413 return register(input, desc, userCredentials, userspace, new UpdateAction(principal)); 414 } else { 415 LOG.error("Update of nonexistent id (" + profileId + ") failed."); 416 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 417 } 418 } catch (ComponentRegistryException e) { 419 LOG.info("Could not retrieve component", e); 420 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 421 } catch (UserUnauthorizedException ex) { 422 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 423 } 420 424 421 425 } … … 423 427 /** 424 428 * 425 * Purely helper method for my front-end (FLEX) which van only do post/get requests. The query param is checked and the "proper" method426 * is called.429 * Purely helper method for my front-end (FLEX) which van only do post/get 430 * requests. The query param is checked and the "proper" method is called. 427 431 * 428 432 * @param componentId … … 433 437 @Path("/components/{componentId}") 434 438 public Response manipulateRegisteredComponent(@PathParam("componentId") String componentId, @FormParam("method") String method, 435 436 437 438 439 440 439 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 440 if ("delete".equalsIgnoreCase(method)) { 441 return deleteRegisteredComponent(componentId, userspace); 442 } else { 443 return Response.ok().build(); 444 } 441 445 } 442 446 … … 445 449 @Consumes("multipart/form-data") 446 450 public Response publishRegisteredComponent(@PathParam("componentId") String componentId, 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 451 @FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 452 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, 453 @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 454 try { 455 Principal principal = checkAndGetUserPrincipal(); 456 // TODO: Get status from parameter 457 ComponentDescription desc = getRegistry(getStatus(true)).getComponentDescription(componentId); 458 if (desc != null) { 459 updateDescription(desc, name, description, domainName, group); 460 return register(input, desc, getUserCredentials(principal), true, new PublishAction(principal)); 461 } else { 462 LOG.error("Update of nonexistent id (" + componentId + ") failed."); 463 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 464 } 465 } catch (ComponentRegistryException e) { 466 LOG.info("Could not retrieve component", e); 467 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 468 } catch (UserUnauthorizedException ex) { 469 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 470 } 467 471 } 468 472 … … 471 475 @Consumes("multipart/form-data") 472 476 public Response updateRegisteredComponent(@PathParam("componentId") String componentId, 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 477 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @FormDataParam(DATA_FORM_FIELD) InputStream input, 478 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 479 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 480 try { 481 Principal principal = checkAndGetUserPrincipal(); 482 ComponentDescription desc = getRegistry(getStatus(userspace)).getComponentDescription(componentId); 483 if (desc != null) { 484 updateDescription(desc, name, description, domainName, group); 485 return register(input, desc, getUserCredentials(principal), userspace, new UpdateAction(principal)); 486 } else { 487 LOG.error("Update of nonexistent id (" + componentId + ") failed."); 488 return Response.serverError().entity("Invalid id, cannot update nonexistent component").build(); 489 } 490 } catch (ComponentRegistryException e) { 491 LOG.info("Could not retrieve component", e); 492 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 493 } catch (UserUnauthorizedException ex) { 494 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 495 } 492 496 } 493 497 494 498 private void updateDescription(AbstractDescription desc, String name, String description, String domainName, String group) { 495 496 497 498 499 499 desc.setName(name); 500 desc.setDescription(description); 501 desc.setDomainName(domainName); 502 desc.setGroupName(group); 503 desc.setRegistrationDate(AbstractDescription.createNewDate()); 500 504 } 501 505 … … 503 507 @Path("/components/{componentId}") 504 508 public Response deleteRegisteredComponent(@PathParam("componentId") String componentId, 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 509 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 510 try { 511 Principal principal = checkAndGetUserPrincipal(); 512 ComponentRegistry registry = getRegistry(getStatus(userspace)); 513 LOG.info("Component with id: " + componentId + " set for deletion."); 514 registry.deleteMDComponent(componentId, principal, false); 515 } catch (DeleteFailedException e) { 516 LOG.info("Component with id: " + componentId + " deletion failed.", e); 517 return Response.status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 518 } catch (ComponentRegistryException e) { 519 LOG.info("Component with id: " + componentId + " deletion failed.", e); 520 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 521 } catch (IOException e) { 522 LOG.info("Component with id: " + componentId + " deletion failed.", e); 523 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 524 } catch (UserUnauthorizedException e) { 525 LOG.info("Component with id: " + componentId + " deletion failed: " + e.getMessage()); 526 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 527 } 528 LOG.info("Component with id: " + componentId + " deleted."); 529 return Response.ok().build(); 526 530 } 527 531 … … 529 533 @Path("/profiles/{profileId}") 530 534 public Response deleteRegisteredProfile(@PathParam("profileId") String profileId, 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 535 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 536 try { 537 Principal principal = checkAndGetUserPrincipal(); 538 LOG.info("Profile with id: " + profileId + " set for deletion."); 539 getRegistry(getStatus(userspace)).deleteMDProfile(profileId, principal); 540 } catch (DeleteFailedException e) { 541 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 542 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 543 } catch (ComponentRegistryException e) { 544 LOG.info("Could not retrieve component", e); 545 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 546 } catch (IOException e) { 547 LOG.info("Profile with id: " + profileId + " deletion failed.", e); 548 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 549 } catch (UserUnauthorizedException e) { 550 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 551 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 552 } 553 LOG.info("Profile with id: " + profileId + " deleted."); 554 return Response.ok().build(); 551 555 } 552 556 … … 554 558 @Path("/profiles/{profileId}/comments/{commentId}") 555 559 public Response deleteCommentFromProfile(@PathParam("profileId") String profileId, @PathParam("commentId") String commentId, 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 560 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 561 try { 562 final Principal principal = checkAndGetUserPrincipal(); 563 final ComponentRegistry registry = getRegistry(getStatus(userspace)); 564 final Comment comment = registry.getSpecifiedCommentInProfile(profileId, commentId, principal); 565 if (comment != null && profileId.equals(comment.getProfileDescriptionId())) { 566 LOG.info("Comment with id: " + commentId + " set for deletion."); 567 registry.deleteComment(commentId, principal); 568 } else { 569 throw new ComponentRegistryException("Comment not found for specified profile"); 570 } 571 } catch (DeleteFailedException e) { 572 LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 573 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 574 } catch (ComponentRegistryException e) { 575 LOG.info("Could not retrieve component", e); 576 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 577 } catch (IOException e) { 578 LOG.info("Comment with id: " + commentId + " deletion failed.", e); 579 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 580 } catch (UserUnauthorizedException e) { 581 LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 582 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 583 } 584 LOG.info("Comment with id: " + commentId + " deleted."); 585 return Response.ok().build(); 582 586 } 583 587 … … 585 589 @Path("/components/{componentId}/comments/{commentId}") 586 590 public Response deleteCommentFromComponent(@PathParam("componentId") String componentId, @PathParam("commentId") String commentId, 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 591 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 592 try { 593 final Principal principal = checkAndGetUserPrincipal(); 594 final ComponentRegistry registry = getRegistry(getStatus(userspace)); 595 final Comment comment = registry.getSpecifiedCommentInComponent(componentId, commentId, principal); 596 if (comment != null && componentId.equals(comment.getComponentDescriptionId())) { 597 LOG.info("Comment with id: " + commentId + " set for deletion."); 598 registry.deleteComment(commentId, principal); 599 } else { 600 throw new ComponentRegistryException("Comment not found for specified component"); 601 } 602 } catch (DeleteFailedException e) { 603 LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 604 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 605 } catch (ComponentRegistryException e) { 606 LOG.info("Could not retrieve component", e); 607 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 608 } catch (IOException e) { 609 LOG.info("Comment with id: " + commentId + " deletion failed.", e); 610 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 611 } catch (UserUnauthorizedException e) { 612 LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 613 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 614 } 615 LOG.info("Comment with id: " + commentId + " deleted."); 616 return Response.ok().build(); 613 617 } 614 618 … … 617 621 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML}) 618 622 public Response getRegisteredProfileRawType(@PathParam("profileId") final String profileId, @PathParam("rawType") String rawType) { 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 623 LOG.info("Profile with id: " + profileId + " and rawType:" + rawType + " is requested."); 624 StreamingOutput result = null; 625 try { 626 final ComponentRegistry registry = findRegistry(profileId, new ProfileClosure()); 627 if (registry == null) { 628 return Response.status(Status.NOT_FOUND).entity("Id: " + profileId + " is not registered, cannot create data.").build(); 629 } 630 ProfileDescription desc = registry.getProfileDescription(profileId); 631 checkAndThrowDescription(desc, profileId); 632 String fileName = desc.getName() + "." + rawType; 633 634 if ("xml".equalsIgnoreCase(rawType)) { 635 result = new StreamingOutput() { 636 @Override 637 public void write(OutputStream output) throws IOException, WebApplicationException { 638 try { 639 registry.getMDProfileAsXml(profileId, output); 640 } catch (ComponentRegistryException e) { 641 LOG.warn("Could not retrieve component", e); 642 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 643 } 644 } 645 }; 646 } else if ("xsd".equalsIgnoreCase(rawType)) { 647 result = new StreamingOutput() { 648 @Override 649 public void write(OutputStream output) throws IOException, WebApplicationException { 650 try { 651 registry.getMDProfileAsXsd(profileId, output); 652 } catch (ComponentRegistryException e) { 653 LOG.warn("Could not retrieve component", e); 654 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 655 } 656 } 657 }; 658 } else { 659 throw new WebApplicationException(Response.serverError().entity( 660 "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); 661 } 662 return createDownloadResponse(result, fileName); 663 } catch (ComponentRegistryException e) { 664 LOG.info("Could not retrieve component", e); 665 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 666 } 663 667 } 664 668 665 669 private void checkAndThrowDescription(AbstractDescription desc, String id) { 666 667 668 670 if (desc == null) { 671 throw new WebApplicationException(Response.serverError().entity("Incorrect id:" + id + "cannot handle request").build()); 672 } 669 673 } 670 674 671 675 private Response createDownloadResponse(StreamingOutput result, String fileName) { 672 673 674 675 676 //Making response so it triggers browsers native save as dialog. 677 Response response = Response.ok().type("application/x-download").header("Content-Disposition", 678 "attachment; filename=\"" + fileName + "\"").entity(result).build(); 679 return response; 676 680 677 681 } … … 682 686 @Consumes("multipart/form-data") 683 687 public Response registerProfile(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 688 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName, 689 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 690 try { 691 Principal principal = checkAndGetUserPrincipal(); 692 UserCredentials userCredentials = getUserCredentials(principal); 693 ProfileDescription desc = createNewProfileDescription(); 694 desc.setCreatorName(userCredentials.getDisplayName()); 695 desc.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 696 desc.setName(name); 697 desc.setDescription(description); 698 desc.setGroupName(group); 699 desc.setDomainName(domainName); 700 LOG.info("Trying to register Profile: " + desc); 701 return register(input, desc, userCredentials, userspace, new NewAction()); 702 } catch (UserUnauthorizedException ex) { 703 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 704 } 701 705 } 702 706 … … 706 710 @Consumes("multipart/form-data") 707 711 public Response registerComponent(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 712 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, 713 @FormDataParam(DOMAIN_FORM_FIELD) String domainName, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 714 try { 715 Principal principal = checkAndGetUserPrincipal(); 716 UserCredentials userCredentials = getUserCredentials(principal); 717 ComponentDescription desc = createNewComponentDescription(); 718 desc.setCreatorName(userCredentials.getDisplayName()); 719 desc.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 720 desc.setName(name); 721 desc.setDescription(description); 722 desc.setGroupName(group); 723 desc.setDomainName(domainName); 724 LOG.info("Trying to register Component: " + desc); 725 return register(input, desc, userCredentials, userspace, new NewAction()); 726 } catch (UserUnauthorizedException ex) { 727 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 728 } 725 729 } 726 730 … … 730 734 @Consumes("multipart/form-data") 731 735 public Response registerCommentInComponent(@FormDataParam(DATA_FORM_FIELD) InputStream input, 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 736 @PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 737 try { 738 Principal principal = checkAndGetUserPrincipal(); 739 UserCredentials userCredentials = getUserCredentials(principal); 740 if (null == componentRegistryFactory.getOrCreateUser(userCredentials)) { 741 throw new UserUnauthorizedException("Cannot materialize authenticated user"); 742 } 743 // TODO: Add user/group param 744 ComponentRegistry registry = getRegistry(getStatus(userspace), null, userCredentials); 745 ComponentDescription description = registry.getComponentDescription(componentId); 746 if (description != null) { 747 LOG.info("Trying to register comment to " + componentId); 748 return registerComment(input, registry, userspace, description, principal, userCredentials); 749 } else { 750 LOG.error("Attempt to post comment on nonexistent component id (" + componentId + ") failed."); 751 return Response.serverError().entity("Invalid id, cannot comment on nonexistent component").build(); 752 } 753 } catch (UserUnauthorizedException ex) { 754 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 755 } 752 756 } 753 757 … … 757 761 @Consumes("multipart/form-data") 758 762 public Response registerCommentInProfile(@FormDataParam(DATA_FORM_FIELD) InputStream input, 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 763 @PathParam("profileId") String profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 764 try { 765 Principal principal = checkAndGetUserPrincipal(); 766 UserCredentials userCredentials = getUserCredentials(principal); 767 if (null == componentRegistryFactory.getOrCreateUser(userCredentials)) { 768 throw new UserUnauthorizedException("Cannot materialize authenticated user"); 769 } 770 // TODO: Add user/group param 771 ComponentRegistry registry = getRegistry(getStatus(userspace), null, userCredentials); 772 ProfileDescription description = registry.getProfileDescription(profileId); 773 if (description != null) { 774 LOG.info("Trying to register comment to " + profileId); 775 return registerComment(input, registry, userspace, description, principal, userCredentials); 776 } else { 777 LOG.error("Attempt to post comment on nonexistent profile id (" + profileId + ") failed."); 778 return Response.serverError().entity("Invalid id, cannot comment on nonexistent profile").build(); 779 } 780 } catch (UserUnauthorizedException ex) { 781 return Response.status(Status.UNAUTHORIZED).entity(ex.getMessage()).build(); 782 } 779 783 } 780 784 … … 783 787 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 784 788 public Response pingSession() { 785 786 787 788 789 790 791 792 793 789 boolean stillActive = false; 790 Principal userPrincipal = security.getUserPrincipal(); 791 LOG.info("ping by user: " + (userPrincipal == null ? "null" : userPrincipal.getName())); 792 if (request != null) { 793 if (userPrincipal != null && !ComponentRegistryFactory.ANONYMOUS_USER.equals(userPrincipal.getName())) { 794 stillActive = !((HttpServletRequest) request).getSession().isNew(); 795 } 796 } 797 return Response.ok().entity("<session stillActive=\"" + stillActive + "\"/>").build(); 794 798 } 795 799 796 800 private Response register(InputStream input, AbstractDescription desc, UserCredentials userCredentials, boolean userspace, 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 801 RegisterAction action) { 802 try { 803 // TODO: Add user/group param 804 ComponentRegistry registry = getRegistry(getStatus(userspace), null, userCredentials); 805 DescriptionValidator descriptionValidator = new DescriptionValidator(desc); 806 MDValidator validator = new MDValidator(input, desc, registry, getRegistry(getStatus(true)), componentRegistryFactory.getPublicRegistry()); 807 RegisterResponse response = new RegisterResponse(); 808 response.setIsInUserSpace(userspace); 809 validate(response, descriptionValidator, validator); 810 if (response.getErrors().isEmpty()) { 811 812 CMDComponentSpec spec = validator.getCMDComponentSpec(); 813 814 // Olha: removing filename from spec before it gets extended!!! recursion over all the components 815 setFileNamesFromListToNull(spec.getCMDComponent()); 816 817 818 819 try { 820 821 822 // Expand to check for recursion 823 registry.getExpander().expandNestedComponent(spec.getCMDComponent(), desc.getId()); 824 825 826 827 // Add profile 828 829 830 int returnCode = action.execute(desc, spec, response, registry); 831 if (returnCode == 0) { 832 response.setRegistered(true); 833 response.setDescription(desc); 834 } else { 835 response.setRegistered(false); 836 response.addError("Unable to register at this moment. Internal server error."); 837 } 838 } catch (ComponentRegistryException ex) { 839 // Recursion detected 840 response.setRegistered(false); 841 response.addError("Error while expanding specification. " + ex.getMessage()); 842 } 843 } else { 844 LOG.info("Registration failed with validation errors:" + Arrays.toString(response.getErrors().toArray())); 845 response.setRegistered(false); 846 } 847 response.setIsProfile(desc.isProfile()); 848 return Response.ok(response).build(); 849 } finally { 850 try { 851 input.close();//either we read the input or there was an exception, we need to close it. 852 } catch (IOException e) { 853 LOG.error("Error when closing inputstream: ", e); 854 } 855 } 852 856 } 853 857 854 858 private Response registerComment(InputStream input, ComponentRegistry registry, boolean userspace, 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 859 AbstractDescription description, Principal principal, UserCredentials userCredentials) { 860 try { 861 CommentValidator validator = new CommentValidator(input, description); 862 CommentResponse response = new CommentResponse(); 863 response.setIsInUserSpace(userspace); 864 validateComment(response, validator); 865 if (response.getErrors().isEmpty()) { 866 Comment com = validator.getCommentSpec(); 867 //int returnCode = action.executeComment(com, response, registry, principal.getName()); 868 869 // If user name is left empty, fill it using the user's display name 870 if (null == com.getUserName() || "".equals(com.getUserName())) { 871 if (userCredentials != null) { 872 com.setUserName(userCredentials.getDisplayName()); 873 } else { 874 com.setUserName(principal.getName()); 875 } 876 } 877 878 int returnCode = registry.registerComment(com, principal.getName()); 879 if (returnCode == 0) { 880 response.setRegistered(true); 881 response.setComment(com); 882 } else { 883 response.setRegistered(false); 884 response.addError("Unable to register at this moment. Internal server error."); 885 } 886 } else { 887 LOG.info("Registration failed with validation errors:" + Arrays.toString(response.getErrors().toArray())); 888 response.setRegistered(false); 889 } 890 return Response.ok(response).build(); 891 } catch (ComponentRegistryException ex) { 892 LOG.error("Error while inserting comment: ", ex); 893 return Response.serverError().entity(ex.getMessage()).build(); 894 } finally { 895 try { 896 input.close();//either we read the input or there was an exception, we need to close it. 897 } catch (IOException e) { 898 LOG.error("Error when closing inputstream: ", e); 899 return Response.serverError().build(); 900 } 901 } 898 902 } 899 903 900 904 private ComponentDescription createNewComponentDescription() { 901 902 903 905 ComponentDescription desc = ComponentDescription.createNewDescription(); 906 desc.setHref(createXlink(desc.getId())); 907 return desc; 904 908 } 905 909 906 910 private ProfileDescription createNewProfileDescription() { 907 908 909 911 ProfileDescription desc = ProfileDescription.createNewDescription(); 912 desc.setHref(createXlink(desc.getId())); 913 return desc; 910 914 } 911 915 912 916 private String createXlink(String id) { 913 914 917 URI uri = uriInfo.getRequestUriBuilder().path(id).build(); 918 return uri.toString(); 915 919 } 916 920 917 921 /** 918 922 * 919 * @return The application's base URI as configured in the "componentRegistryServiceRootUrl" context parameter. 920 * If correctly configured, it should look something like "http://catalog.clarin.eu/ds/ComponentRegistry". 921 * <em>Be aware that this can also be null if configured incorrectly!</em> 923 * @return The application's base URI as configured in the 924 * "componentRegistryServiceRootUrl" context parameter. If correctly 925 * configured, it should look something like 926 * "http://catalog.clarin.eu/ds/ComponentRegistry". <em>Be aware that this 927 * can also be null if configured incorrectly!</em> 922 928 * 923 929 * @see #APPLICATION_BASE_URL_PARAM 924 930 */ 925 931 private String getApplicationBaseURI() { 926 932 return servletContext.getInitParameter(APPLICATION_BASE_URL_PARAM); 927 933 } 928 934 929 935 private void validate(RegisterResponse response, Validator... validators) { 930 931 932 933 934 935 936 936 for (Validator validator : validators) { 937 if (!validator.validate()) { 938 for (String error : validator.getErrorMessages()) { 939 response.addError(error); 940 } 941 } 942 } 937 943 } 938 944 939 945 private void validateComment(CommentResponse response, Validator... validators) { 940 941 942 943 944 945 946 946 for (Validator validator : validators) { 947 if (!validator.validate()) { 948 for (String error : validator.getErrorMessages()) { 949 response.addError(error); 950 } 951 } 952 } 947 953 } 948 954 … … 951 957 */ 952 958 public void setComponentRegistryFactory(ComponentRegistryFactory componentRegistryFactory) { 953 959 this.componentRegistryFactory = componentRegistryFactory; 954 960 } 955 961 … … 960 966 public void setFileNamesFromListToNull(List<CMDComponentType> listofcomponents) { 961 967 962 963 964 968 for (CMDComponentType currentcomponent : listofcomponents) { 969 setFileNamesToNullCurrent(currentcomponent); 970 } 965 971 966 972 } … … 971 977 public void setFileNamesToNullCurrent(CMDComponentType currentcomponent) { 972 978 973 974 979 currentcomponent.setFilename(null); 980 setFileNamesFromListToNull(currentcomponent.getCMDComponent()); 975 981 976 982 } … … 980 986 * 981 987 */ 982 983 private <T extends AbstractDescription> Rss getRss(boolean userspace,String limit, List<T> descs, String kindofdesc) throws ComponentRegistryException, ParseException { 984 985 String baseUri = getApplicationBaseURI()+"/"; 986 RssCreatorDescriptions rssCreator = new RssCreatorDescriptions(baseUri); 987 rssCreator.setVersion(2.0); 988 private <T extends AbstractDescription> Rss getRss(String limit, List<T> descs, 989 String link, String description, String title) throws ComponentRegistryException, ParseException { 990 988 991 992 RssCreatorDescriptions rssCreator = new RssCreatorDescriptions(); 989 993 994 rssCreator.setLink(link); 995 rssCreator.setDescription(description); 996 rssCreator.setTitle(title); 997 998 Collections.sort(descs, AbstractDescription.COMPARE_ON_DATE); 999 990 1000 int limitInt = Integer.parseInt(limit); 1001 1002 if (descs.size() < limitInt) { 1003 limitInt = descs.size(); 1004 }; 1005 List<T> sublist = descs.subList(0, limitInt); 1006 1007 Rss rss = rssCreator.makeRss(sublist); 1008 1009 return rss; 1010 } 1011 //////////////////////////////////////////////// 1012 1013 @GET 1014 @Path("/components/rss") 1015 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 1016 public Rss getRssComponent(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException, ParseException { 1017 1018 List<ComponentDescription> components = getRegistry(getStatus(userspace)).getComponentDescriptions(); 991 1019 992 if (descs.size()<limitInt) {limitInt = descs.size();}; 993 List<T> sublist = descs.subList(0, limitInt); 994 Collections.sort(sublist, AbstractDescription.COMPARE_ON_DATE); 1020 String title; 1021 if (userspace) { title= "Workspace components";} 1022 else {title= "Workspace components"; 1023 } 1024 String link= getApplicationBaseURI() + "/"; 1025 String description = "Updates for components"; 995 1026 1027 Rss rss = getRss(limit, components, link, description, title); 1028 1029 LOG.info("Releasing " + limit + "most recent registered components into the world sorted by their registration date-and-time"); 1030 1031 1032 return rss; 1033 } 1034 1035 //////////////////////////////////////////////// 1036 @GET 1037 @Path("/profiles/rss") 1038 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 1039 public Rss getRssProfile(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException, ParseException { 1040 996 1041 997 if (userspace) {rssCreator.setTitle("Workspace "+kindofdesc);} 998 else {rssCreator.setTitle("Public "+kindofdesc);} 1042 1043 // ?? How to get rid of the deprecated stuff ?? 1044 List<ProfileDescription> profiles = getRegistry(getStatus(userspace)).getProfileDescriptions(); 1045 1046 String title; 1047 if (userspace) { title= "Workspace profiles";} 1048 else {title= "Workspace profiles"; 1049 } 1050 String link= getApplicationBaseURI() + "/"; 1051 String description = "Updates for profiles"; 1052 1053 Rss rss = getRss(limit, profiles, link, description, title); 1054 1055 1056 LOG.info("Releasing " + limit + "most recent registered profiles into the world sorted by their registration date-and-time"); 1057 return rss; 1058 } 1059 1060 /* auxiliary debigging piece of code for grabbing Id-s of profiles and comments 1061 * 1062 List<ProfileDescription> lprfaux = getRegisteredProfiles(userspace, true); 1063 999 1064 1000 Rss rss =rssCreator.makeRss(sublist); 1001 1002 return rss; 1003 } 1004 //////////////////////////////////////////////// 1005 1006 @GET 1007 @Path("/components/rss") 1008 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 1009 public Rss getRssComponent(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException, ParseException { 1010 1011 List<ComponentDescription> components = getRegistry(getStatus(userspace)).getComponentDescriptions(); 1012 Rss rss = getRss(userspace, limit, components, "components"); 1065 for (ProfileDescription currentProfile : lprfaux) { 1066 String currentProfileId = currentProfile.getId(); 1067 LOG.debug(currentProfileId); 1068 1069 } 1070 * 1071 List<ComponentDescription> lcmaux = getRegisteredProfiles(userspace, true); 1013 1072 1014 LOG.info("Releasing " + limit + "most recent registered components into the world sorted by their registration date-and-time"); 1015 1073 1074 for (ProfileDescription currentProfile : lprfaux) { 1075 String currentProfileId = currentProfile.getId(); 1076 LOG.debug(currentProfileId); 1077 1078 } 1079 * 1016 1080 1017 return rss; 1018 } 1019 1020 //////////////////////////////////////////////// 1021 @GET 1022 @Path("/profiles/rss") 1023 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 1024 public Rss getRssProfile(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException, ParseException { 1025 1026 //http://www.clarin.eu/cmdi/profiles 1027 1028 // ?? How to get rid of the deprecated stuff ?? 1029 List<ProfileDescription> profiles = getRegistry(getStatus(userspace)).getProfileDescriptions(); 1030 Rss rss = getRss(userspace, limit, profiles, "profiles"); 1031 1032 LOG.info("Releasing " + limit + "most recent registered profiles into the world sorted by their registration date-and-time"); 1033 return rss; 1034 } 1035 1081 */ 1036 1082 /////////////////////////////////////////////////////////// 1037 @GET 1038 @Path("/profiles/{profileId}/comments/rss") 1039 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 1040 public Rss getRssOfCommentsFromProfile(@PathParam("profileId") String profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException, IOException, JAXBException, ParseException { 1041 1042 1043 // ?? this is for debugging. But, anyway, how to get all the profiles without the need to type them in? 1044 // what if a user do not remember his/her id of profile? 1045 // grabbing all registered profile names from the register and outputting them on the tomcat terminal */ 1046 /*List<ProfileDescription> lprfaux = getRegisteredProfiles(userspace, true); 1047 1048 for (ProfileDescription currentProfile : lprfaux) { 1049 String currentProfileId = currentProfile.getId(); 1050 LOG.debug(currentProfileId); 1051 } 1052 */ 1053 // end of grabbing */ 1054 1055 1056 1057 final Principal principal = security.getUserPrincipal(); 1058 List<Comment> comments = getRegistry(getStatus(userspace)).getCommentsInProfile(profileId, principal); 1059 1060 String baseUri = getApplicationBaseURI()+"/"; 1061 1062 1083 private Rss getRssOfComments(String limit, List<Comment> comments, String description, 1084 String title, String link) throws ComponentRegistryException, ParseException, IOException, JAXBException { 1085 1086 Collections.sort(comments, Comment.COMPARE_ON_DATE); 1087 1063 1088 int limitInt = Integer.parseInt(limit); 1064 1065 if (comments.size()<limitInt) {limitInt = comments.size();}; 1089 if (comments.size() < limitInt) { 1090 limitInt = comments.size(); 1091 }; 1066 1092 List<Comment> sublist = comments.subList(0, limitInt); 1067 Collections.sort(sublist, Comment.COMPARE_ON_DATE); 1068 1069 for (Comment comm: sublist){ 1070 LOG.debug(comm.getCommentDate()); 1071 } 1072 1073 RssCreatorComments instance = new RssCreatorComments(baseUri); 1074 instance.setFlagIsFromProfile(true); 1075 instance.setDescription("Update of comments for current profile"); 1076 1077 String hrefPostfix = "&view=comments"; 1078 String hrefInfix = "?item="; 1079 instance.setLink(baseUri+hrefInfix+profileId+hrefPostfix); 1080 1081 String title="Comments feed for the profile \""+ 1082 getRegistry(getStatus(userspace)).getProfileDescription(profileId).getName()+ 1083 "\" "; 1093 1094 1095 // debug stuff, to see if the dates are sorted with the latest on the top 1096 for (Comment comm : sublist) { 1097 String dt = comm.getCommentDate(); 1098 LOG.debug(dt); 1099 } 1100 // 1101 1102 1103 1104 RssCreatorComments instance = new RssCreatorComments(); 1105 1106 instance.setDescription(description); 1107 instance.setLink(link); 1084 1108 instance.setTitle(title); 1085 1086 1109 1087 1110 Rss result = instance.makeRss(sublist); 1088 1111 1089 // testing stuff1090 String path =openTestDir("testRss");1112 // testing stuff 1113 String path = openTestDir("testRss"); 1091 1114 String os = MDMarshaller.marshalToString(result); 1092 1115 writeStringToFile(os, path + "testRssResl.xml"); 1093 1116 //end of testing stuff 1117 1118 // debugging stuff 1119 LOG.debug("The amount of items: " + Integer.toString(result.getChannel().getItem().size())); 1120 LOG.debug("The amount of comments: " + Integer.toString(comments.size())); 1121 // end of debugging stuff 1122 1123 1124 1125 return result; 1126 1127 } 1128 1129 /////////////////////////////////////////////////////// 1130 @GET 1131 @Path("/profiles/{profileId}/comments/rss") 1132 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 1133 public Rss getRssOfCommentsFromProfile(@PathParam("profileId") String profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException, IOException, JAXBException, ParseException { 1134 1135 1136 final Principal principal = security.getUserPrincipal(); 1137 List<Comment> comments = getRegistry(getStatus(userspace)).getCommentsInProfile(profileId, principal); 1138 1139 String baseUri = getApplicationBaseURI() + "/"; 1140 String link = baseUri + "?item=" + profileId + "&view=comments"; 1141 1142 String title = "Comments feed for the profile \"" 1143 + getRegistry(getStatus(userspace)).getProfileDescription(profileId).getName() 1144 + "\" "; 1145 1146 Rss result = getRssOfComments(limit, comments, "Update of comments for current profile", 1147 title, link); 1148 1149 1150 return result; 1151 } 1152 1153 ////////////////////////////// 1154 @GET 1155 @Path("/components/{componentId}/comments/rss") 1156 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 1157 public Rss getRssOfCommentsFromComponent(@PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException, IOException, JAXBException, ParseException { 1158 1159 //debigging piece 1160 List<ComponentDescription> lcmaux = getRegisteredComponents(userspace); 1161 1162 1163 for (ComponentDescription current : lcmaux) { 1164 String currentId = current.getId(); 1165 LOG.debug(currentId); 1166 } 1094 1167 1095 // debugging stuff 1096 LOG.debug("The amount of items: "+Integer.toString(result.getChannel().getItem().size())); 1097 LOG.debug("The amount of comments: "+Integer.toString(comments.size())); 1098 // end of debugging stuff 1099 1100 1101 return result; 1168 //end of debug 1169 1170 final Principal principal = security.getUserPrincipal(); 1171 List<Comment> comments = getRegistry(getStatus(userspace)).getCommentsInComponent(componentId, principal); 1172 1173 String baseUri = getApplicationBaseURI() + "/"; 1174 String link = baseUri + "?item=" + componentId + "&view=comments"; 1175 String title = "Comments feed for the component \"" 1176 + getRegistry(getStatus(userspace)).getComponentDescription(componentId).getName() 1177 + "\" "; 1178 1179 Rss result = getRssOfComments(limit, comments, "Update of comments for current component", 1180 title, link); 1181 1182 1183 1184 return result; 1102 1185 } 1103 1186 … … 1106 1189 /** 1107 1190 * 1108 * @param bytes is an array of bytes to be written in the file filename (from scratch!) 1109 * @param filename is the name of the file where the array "bytes" is to be written to 1191 * @param bytes is an array of bytes to be written in the file filename 1192 * (from scratch!) 1193 * @param filename is the name of the file where the array "bytes" is to be 1194 * written to 1110 1195 * @throws IOException 1111 1196 * @throws JAXBException … … 1113 1198 public static void writeBytesToFile(byte[] bytes, String filename) throws IOException, JAXBException { 1114 1199 1115 1116 1117 1118 1119 1120 1121 1200 File file = new File(filename); 1201 FileOutputStream fop = new FileOutputStream(file); 1202 1203 fop.write(bytes); 1204 1205 fop.flush(); 1206 fop.close(); 1122 1207 1123 1208 … … 1126 1211 /** 1127 1212 * 1128 * @param str is a string which is to be written into the filename (from scratch!) 1213 * @param str is a string which is to be written into the filename (from 1214 * scratch!) 1129 1215 * @param filename is a filename where the string is to be written to 1130 1216 * @throws IOException … … 1133 1219 public static void writeStringToFile(String str, String filename) throws IOException, JAXBException { 1134 1220 1135 1221 writeBytesToFile(str.getBytes(), filename); 1136 1222 1137 1223 … … 1140 1226 /** 1141 1227 * 1142 * @param os is an output stream which is to be written into the filename (from scratch!) 1228 * @param os is an output stream which is to be written into the filename 1229 * (from scratch!) 1143 1230 * @param filename is a filename where the stream is to be written to 1144 1231 * @throws IOException … … 1147 1234 public static void writeStreamToFile(ByteArrayOutputStream os, String filename) throws IOException, JAXBException { 1148 1235 1149 1236 writeBytesToFile(os.toByteArray(), filename); 1150 1237 1151 1238 … … 1154 1241 /** 1155 1242 * 1156 * @param cdesc is a component which is to be written into the filename (from scratch!) 1243 * @param cdesc is a component which is to be written into the filename 1244 * (from scratch!) 1157 1245 * @param filename is a filename where the component is to be written to 1158 1246 * @throws IOException … … 1162 1250 1163 1251 1164 1165 1166 1167 1252 ByteArrayOutputStream os = new ByteArrayOutputStream(); 1253 MDMarshaller.marshal(cdesc, os); 1254 1255 writeStreamToFile(os, filename); 1168 1256 1169 1257 } … … 1172 1260 * opens a temporary sub-directory dirName in /target/ 1173 1261 * 1174 * @param dirName is the name of the temporary subdirectory which is to be opened 1262 * @param dirName is the name of the temporary subdirectory which is to be 1263 * opened 1175 1264 * @return the absolute part for this directory 1176 1265 */ 1177 1266 public static String openTestDir(String dirName) { 1178 1267 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1268 File testDir = new File("target/" + dirName); 1269 1270 1271 testDir.mkdir(); 1272 1273 System.out.println(dirName); 1274 //String retval = new File(testDir, dirName).getAbsolutePath(); 1275 String retval = new File(testDir, "/").getAbsolutePath(); 1276 1277 return (retval); 1189 1278 1190 1279 } -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/RssCreatorCommentsTest.java
r2259 r2268 90 90 91 91 92 RssCreatorComments instance = new RssCreatorComments( hrefPrefix);92 RssCreatorComments instance = new RssCreatorComments(); 93 93 assertEquals(Double.toString(instance.getVersion()) , "2.0"); // check if the default version is set properly 94 94 95 instance.setFlagIsFromProfile(isFromProfile);95 96 96 instance.setVersion(3.0); 97 97 -
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/RssCreatorDescriptionsTest.java
r2256 r2268 48 48 private void createTestDescription(AbstractDescription desc, int commentcount, String creatorname, 49 49 String description, String domainname, String groupname, 50 String name, String date, String id) {50 String name, String date, String href, String id) { 51 51 52 52 desc.setId(id); … … 58 58 desc.setName(name); 59 59 desc.setRegistrationDate(date); 60 desc.setHref( null);60 desc.setHref(href+id); 61 61 62 62 } … … 64 64 private ProfileDescription createTestProfileDescription(int commentcount, String creatorname, 65 65 String description, String domainname, String groupname, 66 String name, boolean editorFlag, String date, String id) {66 String name, boolean editorFlag, String date, String href, String id) { 67 67 68 68 ProfileDescription pdesc = ProfileDescription.createNewDescription(); 69 69 70 createTestDescription(pdesc, commentcount, creatorname, description, domainname, groupname, name, date, id);70 createTestDescription(pdesc, commentcount, creatorname, description, domainname, groupname, name, date, href, id); 71 71 72 72 pdesc.setShowInEditor(editorFlag); … … 79 79 private ComponentDescription createTestComponentDescription(int commentcount, String creatorname, 80 80 String description, String domainname, String groupname, 81 String name, String date, String id) {81 String name, String date, String href, String id) { 82 82 83 83 ComponentDescription cdesc = ComponentDescription.createNewDescription(); 84 84 85 createTestDescription(cdesc, commentcount, creatorname, description, domainname, groupname, name, date, id);85 createTestDescription(cdesc, commentcount, creatorname, description, domainname, groupname, name, date, href, id); 86 86 87 87 return cdesc; … … 94 94 95 95 assertEquals(description, item.getDescription()); 96 assertEquals(href, item.getGuid().getValue() );96 //assertEquals(href, item.getGuid().getValue() ); 97 97 assertEquals(href, item.getLink()); 98 98 assertEquals(date, item.getPubDate()); … … 108 108 109 109 110 String hrefPrefix = "http://catalog.clarin.eu/ds/ComponentRegistry/";111 String href Infix="?item=";110 String link = "http://catalog.clarin.eu/ds/ComponentRegistry/profiles"; 111 String href = link+"?item="; 112 112 113 113 ProfileDescription desc1 = createTestProfileDescription(23, "Useratti", 114 "description-1", "domainname-1", "groupname-1", "name-1", true, "2001-01-01", "p_1");114 "description-1", "domainname-1", "groupname-1", "name-1", true, "2001-01-01", href,"p_1"); 115 115 116 116 ProfileDescription desc2 = createTestProfileDescription(23, "Usereno", 117 "description-2", "domainname-2", "groupname-2", "name-2", false, "2001-01-02", "p_2");117 "description-2", "domainname-2", "groupname-2", "name-2", false, "2001-01-02", href, "p_2"); 118 118 119 119 ProfileDescription desc3 = createTestProfileDescription(23, "Userio", 120 "description-3", "domainname-3", "groupname-3", "name-3", true, "2001-01-03", "p_3");120 "description-3", "domainname-3", "groupname-3", "name-3", true, "2001-01-03", href, "p_3"); 121 121 122 122 List<ProfileDescription> descriptions = Arrays.asList(desc1, desc2, desc3); 123 123 124 RssCreatorDescriptions instance = new RssCreatorDescriptions( hrefPrefix);124 RssCreatorDescriptions instance = new RssCreatorDescriptions(); 125 125 126 126 … … 129 129 instance.setVersion(2.0); 130 130 131 instance.setLink( null);131 instance.setLink(link); 132 132 instance.setCategory(null); 133 133 instance.setCloud(null); … … 158 158 // String creatorname, String description, String href, String date, String nametitle, RssItem item 159 159 160 String href = hrefPrefix+hrefInfix;160 161 161 162 162 compareRssVsValues("description-1", href+"p_1", "2001-01-01", … … 182 182 assertEquals("engl", result.getChannel().getLanguage()); 183 183 assertEquals("today", result.getChannel().getLastBuildDate()); 184 assertEquals( null, result.getChannel().getLink());184 assertEquals(link, result.getChannel().getLink()); 185 185 assertEquals("twan", result.getChannel().getManagingEditor()); 186 186 assertEquals("publication date", result.getChannel().getPubDate());
Note: See TracChangeset
for help on using the changeset viewer.