Changeset 1352
- Timestamp:
- 05/23/11 08:34:22 (13 years ago)
- Location:
- ComponentRegistry/trunk/ComponentRegistry/src
- Files:
-
- 1 added
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/ComponentRegistry.java
r1342 r1352 15 15 public static final String REGISTRY_ID = "clarin.eu:cr1:"; 16 16 17 List<ComponentDescription> getComponentDescriptions() ;17 List<ComponentDescription> getComponentDescriptions() throws ComponentRegistryException; 18 18 19 ComponentDescription getComponentDescription(String id) ;19 ComponentDescription getComponentDescription(String id) throws ComponentRegistryException; 20 20 21 List<ProfileDescription> getProfileDescriptions() ;21 List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException; 22 22 23 ProfileDescription getProfileDescription(String id) ;23 ProfileDescription getProfileDescription(String id) throws ComponentRegistryException; 24 24 25 CMDComponentSpec getMDProfile(String id) ;25 CMDComponentSpec getMDProfile(String id) throws ComponentRegistryException; 26 26 27 CMDComponentSpec getMDComponent(String id) ;27 CMDComponentSpec getMDComponent(String id) throws ComponentRegistryException; 28 28 29 29 /** … … 45 45 int publish(AbstractDescription desc, CMDComponentSpec spec, Principal principal); 46 46 47 void getMDProfileAsXml(String profileId, OutputStream output) ;47 void getMDProfileAsXml(String profileId, OutputStream output) throws ComponentRegistryException; 48 48 49 void getMDProfileAsXsd(String profileId, OutputStream outputStream) ;49 void getMDProfileAsXsd(String profileId, OutputStream outputStream) throws ComponentRegistryException; 50 50 51 void getMDComponentAsXml(String componentId, OutputStream output) ;51 void getMDComponentAsXml(String componentId, OutputStream output) throws ComponentRegistryException; 52 52 53 void getMDComponentAsXsd(String componentId, OutputStream outputStream) ;53 void getMDComponentAsXsd(String componentId, OutputStream outputStream) throws ComponentRegistryException; 54 54 55 55 /** … … 61 61 * @throws DeleteFailedException 62 62 */ 63 void deleteMDProfile(String profileId, Principal principal) throws IOException, UserUnauthorizedException, DeleteFailedException;63 void deleteMDProfile(String profileId, Principal principal) throws IOException, UserUnauthorizedException, ComponentRegistryException, DeleteFailedException; 64 64 65 65 /** … … 72 72 * @throws DeleteFailedException 73 73 */ 74 void deleteMDComponent(String componentId, Principal principal, boolean forceDelete) throws IOException, UserUnauthorizedException,74 void deleteMDComponent(String componentId, Principal principal, boolean forceDelete) throws IOException, ComponentRegistryException, UserUnauthorizedException, 75 75 DeleteFailedException; 76 76 … … 80 80 * @return List of ComponentDescriptions of Components that use the given Component. 81 81 */ 82 List<ComponentDescription> getUsageInComponents(String componentId) ;82 List<ComponentDescription> getUsageInComponents(String componentId) throws ComponentRegistryException; 83 83 84 84 /** … … 87 87 * @return List of ProfileDescriptions of Profiles that use the given Component. 88 88 */ 89 List<ProfileDescription> getUsageInProfiles(String componentId) ;89 List<ProfileDescription> getUsageInProfiles(String componentId) throws ComponentRegistryException; 90 90 91 91 /** -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/DeleteFailedException.java
r472 r1352 1 1 package clarin.cmdi.componentregistry; 2 2 3 public class DeleteFailedException extends Exception {3 public class DeleteFailedException extends ComponentRegistryException { 4 4 5 5 public DeleteFailedException(String message) { -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/frontend/AdminHomePage.java
r1342 r1352 1 1 package clarin.cmdi.componentregistry.frontend; 2 2 3 import clarin.cmdi.componentregistry.ComponentRegistryException; 3 4 import java.io.File; 4 5 import java.security.Principal; … … 48 49 private ComponentRegistryFactory componentRegistryFactory; 49 50 50 public AdminHomePage(final PageParameters parameters) {51 public AdminHomePage(final PageParameters parameters) throws ComponentRegistryException { 51 52 super(parameters); 52 53 addLinks(); … … 242 243 } 243 244 244 private TreeModel createDBTreeModel() {245 private TreeModel createDBTreeModel() throws ComponentRegistryException { 245 246 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new DisplayDataNode("ComponentRegistry", false)); 246 247 DefaultMutableTreeNode publicNode = new DefaultMutableTreeNode(new DisplayDataNode("Public", false)); … … 259 260 } 260 261 261 private void add(DefaultMutableTreeNode parent, ComponentRegistry registry) {262 private void add(DefaultMutableTreeNode parent, ComponentRegistry registry) throws ComponentRegistryException { 262 263 DefaultMutableTreeNode componentsNode = new DefaultMutableTreeNode(new DisplayDataNode("Components", false)); 263 264 parent.add(componentsNode); -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/frontend/MassMigratePage.java
r1342 r1352 1 1 package clarin.cmdi.componentregistry.frontend; 2 2 3 import clarin.cmdi.componentregistry.ComponentRegistryException; 3 4 import java.io.ByteArrayOutputStream; 4 5 import java.io.Serializable; … … 6 7 import java.util.ArrayList; 7 8 import java.util.List; 9 import java.util.logging.Level; 8 10 9 11 import javax.xml.bind.JAXBException; … … 41 43 @SuppressWarnings("serial") 42 44 public class MassMigratePage extends SecureAdminWebPage { 45 43 46 private final static Logger LOG = LoggerFactory.getLogger(MassMigratePage.class); 44 45 47 private FeedbackPanel feedback; 46 47 48 @SpringBean(name = "fileRegistryFactory") 48 49 private ComponentRegistryFactoryImpl fileRegistryFactory; … … 53 54 @SpringBean 54 55 private UserDao userDao; 55 56 56 private transient UserMapping userMap; 57 57 58 58 public MassMigratePage(final PageParameters pageParameters) { 59 super(pageParameters); 60 userMap = fileRegistryFactory.getUserMap(); 61 addLinks(); 62 feedback = new FeedbackPanel("feedback") { 63 protected Component newMessageDisplayComponent(String id, FeedbackMessage message) { 64 Serializable serializable = message.getMessage(); 65 MultiLineLabel label = new MultiLineLabel(id, (serializable == null) ? "" : serializable.toString()); 66 label.setEscapeModelStrings(getEscapeModelStrings()); 67 return label; 68 } 69 }; 70 feedback.setOutputMarkupPlaceholderTag(true); 71 add(feedback); 72 addMigrationOptions(); 59 super(pageParameters); 60 userMap = fileRegistryFactory.getUserMap(); 61 addLinks(); 62 feedback = new FeedbackPanel("feedback") { 63 64 protected Component newMessageDisplayComponent(String id, FeedbackMessage message) { 65 Serializable serializable = message.getMessage(); 66 MultiLineLabel label = new MultiLineLabel(id, (serializable == null) ? "" : serializable.toString()); 67 label.setEscapeModelStrings(getEscapeModelStrings()); 68 return label; 69 } 70 }; 71 feedback.setOutputMarkupPlaceholderTag(true); 72 add(feedback); 73 addMigrationOptions(); 73 74 } 74 75 75 76 private void addLinks() { 76 add(new Label("linksMessage", "Do some mass migration or go to:")); 77 add(new Link("home") { 78 @Override 79 public void onClick() { 80 setResponsePage(AdminHomePage.class); 81 } 82 }); 77 add(new Label("linksMessage", "Do some mass migration or go to:")); 78 add(new Link("home") { 79 80 @Override 81 public void onClick() { 82 setResponsePage(AdminHomePage.class); 83 } 84 }); 83 85 } 84 86 85 87 private void addMigrationOptions() { 86 add(new Label("migrate1Label", "Click here to start the migration of the file storage into the database.")); 87 add(new IndicatingAjaxLink("migrate1") { 88 public void onClick(final AjaxRequestTarget target) { 89 if (target != null) { 90 target.addComponent(feedback); 91 } 92 startMigration(); 93 } 94 }); 88 add(new Label("migrate1Label", "Click here to start the migration of the file storage into the database.")); 89 add(new IndicatingAjaxLink("migrate1") { 90 91 public void onClick(final AjaxRequestTarget target) { 92 if (target != null) { 93 target.addComponent(feedback); 94 } 95 startMigration(); 96 } 97 }); 95 98 } 96 99 97 100 private void startMigration() { 98 info("Start Migration users..."); 99 List<User> users = userMap.getUsers(); 100 for (User user : users) { 101 userDao.insertUser(user); 102 } 103 info("Start Migration descriptions and content..."); 104 List<ComponentRegistry> registries = new ArrayList<ComponentRegistry>(); 105 registries.add(fileRegistryFactory.getPublicRegistry()); 106 registries.addAll(fileRegistryFactory.getAllUserRegistries()); 107 for (ComponentRegistry registry : registries) { 108 migrateDescriptions(registry.getComponentDescriptions(), registry, componentDescriptionDao); 109 migrateDescriptions(registry.getProfileDescriptions(), registry, profileDescriptionDao); 110 } 111 info("End Migration."); 101 info("Start Migration users..."); 102 List<User> users = userMap.getUsers(); 103 for (User user : users) { 104 userDao.insertUser(user); 105 } 106 info("Start Migration descriptions and content..."); 107 List<ComponentRegistry> registries = new ArrayList<ComponentRegistry>(); 108 registries.add(fileRegistryFactory.getPublicRegistry()); 109 registries.addAll(fileRegistryFactory.getAllUserRegistries()); 110 for (ComponentRegistry registry : registries) { 111 try { 112 migrateDescriptions(registry.getComponentDescriptions(), registry, componentDescriptionDao); 113 migrateDescriptions(registry.getProfileDescriptions(), registry, profileDescriptionDao); 114 } catch (ComponentRegistryException e) { 115 LOG.error("Error in migration, check the logs!", e); 116 info("Error cannot retrieve descriptions from registry " + registry.getName()); 117 } 118 } 119 info("End Migration."); 112 120 } 113 121 114 122 private void migrateDescriptions(List<? extends AbstractDescription> descs, ComponentRegistry registry, AbstractDescriptionDao descDao) { 115 116 117 118 119 120 121 122 123 124 125 126 123 int migrateCount = 0; 124 for (AbstractDescription description : descs) { 125 try { 126 User user = userDao.getByPrincipalName(userMap.findUser(description.getUserId()).getPrincipalName()); 127 descDao.insertDescription(description, getContent(description, registry), registry.isPublic(), user.getId()); 128 } catch (Exception e) { 129 LOG.error("Error in migration, check the logs!", e); 130 info("Error cannot migrate " + description.getId()); 131 } 132 migrateCount++; 133 } 134 LOG.info(registry.getName() + " migrated: " + migrateCount + " out of " + descs.size() + " descs"); 127 135 } 128 136 129 137 private String getContent(AbstractDescription description, ComponentRegistry registry) throws UnsupportedEncodingException, 130 JAXBException { 131 ByteArrayOutputStream out = new ByteArrayOutputStream(); 132 CMDComponentSpec spec = null; 133 if (description.isProfile()) { 134 spec = registry.getMDProfile(description.getId()); 135 } else { 136 spec = registry.getMDComponent(description.getId()); 137 } 138 MDMarshaller.marshal(spec, out); 139 return out.toString(); 138 JAXBException, 139 ComponentRegistryException { 140 ByteArrayOutputStream out = new ByteArrayOutputStream(); 141 CMDComponentSpec spec = null; 142 if (description.isProfile()) { 143 spec = registry.getMDProfile(description.getId()); 144 } else { 145 spec = registry.getMDComponent(description.getId()); 146 } 147 MDMarshaller.marshal(spec, out); 148 return out.toString(); 140 149 } 141 142 150 } -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/frontend/StatisticsPage.java
r1334 r1352 2 2 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 4 5 import clarin.cmdi.componentregistry.impl.filesystem.ComponentRegistryFactoryImpl; 5 6 import clarin.cmdi.componentregistry.components.CMDComponentSpec; … … 33 34 } 34 35 35 public StatisticsPage(final PageParameters pageParameters) throws IOException {36 public StatisticsPage(final PageParameters pageParameters) throws IOException, ComponentRegistryException { 36 37 super(pageParameters); 37 38 addLinks(); … … 39 40 } 40 41 41 private void DisplayStatistics() {42 private void DisplayStatistics() throws ComponentRegistryException { 42 43 List<ProfileDescription> profileList = registry.getProfileDescriptions(); 43 44 RepeatingView repeating = new RepeatingView("repeating"); -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentRegistryImplBase.java
r1343 r1352 2 2 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 4 5 import clarin.cmdi.componentregistry.DeleteFailedException; 5 6 import clarin.cmdi.componentregistry.MDMarshaller; … … 26 27 * @author Twan Goosen <twan.goosen@mpi.nl> 27 28 */ 28 public abstract class ComponentRegistryImplBase implements ComponentRegistry{ 29 private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryImplBase.class); 29 public abstract class ComponentRegistryImplBase implements ComponentRegistry { 30 31 private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryImplBase.class); 30 32 31 33 @Override 32 public List<ComponentDescription> getUsageInComponents(String componentId) {34 public List<ComponentDescription> getUsageInComponents(String componentId) throws ComponentRegistryException { 33 35 List<ComponentDescription> result = new ArrayList<ComponentDescription>(); 34 36 List<ComponentDescription> descs = getComponentDescriptions(); … … 43 45 44 46 @Override 45 public List<ProfileDescription> getUsageInProfiles(String componentId) {47 public List<ProfileDescription> getUsageInProfiles(String componentId) throws ComponentRegistryException { 46 48 List<ProfileDescription> result = new ArrayList<ProfileDescription>(); 47 49 for (ProfileDescription profileDescription : getProfileDescriptions()) { … … 55 57 56 58 /* HELPER METHODS */ 57 58 59 protected static String stripRegistryId(String id) { 59 60 return StringUtils.removeStart(id, ComponentRegistry.REGISTRY_ID); 60 61 } 61 62 62 63 protected static void enrichSpecHeader(CMDComponentSpec spec, AbstractDescription description) { 63 64 65 66 67 68 69 70 64 Header header = spec.getHeader(); 65 header.setID(description.getId()); 66 if (StringUtils.isEmpty(header.getName())) { 67 header.setName(description.getName()); 68 } 69 if (StringUtils.isEmpty(header.getDescription())) { 70 header.setDescription(description.getDescription()); 71 } 71 72 } 72 73 73 74 protected static boolean findComponentId(String componentId, List<CMDComponentType> componentReferences) { 74 75 76 77 78 79 80 81 75 for (CMDComponentType cmdComponent : componentReferences) { 76 if (componentId.equals(cmdComponent.getComponentId())) { 77 return true; 78 } else if (findComponentId(componentId, cmdComponent.getCMDComponent())) { 79 return true; 80 } 81 } 82 return false; 82 83 } 83 84 84 85 protected static void writeXsd(CMDComponentSpec expandedSpec, OutputStream outputStream) { 85 86 MDMarshaller.generateXsd(expandedSpec, outputStream); 86 87 } 87 88 88 89 protected static void writeXml(CMDComponentSpec spec, OutputStream outputStream) { 89 90 91 92 93 94 95 90 try { 91 MDMarshaller.marshal(spec, outputStream); 92 } catch (UnsupportedEncodingException e) { 93 LOG.error("Error in encoding: ", e); 94 } catch (JAXBException e) { 95 LOG.error("Cannot marshall spec: " + spec, e); 96 } 96 97 } 97 98 98 protected void checkStillUsed(String componentId) throws DeleteFailedException {99 100 101 102 103 99 protected void checkStillUsed(String componentId) throws DeleteFailedException, ComponentRegistryException { 100 List<ProfileDescription> profiles = getUsageInProfiles(componentId); 101 List<ComponentDescription> components = getUsageInComponents(componentId); 102 if (!profiles.isEmpty() || !components.isEmpty()) { 103 throw new DeleteFailedException(createStillInUseMessage(profiles, components)); 104 } 104 105 } 105 106 106 107 private String createStillInUseMessage(List<ProfileDescription> profiles, List<ComponentDescription> components) { 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 108 StringBuilder result = new StringBuilder(); 109 if (!profiles.isEmpty()) { 110 result.append("Still used by the following profiles: \n"); 111 for (ProfileDescription profileDescription : profiles) { 112 result.append(" - ").append(profileDescription.getName()).append("\n"); 113 } 114 } 115 if (!components.isEmpty()) { 116 result.append("Still used by the following components: \n"); 117 for (ComponentDescription componentDescription : components) { 118 result.append(" - ").append(componentDescription.getName()).append("\n"); 119 } 120 } 121 result.append("Try to change above mentioned references first."); 122 return result.toString(); 122 123 } 123 124 124 125 /* UNIMPLEMENTED INTERFACE METHODS */ 126 @Override 127 public abstract List<ComponentDescription> getComponentDescriptions() throws ComponentRegistryException; 125 128 126 129 @Override 127 public abstract List<ComponentDescription> getComponentDescriptions();130 public abstract ComponentDescription getComponentDescription(String id) throws ComponentRegistryException; 128 131 129 132 @Override 130 public abstract ComponentDescription getComponentDescription(String id);133 public abstract List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException; 131 134 132 135 @Override 133 public abstract List<ProfileDescription> getProfileDescriptions();136 public abstract ProfileDescription getProfileDescription(String id) throws ComponentRegistryException; 134 137 135 138 @Override 136 public abstract ProfileDescription getProfileDescription(String id);139 public abstract CMDComponentSpec getMDProfile(String id) throws ComponentRegistryException; 137 140 138 141 @Override 139 public abstract CMDComponentSpec getMDProfile(String id); 140 141 @Override 142 public abstract CMDComponentSpec getMDComponent(String id); 142 public abstract CMDComponentSpec getMDComponent(String id) throws ComponentRegistryException; 143 143 144 144 @Override … … 146 146 147 147 @Override 148 public abstract 148 public abstract int update(AbstractDescription description, CMDComponentSpec spec); 149 149 150 150 @Override 151 public abstract 151 public abstract int publish(AbstractDescription desc, CMDComponentSpec spec, Principal principal); 152 152 153 153 @Override 154 public abstract void getMDProfileAsXml(String profileId, OutputStream output);154 public abstract void getMDProfileAsXml(String profileId, OutputStream output) throws ComponentRegistryException; 155 155 156 156 @Override 157 public abstract void getMDProfileAsXsd(String profileId, OutputStream outputStream);157 public abstract void getMDProfileAsXsd(String profileId, OutputStream outputStream) throws ComponentRegistryException; 158 158 159 159 @Override 160 public abstract void getMDComponentAsXml(String componentId, OutputStream output);160 public abstract void getMDComponentAsXml(String componentId, OutputStream output) throws ComponentRegistryException; 161 161 162 162 @Override 163 public abstract void getMDComponentAsXsd(String componentId, OutputStream outputStream);163 public abstract void getMDComponentAsXsd(String componentId, OutputStream outputStream) throws ComponentRegistryException; 164 164 165 165 @Override 166 public abstract void deleteMDProfile(String profileId, Principal principal) throws IOException, UserUnauthorizedException, DeleteFailedException;166 public abstract void deleteMDProfile(String profileId, Principal principal) throws IOException, UserUnauthorizedException, DeleteFailedException, ComponentRegistryException; 167 167 168 168 @Override 169 public abstract void deleteMDComponent(String componentId, Principal principal, boolean forceDelete) throws IOException, UserUnauthorizedException, DeleteFailedException;169 public abstract void deleteMDComponent(String componentId, Principal principal, boolean forceDelete) throws IOException, UserUnauthorizedException, DeleteFailedException, ComponentRegistryException; 170 170 171 171 @Override 172 public abstract boolean isPublic(); 173 172 public abstract boolean isPublic(); 174 173 } -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
r1346 r1352 2 2 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 4 5 import clarin.cmdi.componentregistry.Configuration; 5 6 import clarin.cmdi.componentregistry.DeleteFailedException; … … 40 41 private Number userId; 41 42 @Autowired 43 private Configuration configuration; 44 @Autowired 42 45 private ProfileDescriptionDao profileDescriptionDao; 43 46 @Autowired … … 46 49 private UserDao userDao; 47 50 @Autowired 48 Configuration configuration;49 @Autowired50 51 @Qualifier("componentsCache") 51 CMDComponentSpecCache componentsCache;52 private CMDComponentSpecCache componentsCache; 52 53 @Autowired 53 54 @Qualifier("profilesCache") 54 CMDComponentSpecCache profilesCache;55 private CMDComponentSpecCache profilesCache; 55 56 56 57 /** … … 71 72 72 73 @Override 73 public List<ProfileDescription> getProfileDescriptions() {74 public List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException { 74 75 try { 75 76 if (isPublic()) { … … 79 80 } 80 81 } catch (DataAccessException ex) { 81 LOG.error("Database access error while trying to get profile descriptions", ex); 82 throw ex; 83 } 84 } 85 86 @Override 87 public ProfileDescription getProfileDescription(String id) { 82 throw new ComponentRegistryException("Database access error while trying to get profile descriptions", ex); 83 } 84 } 85 86 @Override 87 public ProfileDescription getProfileDescription(String id) throws ComponentRegistryException { 88 88 try { 89 89 return profileDescriptionDao.getByCmdId(id, getUserId()); 90 90 } catch (DataAccessException ex) { 91 LOG.error("Database access error while trying to get profile description", ex); 92 throw ex; 93 } 94 } 95 96 @Override 97 public List<ComponentDescription> getComponentDescriptions() { 91 throw new ComponentRegistryException("Database access error while trying to get profile description", ex); 92 } 93 } 94 95 @Override 96 public List<ComponentDescription> getComponentDescriptions() throws ComponentRegistryException { 98 97 try { 99 98 if (isPublic()) { … … 103 102 } 104 103 } catch (DataAccessException ex) { 105 LOG.error("Database access error while trying to get component descriptions", ex); 106 throw ex; 107 } 108 } 109 110 @Override 111 public ComponentDescription getComponentDescription(String id) { 104 throw new ComponentRegistryException("Database access error while trying to get component descriptions", ex); 105 } 106 } 107 108 @Override 109 public ComponentDescription getComponentDescription(String id) throws ComponentRegistryException { 112 110 try { 113 111 return componentDescriptionDao.getByCmdId(id, getUserId()); 114 112 } catch (DataAccessException ex) { 115 LOG.error("Database access error while trying to get component description", ex); 116 throw ex; 117 } 118 } 119 120 @Override 121 public CMDComponentSpec getMDProfile(String id) { 113 throw new ComponentRegistryException("Database access error while trying to get component description", ex); 114 } 115 } 116 117 @Override 118 public CMDComponentSpec getMDProfile(String id) throws ComponentRegistryException { 122 119 if (inWorkspace(profileDescriptionDao, id)) { 123 120 CMDComponentSpec result = profilesCache.get(id); … … 128 125 return result; 129 126 } else { 127 // May exist, but not in this workspace 130 128 return null; 131 129 } 132 130 } 133 131 134 public CMDComponentSpec getUncachedMDProfile(String id) {132 public CMDComponentSpec getUncachedMDProfile(String id) throws ComponentRegistryException { 135 133 try { 136 134 return getUncachedMDComponent(id, profileDescriptionDao); 137 135 } catch (DataAccessException ex) { 138 LOG.error("Database access error while trying to get profile", ex); 139 throw ex; 140 } 141 } 142 143 @Override 144 public CMDComponentSpec getMDComponent(String id) { 136 throw new ComponentRegistryException("Database access error while trying to get profile", ex); 137 } 138 } 139 140 @Override 141 public CMDComponentSpec getMDComponent(String id) throws ComponentRegistryException { 145 142 if (inWorkspace(componentDescriptionDao, id)) { 146 143 CMDComponentSpec result = componentsCache.get(id); … … 155 152 } 156 153 157 public CMDComponentSpec getUncachedMDComponent(String id) {154 public CMDComponentSpec getUncachedMDComponent(String id) throws ComponentRegistryException { 158 155 try { 159 156 return getUncachedMDComponent(id, componentDescriptionDao); 160 157 } catch (DataAccessException ex) { 161 LOG.error("Database access error while trying to get component", ex); 162 throw ex; 158 throw new ComponentRegistryException("Database access error while trying to get component", ex); 163 159 } 164 160 } … … 174 170 invalidateCache(description); 175 171 return 0; 172 } catch (DataAccessException ex) { 173 LOG.error("Database error while registering component", ex); 174 return -1; 176 175 } catch (JAXBException ex) { 177 LOG.error(null, ex); 176 LOG.error("Error while registering component", ex); 177 return -2; 178 178 } catch (UnsupportedEncodingException ex) { 179 LOG.error(null, ex); 180 } catch (DataAccessException ex) { 181 LOG.error("Database error while registering component", ex); 182 } 183 return -1; 179 LOG.error("Error while registering component", ex); 180 return -3; 181 } 184 182 } 185 183 … … 216 214 return 0; 217 215 } catch (DataAccessException ex) { 218 LOG.error( null, ex);216 LOG.error("Database error while updating component", ex); 219 217 return -1; 218 } catch (JAXBException ex) { 219 LOG.error("Error while updating component", ex); 220 return -2; 221 } catch (UnsupportedEncodingException ex) { 222 LOG.error("Error while updating component", ex); 223 return -3; 220 224 } catch (IllegalArgumentException ex) { 221 LOG.error(null, ex); 222 return -2; 223 } catch (JAXBException ex) { 224 LOG.error(null, ex); 225 return -3; 226 } catch (UnsupportedEncodingException ex) { 227 LOG.error(null, ex); 225 LOG.error("Error while updating component", ex); 228 226 return -4; 229 227 } … … 235 233 AbstractDescriptionDao<?> dao = getDaoForDescription(desc); 236 234 if (!isPublic()) { //if already in public workspace there is nothing todo 235 desc.setHref(AbstractDescription.createPublicHref(desc.getHref())); 236 Number id = getIdForDescription(desc); 237 237 try { 238 desc.setHref(AbstractDescription.createPublicHref(desc.getHref()));239 Number id = getIdForDescription(desc);240 238 // Update description & content 241 239 dao.updateDescription(id, desc, componentSpecToString(spec)); 242 240 // Set to public 243 241 dao.setPublished(id, true); 244 } catch (Exception e) { 245 LOG.error("Delete failed:", e); 246 result = -1; 242 } catch (DataAccessException ex) { 243 LOG.error("Database error while updating component", ex); 244 return -1; 245 } catch (JAXBException ex) { 246 LOG.error("Error while updating component", ex); 247 return -2; 248 } catch (UnsupportedEncodingException ex) { 249 LOG.error("Error while updating component", ex); 250 return -3; 247 251 } 248 252 } … … 252 256 @Override 253 257 public void getMDProfileAsXml(String profileId, OutputStream output) { 254 CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl. 255 expandProfile(profileId, this); 258 CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandProfile(profileId, this); 256 259 writeXml(expandedSpec, output); 257 260 } … … 259 262 @Override 260 263 public void getMDProfileAsXsd(String profileId, OutputStream outputStream) { 261 CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl. 262 expandProfile(profileId, this); 264 CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandProfile(profileId, this); 263 265 writeXsd(expandedSpec, outputStream); 264 266 } … … 266 268 @Override 267 269 public void getMDComponentAsXml(String componentId, OutputStream output) { 268 CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl. 269 expandComponent(componentId, this); 270 CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandComponent(componentId, this); 270 271 writeXml(expandedSpec, output); 271 272 } … … 273 274 @Override 274 275 public void getMDComponentAsXsd(String componentId, OutputStream outputStream) { 275 CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl. 276 expandComponent(componentId, this); 276 CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandComponent(componentId, this); 277 277 writeXsd(expandedSpec, outputStream); 278 278 } 279 279 280 280 @Override 281 public void deleteMDProfile(String profileId, Principal principal) throws IOException, UserUnauthorizedException, DeleteFailedException {282 try {283 ProfileDescription desc = getProfileDescription(profileId);284 if (desc != null){281 public void deleteMDProfile(String profileId, Principal principal) throws IOException, UserUnauthorizedException, DeleteFailedException, ComponentRegistryException { 282 ProfileDescription desc = getProfileDescription(profileId); 283 if (desc != null) { 284 try { 285 285 checkAuthorisation(desc, principal); 286 286 checkAge(desc, principal); 287 287 profileDescriptionDao.setDeleted(getIdForDescription(desc)); 288 288 invalidateCache(desc); 289 } 290 } catch (DataAccessException ex) { 291 LOG.error("Database access error while trying to delete profile", ex); 292 throw ex; 293 } 294 295 } 296 297 @Override 298 public void deleteMDComponent(String componentId, Principal principal, boolean forceDelete) throws IOException, UserUnauthorizedException, DeleteFailedException { 299 try { 300 ComponentDescription desc = componentDescriptionDao.getByCmdId(componentId); 301 if (desc != null) { 289 } catch (DataAccessException ex) { 290 throw new DeleteFailedException("Database access error while trying to delete profile", ex); 291 } 292 } 293 } 294 295 @Override 296 public void deleteMDComponent(String componentId, Principal principal, boolean forceDelete) throws IOException, UserUnauthorizedException, DeleteFailedException, ComponentRegistryException { 297 ComponentDescription desc = componentDescriptionDao.getByCmdId(componentId); 298 if (desc != null) { 299 try { 302 300 checkAuthorisation(desc, principal); 303 301 checkAge(desc, principal); … … 308 306 componentDescriptionDao.setDeleted(getIdForDescription(desc)); 309 307 invalidateCache(desc); 310 } 311 } catch (DataAccessException ex) { 312 LOG.error("Database access error while trying to delete component", ex); 313 throw ex; 308 } catch (DataAccessException ex) { 309 throw new DeleteFailedException("Database access error while trying to delete component", ex); 310 } 314 311 } 315 312 } … … 386 383 try { 387 384 InputStream is = new ByteArrayInputStream(xml.getBytes()); 388 return MDMarshaller.unmarshal(CMDComponentSpec.class, is, MDMarshaller. 389 getCMDComponentSchema()); 385 return MDMarshaller.unmarshal(CMDComponentSpec.class, is, MDMarshaller.getCMDComponentSchema()); 390 386 } catch (JAXBException ex) { 391 387 LOG.error(null, ex); … … 398 394 if (!isOwnerOfDescription(desc, principal.getName()) 399 395 && !configuration.isAdminUser(principal)) { 400 throw new UserUnauthorizedException("Unauthorized operation user '" + principal. 401 getName() 402 + "' is not the creator (nor an administrator) of the " + (desc. 403 isProfile() ? "profile" : "component") + "(" + desc 396 throw new UserUnauthorizedException("Unauthorized operation user '" + principal.getName() 397 + "' is not the creator (nor an administrator) of the " + (desc.isProfile() ? "profile" : "component") + "(" + desc 404 398 + ")."); 405 399 } … … 415 409 if (isPublic() && !configuration.isAdminUser(principal)) { 416 410 try { 417 Date regDate = AbstractDescription.getDate(desc. 418 getRegistrationDate()); 411 Date regDate = AbstractDescription.getDate(desc.getRegistrationDate()); 419 412 Calendar calendar = Calendar.getInstance(); 420 413 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); … … 431 424 } 432 425 433 434 426 private boolean inWorkspace(AbstractDescriptionDao<?> dao, String cmdId) { 435 427 if (isPublic()) { -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/filesystem/AdminRegistry.java
r1342 r1352 1 1 package clarin.cmdi.componentregistry.impl.filesystem; 2 2 3 import clarin.cmdi.componentregistry.ComponentRegistryException; 3 4 import java.io.File; 4 5 import java.io.FileInputStream; … … 27 28 28 29 public class AdminRegistry { 30 29 31 private final static Logger LOG = LoggerFactory.getLogger(AdminRegistry.class); 30 32 31 33 public void submitFile(FileInfo fileInfo, Principal userPrincipal) throws SubmitFailedException { 32 try { 33 File file = getFile(fileInfo); 34 if (fileInfo.getDisplayNode().isDeleted()) { 35 //already deleted file 36 FileUtils.writeStringToFile(file, fileInfo.getText(), "UTF-8"); 37 } else { 38 //Description or cmdSpec file. 39 String name = fileInfo.getName(); 40 String id = ComponentRegistry.REGISTRY_ID + getFile(fileInfo).getParentFile().getName(); 41 AbstractDescription originalDescription = getDescription(fileInfo); 42 CMDComponentSpec originalSpec = getSpec(fileInfo); 43 AbstractDescription description = null; 44 CMDComponentSpec spec = null; 45 if (ComponentRegistryImpl.DESCRIPTION_FILE_NAME.equals(name)) { 46 if (getFile(fileInfo).getParentFile().getName().startsWith("c_")) { 47 description = MDMarshaller.unmarshal(ComponentDescription.class, 48 IOUtils.toInputStream(fileInfo.getText(), "UTF-8"), null); 49 } else { 50 description = MDMarshaller.unmarshal(ProfileDescription.class, IOUtils.toInputStream(fileInfo.getText(), "UTF-8"), 51 null); 52 } 53 checkId(id, description.getId()); 54 spec = originalSpec; 55 } else { 56 spec = MDMarshaller.unmarshal(CMDComponentSpec.class, IOUtils.toInputStream(fileInfo.getText(), "UTF-8"), MDMarshaller 57 .getCMDComponentSchema()); 58 checkId(id, spec.getHeader().getID()); 59 description = originalDescription; 60 } 61 deleteFromRegistry(userPrincipal, originalDescription, fileInfo); 62 int result = submitToRegistry(description, spec, userPrincipal, fileInfo); 63 if (result == 0) { 64 //submit is successful so now really delete the old one, we cannot have that around anymore. 65 ComponentRegistryImpl registry = (ComponentRegistryImpl) getRegistry(userPrincipal, originalDescription, fileInfo); 66 registry.emptyFromTrashcan(originalDescription); 67 } else { 68 throw new SubmitFailedException("Problem occured while registering, please check the tomcat logs for errors. " 69 + "Original files are removed already you can find them " 70 + "in the deleted section of the registry. You have to put that back manually."); 71 } 72 } 73 } catch (JAXBException e) { 74 throw new SubmitFailedException(e); 75 } catch (IOException e) { 76 throw new SubmitFailedException(e); 77 } catch (UserUnauthorizedException e) { 78 throw new SubmitFailedException(e); 79 } catch (DeleteFailedException e) { 80 throw new SubmitFailedException(e); 81 } 34 try { 35 File file = getFile(fileInfo); 36 if (fileInfo.getDisplayNode().isDeleted()) { 37 //already deleted file 38 FileUtils.writeStringToFile(file, fileInfo.getText(), "UTF-8"); 39 } else { 40 //Description or cmdSpec file. 41 String name = fileInfo.getName(); 42 String id = ComponentRegistry.REGISTRY_ID + getFile(fileInfo).getParentFile().getName(); 43 AbstractDescription originalDescription = getDescription(fileInfo); 44 CMDComponentSpec originalSpec = getSpec(fileInfo); 45 AbstractDescription description = null; 46 CMDComponentSpec spec = null; 47 if (ComponentRegistryImpl.DESCRIPTION_FILE_NAME.equals(name)) { 48 if (getFile(fileInfo).getParentFile().getName().startsWith("c_")) { 49 description = MDMarshaller.unmarshal(ComponentDescription.class, 50 IOUtils.toInputStream(fileInfo.getText(), "UTF-8"), null); 51 } else { 52 description = MDMarshaller.unmarshal(ProfileDescription.class, IOUtils.toInputStream(fileInfo.getText(), "UTF-8"), 53 null); 54 } 55 checkId(id, description.getId()); 56 spec = originalSpec; 57 } else { 58 spec = MDMarshaller.unmarshal(CMDComponentSpec.class, IOUtils.toInputStream(fileInfo.getText(), "UTF-8"), MDMarshaller.getCMDComponentSchema()); 59 checkId(id, spec.getHeader().getID()); 60 description = originalDescription; 61 } 62 deleteFromRegistry(userPrincipal, originalDescription, fileInfo); 63 int result = submitToRegistry(description, spec, userPrincipal, fileInfo); 64 if (result == 0) { 65 //submit is successful so now really delete the old one, we cannot have that around anymore. 66 ComponentRegistryImpl registry = (ComponentRegistryImpl) getRegistry(userPrincipal, originalDescription, fileInfo); 67 registry.emptyFromTrashcan(originalDescription); 68 } else { 69 throw new SubmitFailedException("Problem occured while registering, please check the tomcat logs for errors. " 70 + "Original files are removed already you can find them " 71 + "in the deleted section of the registry. You have to put that back manually."); 72 } 73 } 74 } catch (JAXBException e) { 75 throw new SubmitFailedException(e); 76 } catch (IOException e) { 77 throw new SubmitFailedException(e); 78 } catch (UserUnauthorizedException e) { 79 throw new SubmitFailedException(e); 80 } catch (DeleteFailedException e) { 81 throw new SubmitFailedException(e); 82 } catch (ComponentRegistryException e) { 83 throw new SubmitFailedException(e); 84 } 82 85 83 86 } 84 87 85 88 private void checkId(String id, String id2) throws SubmitFailedException { 86 87 88 89 if (id == null || id2 == null || !id.equals(id2)) { 90 throw new SubmitFailedException("Id's do not match up, you cannot edit id's: id1=" + id + ", id2=" + id2); 91 } 89 92 } 90 93 91 94 public void undelete(FileInfo fileInfo, Principal userPrincipal) throws SubmitFailedException { 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 95 String id = fileInfo.getName(); 96 AbstractDescription desc = getDescription(fileInfo); 97 try { 98 CMDComponentSpec spec = getSpec(fileInfo); 99 int result = submitToRegistry(desc, spec, userPrincipal, fileInfo); 100 if (result == 0) { 101 FileUtils.deleteDirectory(getFile(fileInfo)); 102 LOG.info("Undeleted item: " + id); 103 } else { 104 throw new SubmitFailedException("Problem occured while registering, please check the tomcat logs for errors."); 105 } 106 } catch (IOException e) { 107 throw new SubmitFailedException(e); 108 } catch (JAXBException e) { 109 throw new SubmitFailedException(e); 110 } 108 111 109 112 } 110 113 111 114 public void delete(FileInfo fileInfo, Principal userPrincipal) throws SubmitFailedException { 112 String id = fileInfo.getName(); 113 AbstractDescription desc = getDescription(fileInfo); 114 try { 115 deleteFromRegistry(userPrincipal, desc, fileInfo); 116 LOG.info("Deleted item: " + id); 117 } catch (IOException e) { 118 throw new SubmitFailedException(e); 119 } catch (UserUnauthorizedException e) { 120 throw new SubmitFailedException(e); 121 } catch (DeleteFailedException e) { 122 throw new SubmitFailedException(e); 123 } 115 String id = fileInfo.getName(); 116 AbstractDescription desc = getDescription(fileInfo); 117 try { 118 deleteFromRegistry(userPrincipal, desc, fileInfo); 119 LOG.info("Deleted item: " + id); 120 } catch (IOException e) { 121 throw new SubmitFailedException(e); 122 } catch (UserUnauthorizedException e) { 123 throw new SubmitFailedException(e); 124 } catch (DeleteFailedException e) { 125 throw new SubmitFailedException(e); 126 } catch (ComponentRegistryException e) { 127 throw new SubmitFailedException(e); 128 } 124 129 125 130 } 126 131 127 132 private int submitToRegistry(AbstractDescription description, CMDComponentSpec spec, Principal userPrincipal, FileInfo fileInfo) { 128 129 130 131 132 133 133 ComponentRegistry registry = getRegistry(userPrincipal, description, fileInfo); 134 if (spec.isIsProfile()) { 135 return registry.register((ProfileDescription) description, spec); 136 } else { 137 return registry.register((ComponentDescription) description, spec); 138 } 134 139 135 140 } 136 141 137 142 private void deleteFromRegistry(Principal userPrincipal, AbstractDescription desc, FileInfo fileInfo) throws IOException, 138 UserUnauthorizedException, DeleteFailedException {139 140 141 142 143 144 145 143 UserUnauthorizedException, DeleteFailedException, ComponentRegistryException { 144 ComponentRegistry registry = getRegistry(userPrincipal, desc, fileInfo); 145 LOG.info("Deleting item: " + desc); 146 if (desc.isProfile()) { 147 registry.deleteMDProfile(desc.getId(), userPrincipal); 148 } else { 149 registry.deleteMDComponent(desc.getId(), userPrincipal, fileInfo.isForceUpdate()); 150 } 146 151 } 147 152 148 153 private ComponentRegistry getRegistry(Principal userPrincipal, AbstractDescription desc, FileInfo fileInfo) { 149 150 151 152 153 154 ComponentRegistry registry = ComponentRegistryFactoryImpl.getInstance().getPublicRegistry(); 155 if (fileInfo.isInUserWorkSpace()) { 156 registry = ComponentRegistryFactoryImpl.getInstance().getOtherUserComponentRegistry(userPrincipal, desc.getUserId()); 157 } 158 return registry; 154 159 } 155 160 156 161 private CMDComponentSpec getSpec(FileInfo fileInfo) throws FileNotFoundException, JAXBException { 157 File parent = getFile(fileInfo); 158 if (!parent.isDirectory()) { 159 parent = parent.getParentFile(); 160 } 161 File file = new File(parent, parent.getName() + ".xml"); 162 CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, new FileInputStream(file), MDMarshaller 163 .getCMDComponentSchema()); 164 return spec; 162 File parent = getFile(fileInfo); 163 if (!parent.isDirectory()) { 164 parent = parent.getParentFile(); 165 } 166 File file = new File(parent, parent.getName() + ".xml"); 167 CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, new FileInputStream(file), MDMarshaller.getCMDComponentSchema()); 168 return spec; 165 169 } 166 170 167 171 private AbstractDescription getDescription(FileInfo fileInfo) { 168 169 170 171 172 173 174 175 172 File parent = getFile(fileInfo); 173 if (!parent.isDirectory()) { 174 parent = parent.getParentFile(); 175 } 176 File descFile = new File(parent, ComponentRegistryImpl.DESCRIPTION_FILE_NAME); 177 Class<? extends AbstractDescription> clazz = fileInfo.isComponent() ? ComponentDescription.class : ProfileDescription.class; 178 AbstractDescription result = MDMarshaller.unmarshal(clazz, descFile, null); 179 return result; 176 180 } 177 181 178 182 private File getFile(FileInfo fileInfo) { 179 180 183 FileNode fileNode = (FileNode) fileInfo.getDisplayNode(); 184 return fileNode.getFile(); 181 185 } 182 183 186 } -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/filesystem/ComponentRegistryImpl.java
r1346 r1352 3 3 import clarin.cmdi.componentregistry.impl.ComponentRegistryImplBase; 4 4 import clarin.cmdi.componentregistry.ComponentRegistry; 5 import clarin.cmdi.componentregistry.ComponentRegistryException; 5 6 import clarin.cmdi.componentregistry.Configuration; 6 7 import clarin.cmdi.componentregistry.DeleteFailedException; … … 356 357 @Override 357 358 public void deleteMDComponent(String componentId, Principal principal, boolean forceDelete) throws IOException, 358 UserUnauthorizedException, DeleteFailedException {359 UserUnauthorizedException, DeleteFailedException, ComponentRegistryException { 359 360 ComponentDescription desc = componentDescriptions.get(componentId); 360 361 if (desc != null) { -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentClosure.java
r1089 r1352 2 2 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 4 5 import clarin.cmdi.componentregistry.model.ComponentDescription; 5 6 … … 7 8 8 9 @Override 9 public ComponentDescription getDescription(ComponentRegistry registry, String id) {10 public ComponentDescription getDescription(ComponentRegistry registry, String id) throws ComponentRegistryException { 10 11 return registry.getComponentDescription(id); 11 12 } -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r1345 r1352 33 33 34 34 import clarin.cmdi.componentregistry.ComponentRegistry; 35 import clarin.cmdi.componentregistry.ComponentRegistryException; 35 36 import clarin.cmdi.componentregistry.ComponentRegistryFactory; 36 37 import clarin.cmdi.componentregistry.DeleteFailedException; … … 62 63 public static final String DOMAIN_FORM_FIELD = "domainName"; 63 64 public static final String USERSPACE_PARAM = "userspace"; 64 65 66 @Inject(value="componentRegistryFactory") 65 @Inject(value = "componentRegistryFactory") 67 66 private ComponentRegistryFactory componentRegistryFactory; 68 67 69 68 private ComponentRegistry getRegistry(boolean userspace) { 70 71 72 69 Principal userPrincipal = security.getUserPrincipal(); 70 UserCredentials userCredentials = getUserCredentials(userPrincipal); 71 return getRegistry(userspace, userCredentials); 73 72 } 74 73 75 74 private ComponentRegistry getRegistry(boolean userspace, UserCredentials userCredentials) { 76 75 return componentRegistryFactory.getComponentRegistry(userspace, userCredentials); 77 76 } 78 77 79 78 private Principal checkAndGetUserPrincipal() { 80 81 82 83 84 79 Principal principal = security.getUserPrincipal(); 80 if (principal == null) { 81 throw new IllegalArgumentException("no user principal found."); 82 } 83 return principal; 85 84 } 86 85 87 86 private UserCredentials getUserCredentials(Principal userPrincipal) { 88 89 90 91 92 87 UserCredentials userCredentials = null; 88 if (userPrincipal != null) { 89 userCredentials = new UserCredentials(userPrincipal); 90 } 91 return userCredentials; 93 92 } 94 93 … … 96 95 @Path("/components") 97 96 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 98 public List<ComponentDescription> getRegisteredComponents(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) {99 100 101 102 103 97 public List<ComponentDescription> getRegisteredComponents(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 98 long start = System.currentTimeMillis(); 99 List<ComponentDescription> components = getRegistry(userspace).getComponentDescriptions(); 100 LOG.info("Releasing " + components.size() + " registered components into the world (" + (System.currentTimeMillis() - start) 101 + " millisecs)"); 102 return components; 104 103 } 105 104 … … 107 106 @Path("/profiles") 108 107 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 109 public List<ProfileDescription> getRegisteredProfiles(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) {110 111 112 113 114 108 public List<ProfileDescription> getRegisteredProfiles(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 109 long start = System.currentTimeMillis(); 110 List<ProfileDescription> profiles = getRegistry(userspace).getProfileDescriptions(); 111 LOG.info("Releasing " + profiles.size() + " registered profiles into the world (" + (System.currentTimeMillis() - start) 112 + " millisecs)"); 113 return profiles; 115 114 } 116 115 … … 119 118 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 120 119 public CMDComponentSpec getRegisteredComponent(@PathParam("componentId") String componentId, 121 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace){122 123 120 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 121 LOG.info("Component with id: " + componentId + " is requested."); 122 return getRegistry(userspace).getMDComponent(componentId); 124 123 } 125 124 … … 128 127 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML}) 129 128 public Response getRegisteredComponentRawType(@PathParam("componentId") final String componentId, @PathParam("rawType") String rawType) { 130 LOG.info("Component with id: " + componentId + " and rawType:" + rawType + " is requested."); 131 StreamingOutput result = null; 132 final ComponentRegistry registry = findRegistry(componentId, new ComponentClosure()); 133 if (registry == null) { 134 return Response.status(Status.NOT_FOUND).entity("Id: " + componentId + " is not registered, cannot create data.").build(); 135 } 136 ComponentDescription desc = registry.getComponentDescription(componentId); 137 checkAndThrowDescription(desc, componentId); 138 String fileName = desc.getName() + "." + rawType; 139 if ("xml".equalsIgnoreCase(rawType)) { 140 result = new StreamingOutput() { 141 142 public void write(OutputStream output) throws IOException, WebApplicationException { 143 registry.getMDComponentAsXml(componentId, output); 144 } 145 }; 146 } else if ("xsd".equalsIgnoreCase(rawType)) { 147 result = new StreamingOutput() { 148 149 public void write(OutputStream output) throws IOException, WebApplicationException { 150 registry.getMDComponentAsXsd(componentId, output); 151 } 152 }; 153 } else { 154 throw new WebApplicationException(Response.serverError().entity( 155 "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); 156 } 157 return createDownloadResponse(result, fileName); 158 } 159 160 public ComponentRegistry findRegistry(String id, RegistryClosure<? extends AbstractDescription> clos) { 161 AbstractDescription desc = null; 162 ComponentRegistry result = getRegistry(false); 163 desc = clos.getDescription(result, id); 164 if (desc == null) { 165 List<ComponentRegistry> userRegs = componentRegistryFactory.getAllUserRegistries(); 166 for (ComponentRegistry reg : userRegs) { 167 desc = clos.getDescription(reg, id); 168 if (desc != null) { 169 result = reg; 170 break; 171 } 172 } 173 } 174 return result; 129 LOG.info("Component with id: " + componentId + " and rawType:" + rawType + " is requested."); 130 StreamingOutput result = null; 131 try { 132 final ComponentRegistry registry = findRegistry(componentId, new ComponentClosure()); 133 if (registry == null) { 134 return Response.status(Status.NOT_FOUND).entity("Id: " + componentId + " is not registered, cannot create data.").build(); 135 } 136 ComponentDescription desc = registry.getComponentDescription(componentId); 137 checkAndThrowDescription(desc, componentId); 138 String fileName = desc.getName() + "." + rawType; 139 if ("xml".equalsIgnoreCase(rawType)) { 140 result = new StreamingOutput() { 141 142 @Override 143 public void write(OutputStream output) throws IOException, WebApplicationException { 144 try { 145 registry.getMDComponentAsXml(componentId, output); 146 } catch (ComponentRegistryException e) { 147 LOG.info("Could not retrieve component", e); 148 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 149 } 150 } 151 }; 152 } else if ("xsd".equalsIgnoreCase(rawType)) { 153 result = new StreamingOutput() { 154 155 @Override 156 public void write(OutputStream output) throws IOException, WebApplicationException { 157 try { 158 registry.getMDComponentAsXsd(componentId, output); 159 } catch (ComponentRegistryException e) { 160 LOG.info("Could not retrieve component", e); 161 throw new WebApplicationException(e, Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 162 } 163 164 } 165 }; 166 } else { 167 throw new WebApplicationException(Response.serverError().entity( 168 "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); 169 } 170 return createDownloadResponse(result, fileName); 171 } catch (ComponentRegistryException e) { 172 LOG.info("Could not retrieve component", e); 173 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 174 } 175 } 176 177 public ComponentRegistry findRegistry(String id, RegistryClosure<? extends AbstractDescription> clos) throws ComponentRegistryException { 178 AbstractDescription desc = null; 179 ComponentRegistry result = getRegistry(false); 180 desc = clos.getDescription(result, id); 181 if (desc == null) { 182 List<ComponentRegistry> userRegs = componentRegistryFactory.getAllUserRegistries(); 183 for (ComponentRegistry reg : userRegs) { 184 desc = clos.getDescription(reg, id); 185 if (desc != null) { 186 result = reg; 187 break; 188 } 189 } 190 } 191 return result; 175 192 } 176 193 … … 179 196 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 180 197 public CMDComponentSpec getRegisteredProfile(@PathParam("profileId") String profileId, 181 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace){182 183 198 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 199 LOG.info("Profile with id: " + profileId + " is requested."); 200 return getRegistry(userspace).getMDProfile(profileId); 184 201 } 185 202 … … 195 212 @Path("/profiles/{profileId}") 196 213 public Response manipulateRegisteredProfile(@PathParam("profileId") String profileId, @FormParam("method") String method, 197 198 199 200 201 202 214 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 215 if ("delete".equalsIgnoreCase(method)) { 216 return deleteRegisteredProfile(profileId, userspace); 217 } else { 218 return Response.ok().build(); 219 } 203 220 } 204 221 … … 207 224 @Consumes("multipart/form-data") 208 225 public Response publishRegisteredProfile(@PathParam("profileId") String profileId, @FormDataParam(DATA_FORM_FIELD) InputStream input, 209 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 210 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 211 Principal principal = checkAndGetUserPrincipal(); 212 ProfileDescription desc = getRegistry(true).getProfileDescription(profileId); 213 if (desc != null) { 214 updateDescription(desc, name, description, domainName, group); 215 return register(input, desc, getUserCredentials(principal), true, new PublishAction(principal)); 216 } else { 217 LOG.error("Update of nonexistent id (" + profileId + ") failed."); 218 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 219 } 226 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 227 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 228 Principal principal = checkAndGetUserPrincipal(); 229 try { 230 ProfileDescription desc = getRegistry(true).getProfileDescription(profileId); 231 if (desc != null) { 232 updateDescription(desc, name, description, domainName, group); 233 return register(input, desc, getUserCredentials(principal), true, new PublishAction(principal)); 234 } else { 235 LOG.error("Update of nonexistent id (" + profileId + ") failed."); 236 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 237 } 238 } catch (ComponentRegistryException e) { 239 LOG.info("Could not retrieve component", e); 240 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 241 } 220 242 } 221 243 … … 224 246 @Consumes("multipart/form-data") 225 247 public Response updateRegisteredProfile(@PathParam("profileId") String profileId, 226 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @FormDataParam(DATA_FORM_FIELD) InputStream input, 227 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 228 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 229 Principal principal = checkAndGetUserPrincipal(); 230 UserCredentials userCredentials = getUserCredentials(principal); 231 ProfileDescription desc = getRegistry(userspace).getProfileDescription(profileId); 232 if (desc != null) { 233 updateDescription(desc, name, description, domainName, group); 234 return register(input, desc, userCredentials, userspace, new UpdateAction()); 235 } else { 236 LOG.error("Update of nonexistent id (" + profileId + ") failed."); 237 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 238 } 248 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @FormDataParam(DATA_FORM_FIELD) InputStream input, 249 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 250 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 251 Principal principal = checkAndGetUserPrincipal(); 252 UserCredentials userCredentials = getUserCredentials(principal); 253 try { 254 ProfileDescription desc = getRegistry(userspace).getProfileDescription(profileId); 255 if (desc != null) { 256 updateDescription(desc, name, description, domainName, group); 257 return register(input, desc, userCredentials, userspace, new UpdateAction()); 258 } else { 259 LOG.error("Update of nonexistent id (" + profileId + ") failed."); 260 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 261 } 262 } catch (ComponentRegistryException e) { 263 LOG.info("Could not retrieve component", e); 264 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 265 } 239 266 } 240 267 … … 250 277 @Path("/components/{componentId}") 251 278 public Response manipulateRegisteredComponent(@PathParam("componentId") String componentId, @FormParam("method") String method, 252 253 254 255 256 257 279 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 280 if ("delete".equalsIgnoreCase(method)) { 281 return deleteRegisteredComponent(componentId, userspace); 282 } else { 283 return Response.ok().build(); 284 } 258 285 } 259 286 … … 262 289 @Consumes("multipart/form-data") 263 290 public Response publishRegisteredComponent(@PathParam("componentId") String componentId, 264 @FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 265 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, 266 @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 267 Principal principal = checkAndGetUserPrincipal(); 268 ComponentDescription desc = getRegistry(true).getComponentDescription(componentId); 269 if (desc != null) { 270 updateDescription(desc, name, description, domainName, group); 271 return register(input, desc, getUserCredentials(principal), true, new PublishAction(principal)); 272 } else { 273 LOG.error("Update of nonexistent id (" + componentId + ") failed."); 274 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 275 } 291 @FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 292 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, 293 @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 294 Principal principal = checkAndGetUserPrincipal(); 295 try { 296 ComponentDescription desc = getRegistry(true).getComponentDescription(componentId); 297 if (desc != null) { 298 updateDescription(desc, name, description, domainName, group); 299 return register(input, desc, getUserCredentials(principal), true, new PublishAction(principal)); 300 } else { 301 LOG.error("Update of nonexistent id (" + componentId + ") failed."); 302 return Response.serverError().entity("Invalid id, cannot update nonexistent profile").build(); 303 } 304 } catch (ComponentRegistryException e) { 305 LOG.info("Could not retrieve component", e); 306 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 307 } 276 308 } 277 309 … … 280 312 @Consumes("multipart/form-data") 281 313 public Response updateRegisteredComponent(@PathParam("componentId") String componentId, 282 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @FormDataParam(DATA_FORM_FIELD) InputStream input, 283 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 284 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 285 Principal principal = checkAndGetUserPrincipal(); 286 ComponentDescription desc = getRegistry(userspace).getComponentDescription(componentId); 287 if (desc != null) { 288 updateDescription(desc, name, description, domainName, group); 289 return register(input, desc, getUserCredentials(principal), userspace, new UpdateAction()); 290 } else { 291 LOG.error("Update of nonexistent id (" + componentId + ") failed."); 292 return Response.serverError().entity("Invalid id, cannot update nonexistent component").build(); 293 } 314 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @FormDataParam(DATA_FORM_FIELD) InputStream input, 315 @FormDataParam(NAME_FORM_FIELD) String name, @FormDataParam(DESCRIPTION_FORM_FIELD) String description, 316 @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName) { 317 Principal principal = checkAndGetUserPrincipal(); 318 try { 319 ComponentDescription desc = getRegistry(userspace).getComponentDescription(componentId); 320 if (desc != null) { 321 updateDescription(desc, name, description, domainName, group); 322 return register(input, desc, getUserCredentials(principal), userspace, new UpdateAction()); 323 } else { 324 LOG.error("Update of nonexistent id (" + componentId + ") failed."); 325 return Response.serverError().entity("Invalid id, cannot update nonexistent component").build(); 326 } 327 } catch (ComponentRegistryException e) { 328 LOG.info("Could not retrieve component", e); 329 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 330 } 294 331 } 295 332 296 333 private void updateDescription(AbstractDescription desc, String name, String description, String domainName, String group) { 297 298 299 300 301 334 desc.setName(name); 335 desc.setDescription(description); 336 desc.setDomainName(domainName); 337 desc.setGroupName(group); 338 desc.setRegistrationDate(AbstractDescription.createNewDate()); 302 339 } 303 340 … … 305 342 @Path("/components/{componentId}") 306 343 public Response deleteRegisteredComponent(@PathParam("componentId") String componentId, 307 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 308 Principal principal = checkAndGetUserPrincipal(); 309 ComponentRegistry registry = getRegistry(userspace); 310 LOG.info("Component with id: " + componentId + " set for deletion."); 311 try { 312 registry.deleteMDComponent(componentId, principal, false); 313 } catch (DeleteFailedException e) { 314 LOG.info("Component with id: " + componentId + " deletion failed.", e); 315 return Response.status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 316 } catch (IOException e) { 317 LOG.info("Component with id: " + componentId + " deletion failed.", e); 318 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 319 } catch (UserUnauthorizedException e) { 320 LOG.info("Component with id: " + componentId + " deletion failed: " + e.getMessage()); 321 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 322 } 323 LOG.info("Component with id: " + componentId + " deleted."); 324 return Response.ok().build(); 344 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 345 Principal principal = checkAndGetUserPrincipal(); 346 ComponentRegistry registry = getRegistry(userspace); 347 LOG.info("Component with id: " + componentId + " set for deletion."); 348 try { 349 registry.deleteMDComponent(componentId, principal, false); 350 } catch (DeleteFailedException e) { 351 LOG.info("Component with id: " + componentId + " deletion failed.", e); 352 return Response.status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 353 } catch (ComponentRegistryException e) { 354 LOG.info("Component with id: " + componentId + " deletion failed.", e); 355 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 356 } catch (IOException e) { 357 LOG.info("Component with id: " + componentId + " deletion failed.", e); 358 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 359 } catch (UserUnauthorizedException e) { 360 LOG.info("Component with id: " + componentId + " deletion failed: " + e.getMessage()); 361 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 362 } 363 LOG.info("Component with id: " + componentId + " deleted."); 364 return Response.ok().build(); 325 365 } 326 366 … … 328 368 @Path("/profiles/{profileId}") 329 369 public Response deleteRegisteredProfile(@PathParam("profileId") String profileId, 330 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 331 Principal principal = checkAndGetUserPrincipal(); 332 LOG.info("Profile with id: " + profileId + " set for deletion."); 333 try { 334 getRegistry(userspace).deleteMDProfile(profileId, principal); 335 } catch (DeleteFailedException e) { 336 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 337 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 338 } catch (IOException e) { 339 LOG.info("Profile with id: " + profileId + " deletion failed.", e); 340 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 341 } catch (UserUnauthorizedException e) { 342 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 343 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 344 } 345 LOG.info("Profile with id: " + profileId + " deleted."); 346 return Response.ok().build(); 370 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 371 Principal principal = checkAndGetUserPrincipal(); 372 LOG.info("Profile with id: " + profileId + " set for deletion."); 373 try { 374 getRegistry(userspace).deleteMDProfile(profileId, principal); 375 } catch (DeleteFailedException e) { 376 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 377 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 378 } catch (ComponentRegistryException e) { 379 LOG.info("Could not retrieve component", e); 380 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 381 } catch (IOException e) { 382 LOG.info("Profile with id: " + profileId + " deletion failed.", e); 383 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 384 } catch (UserUnauthorizedException e) { 385 LOG.info("Profile with id: " + profileId + " deletion failed: " + e.getMessage()); 386 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 387 } 388 LOG.info("Profile with id: " + profileId + " deleted."); 389 return Response.ok().build(); 347 390 } 348 391 … … 351 394 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML}) 352 395 public Response getRegisteredProfileRawType(@PathParam("profileId") final String profileId, @PathParam("rawType") String rawType) { 353 LOG.info("Profile with id: " + profileId + " and rawType:" + rawType + " is requested."); 354 StreamingOutput result = null; 355 final ComponentRegistry registry = findRegistry(profileId, new ProfileClosure()); 356 if (registry == null) { 357 return Response.status(Status.NOT_FOUND).entity("Id: " + profileId + " is not registered, cannot create data.").build(); 358 } 359 ProfileDescription desc = registry.getProfileDescription(profileId); 360 checkAndThrowDescription(desc, profileId); 361 String fileName = desc.getName() + "." + rawType; 362 363 if ("xml".equalsIgnoreCase(rawType)) { 364 result = new StreamingOutput() { 365 366 public void write(OutputStream output) throws IOException, WebApplicationException { 367 registry.getMDProfileAsXml(profileId, output); 368 } 369 }; 370 } else if ("xsd".equalsIgnoreCase(rawType)) { 371 result = new StreamingOutput() { 372 373 public void write(OutputStream output) throws IOException, WebApplicationException { 374 registry.getMDProfileAsXsd(profileId, output); 375 } 376 }; 377 } else { 378 throw new WebApplicationException(Response.serverError().entity( 379 "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); 380 } 381 return createDownloadResponse(result, fileName); 396 LOG.info("Profile with id: " + profileId + " and rawType:" + rawType + " is requested."); 397 StreamingOutput result = null; 398 try { 399 final ComponentRegistry registry = findRegistry(profileId, new ProfileClosure()); 400 if (registry == null) { 401 return Response.status(Status.NOT_FOUND).entity("Id: " + profileId + " is not registered, cannot create data.").build(); 402 } 403 ProfileDescription desc = registry.getProfileDescription(profileId); 404 checkAndThrowDescription(desc, profileId); 405 String fileName = desc.getName() + "." + rawType; 406 407 if ("xml".equalsIgnoreCase(rawType)) { 408 result = new StreamingOutput() { 409 410 @Override 411 public void write(OutputStream output) throws IOException, WebApplicationException { 412 try { 413 registry.getMDProfileAsXml(profileId, output); 414 } catch (ComponentRegistryException e) { 415 LOG.info("Could not retrieve component", e); 416 throw new WebApplicationException(Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 417 } 418 } 419 }; 420 } else if ("xsd".equalsIgnoreCase(rawType)) { 421 result = new StreamingOutput() { 422 423 @Override 424 public void write(OutputStream output) throws IOException, WebApplicationException { 425 try { 426 registry.getMDProfileAsXsd(profileId, output); 427 } catch (ComponentRegistryException e) { 428 LOG.info("Could not retrieve component", e); 429 throw new WebApplicationException(Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build()); 430 } 431 } 432 }; 433 } else { 434 throw new WebApplicationException(Response.serverError().entity( 435 "unsupported rawType: " + rawType + " (only xml or xsd are supported)").build()); 436 } 437 return createDownloadResponse(result, fileName); 438 } catch (ComponentRegistryException e) { 439 LOG.info("Could not retrieve component", e); 440 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 441 } 382 442 } 383 443 384 444 private void checkAndThrowDescription(AbstractDescription desc, String id) { 385 386 387 445 if (desc == null) { 446 throw new WebApplicationException(Response.serverError().entity("Incorrect id:" + id + "cannot handle request").build()); 447 } 388 448 } 389 449 390 450 private Response createDownloadResponse(StreamingOutput result, String fileName) { 391 392 393 394 451 //Making response so it triggers browsers native save as dialog. 452 Response response = Response.ok().type("application/x-download").header("Content-Disposition", 453 "attachment; filename=\"" + fileName + "\"").entity(result).build(); 454 return response; 395 455 396 456 } … … 401 461 @Consumes("multipart/form-data") 402 462 public Response registerProfile(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 403 404 405 406 407 408 463 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, @FormDataParam(DOMAIN_FORM_FIELD) String domainName, 464 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 465 Principal principal = checkAndGetUserPrincipal(); 466 UserCredentials userCredentials = getUserCredentials(principal); 467 ProfileDescription desc = createNewProfileDescription(); 468 desc.setCreatorName(userCredentials.getDisplayName()); 409 469 desc.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 410 411 412 413 414 415 470 desc.setName(name); 471 desc.setDescription(description); 472 desc.setGroupName(group); 473 desc.setDomainName(domainName); 474 LOG.info("Trying to register Profile: " + desc); 475 return register(input, desc, userCredentials, userspace, new NewAction()); 416 476 } 417 477 … … 421 481 @Consumes("multipart/form-data") 422 482 public Response registerComponent(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String name, 423 424 425 426 427 428 429 430 431 432 433 434 435 483 @FormDataParam(DESCRIPTION_FORM_FIELD) String description, @FormDataParam(GROUP_FORM_FIELD) String group, 484 @FormDataParam(DOMAIN_FORM_FIELD) String domainName, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 485 Principal principal = checkAndGetUserPrincipal(); 486 UserCredentials userCredentials = getUserCredentials(principal); 487 ComponentDescription desc = createNewComponentDescription(); 488 desc.setCreatorName(userCredentials.getDisplayName()); 489 desc.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 490 desc.setName(name); 491 desc.setDescription(description); 492 desc.setGroupName(group); 493 desc.setDomainName(domainName); 494 LOG.info("Trying to register Component: " + desc); 495 return register(input, desc, userCredentials, userspace, new NewAction()); 436 496 } 437 497 … … 440 500 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 441 501 public Response pingSession() { 442 443 444 445 446 447 448 449 450 502 boolean stillActive = false; 503 Principal userPrincipal = security.getUserPrincipal(); 504 LOG.info("ping by user: " + (userPrincipal == null ? "null" : userPrincipal.getName())); 505 if (request != null) { 506 if (userPrincipal != null && !ComponentRegistryFactory.ANONYMOUS_USER.equals(userPrincipal.getName())) { 507 stillActive = !((HttpServletRequest) request).getSession().isNew(); 508 } 509 } 510 return Response.ok().entity("<session stillActive=\"" + stillActive + "\"/>").build(); 451 511 } 452 512 453 513 private Response register(InputStream input, AbstractDescription desc, UserCredentials userCredentials, boolean userspace, 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 514 RegisterAction action) { 515 try { 516 ComponentRegistry registry = getRegistry(userspace, userCredentials); 517 DescriptionValidator descriptionValidator = new DescriptionValidator(desc); 518 MDValidator validator = new MDValidator(input, desc, registry, getRegistry(true), componentRegistryFactory.getPublicRegistry()); 519 RegisterResponse response = new RegisterResponse(); 520 response.setIsInUserSpace(userspace); 521 validate(response, descriptionValidator, validator); 522 if (response.getErrors().isEmpty()) { 523 CMDComponentSpec spec = validator.getCMDComponentSpec(); 524 int returnCode = action.execute(desc, spec, response, registry); 525 if (returnCode == 0) { 526 response.setRegistered(true); 527 response.setDescription(desc); 528 } else { 529 response.setRegistered(false); 530 response.addError("Unable to register at this moment. Internal server error."); 531 } 532 } else { 533 LOG.info("Registration failed with validation errors:" + Arrays.toString(response.getErrors().toArray())); 534 response.setRegistered(false); 535 } 536 response.setIsProfile(desc.isProfile()); 537 return Response.ok(response).build(); 538 } finally { 539 try { 540 input.close();//either we read the input or there was an exception, we need to close it. 541 } catch (IOException e) { 542 LOG.error("Error when closing inputstream: ", e); 543 } 544 } 485 545 } 486 546 487 547 private ComponentDescription createNewComponentDescription() { 488 489 490 548 ComponentDescription desc = ComponentDescription.createNewDescription(); 549 desc.setHref(createXlink(desc.getId())); 550 return desc; 491 551 } 492 552 493 553 private ProfileDescription createNewProfileDescription() { 494 495 496 554 ProfileDescription desc = ProfileDescription.createNewDescription(); 555 desc.setHref(createXlink(desc.getId())); 556 return desc; 497 557 } 498 558 499 559 private String createXlink(String id) { 500 501 560 URI uri = uriInfo.getRequestUriBuilder().path(id).build(); 561 return uri.toString(); 502 562 } 503 563 504 564 private void validate(RegisterResponse response, Validator... validators) { 505 506 507 508 509 510 511 565 for (Validator validator : validators) { 566 if (!validator.validate()) { 567 for (String error : validator.getErrorMessages()) { 568 response.addError(error); 569 } 570 } 571 } 512 572 } 513 573 -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java
r1282 r1352 8 8 9 9 import clarin.cmdi.componentregistry.ComponentRegistry; 10 import clarin.cmdi.componentregistry.ComponentRegistryException; 10 11 import clarin.cmdi.componentregistry.MDMarshaller; 11 12 import clarin.cmdi.componentregistry.components.CMDComponentSpec; … … 20 21 static final String PARSE_ERROR = "Error in validation input file. Error is: "; 21 22 static final String COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR = "referenced component cannot be found in the published components: "; 23 static final String COMPONENT_REGISTRY_EXCEPTION_ERROR = "An exception occurred while accessing the component registry: "; 22 24 23 25 private List<String> errorMessages = new ArrayList<String>(); … … 60 62 } 61 63 if (errorMessages.isEmpty()) { 62 validateComponents(spec.getCMDComponent()); 64 try { 65 validateComponents(spec.getCMDComponent()); 66 } catch (ComponentRegistryException e) { 67 errorMessages.add(COMPONENT_REGISTRY_EXCEPTION_ERROR + e); 68 } 63 69 } 64 70 return errorMessages.isEmpty(); 65 71 } 66 72 67 private void validateComponents(List<CMDComponentType> cmdComponents) {73 private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException { 68 74 for (CMDComponentType cmdComponentType : cmdComponents) { 69 75 validateDescribedComponents(cmdComponentType); … … 72 78 } 73 79 74 private void validateDescribedComponents(CMDComponentType cmdComponentType) {80 private void validateDescribedComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException { 75 81 if (isDefinedInSeparateFile(cmdComponentType)) { 76 82 String id = cmdComponentType.getComponentId(); -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ProfileClosure.java
r1089 r1352 2 2 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 4 5 import clarin.cmdi.componentregistry.model.ProfileDescription; 5 6 … … 7 8 8 9 @Override 9 public ProfileDescription getDescription(ComponentRegistry registry, String id) {10 public ProfileDescription getDescription(ComponentRegistry registry, String id) throws ComponentRegistryException { 10 11 return registry.getProfileDescription(id); 11 12 } -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/RegistryClosure.java
r1089 r1352 2 2 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 4 5 import clarin.cmdi.componentregistry.model.AbstractDescription; 5 6 6 7 public interface RegistryClosure<T extends AbstractDescription> { 7 8 8 T getDescription(ComponentRegistry registry, String id) ;9 T getDescription(ComponentRegistry registry, String id) throws ComponentRegistryException; 9 10 10 11 -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImplTest.java
r1346 r1352 2 2 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 4 5 import clarin.cmdi.componentregistry.DeleteFailedException; 5 6 import clarin.cmdi.componentregistry.UserCredentials; … … 160 161 } 161 162 162 private ProfileDescription createProfile(ComponentRegistry register) throws IOException, JAXBException, DeleteFailedException {163 private ProfileDescription createProfile(ComponentRegistry register) throws IOException, JAXBException, DeleteFailedException, ComponentRegistryException { 163 164 ProfileDescription description = ProfileDescription.createNewDescription(); 164 165 description.setName("Aap"); … … 295 296 } 296 297 297 private ComponentDescription createComponent(ComponentRegistry registry) throws IOException, DeleteFailedException, JAXBException {298 private ComponentDescription createComponent(ComponentRegistry registry) throws IOException, DeleteFailedException, JAXBException, ComponentRegistryException { 298 299 ComponentDescription description = ComponentDescription. 299 300 createNewDescription(); -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/filesystem/ComponentRegistryImplTest.java
r1346 r1352 2 2 3 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 4 5 import clarin.cmdi.componentregistry.DeleteFailedException; 5 6 import clarin.cmdi.componentregistry.UserUnauthorizedException; … … 32 33 33 34 @Test 34 public void testRegisterProfile() throws JAXBException {35 public void testRegisterProfile() throws JAXBException, ComponentRegistryException { 35 36 ComponentRegistry register = getTestRegistry(getRegistryDir()); 36 37 ProfileDescription description = ProfileDescription.createNewDescription(); … … 62 63 63 64 @Test 64 public void testRegisterComponent() throws JAXBException {65 public void testRegisterComponent() throws JAXBException, ComponentRegistryException { 65 66 ComponentRegistry register = getTestRegistry(getRegistryDir()); 66 67 ComponentDescription description = ComponentDescription. -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java
r1348 r1352 15 15 import javax.ws.rs.core.Response.Status; 16 16 17 import org.apache.commons.codec.digest.DigestUtils;18 17 import org.junit.Test; 19 18 20 19 import clarin.cmdi.componentregistry.ComponentRegistry; 21 20 import clarin.cmdi.componentregistry.ComponentRegistryFactory; 22 import clarin.cmdi.componentregistry.impl.filesystem.ComponentRegistryFactoryImpl;23 21 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 24 22 import clarin.cmdi.componentregistry.model.AbstractDescription;
Note: See TracChangeset
for help on using the changeset viewer.